PluginProbe ʕ •ᴥ•ʔ
Matomo Analytics – Powerful, Privacy-First Insights for WordPress / trunk
Matomo Analytics – Powerful, Privacy-First Insights for WordPress vtrunk
5.11.1 5.11.0 5.10.2 5.10.1 trunk 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.1.0 1.1.1 1.1.2 1.1.3 1.2.0 1.3.0 1.3.1 1.3.2 4.0.0 4.0.1 4.0.2 4.0.3 4.0.4 4.1.0 4.1.1 4.1.2 4.1.3 4.10.0 4.11.0 4.12.0 4.13.0 4.13.2 4.13.3 4.13.4 4.13.5 4.14.0 4.14.1 4.14.2 4.15.0 4.15.1 4.15.2 4.15.3 4.2.0 4.3.0 4.3.1 4.4.1 4.4.2 4.5.0 4.6.0 5.0.1 5.0.2 5.0.3 5.0.4 5.0.5 5.0.6 5.0.7 5.0.8 5.1.0 5.1.1 5.1.2 5.1.3 5.1.4 5.1.5 5.1.6 5.1.7 5.10.0 5.2.0 5.2.1 5.2.2 5.3.0 5.3.1 5.3.2 5.3.3 5.6.0 5.6.1 5.7.0 5.7.1 5.8.0 5.8.1 5.8.2
matomo / app / libs / HTML / QuickForm2 / Rule / Length.php
matomo / app / libs / HTML / QuickForm2 / Rule Last commit date
Callback.php 2 years ago Compare.php 2 years ago Each.php 2 years ago Empty.php 2 years ago Length.php 2 years ago MaxFileSize.php 2 years ago MimeType.php 2 years ago Nonempty.php 2 years ago NotCallback.php 2 years ago NotRegex.php 2 years ago Regex.php 2 years ago Required.php 2 years ago
Length.php
213 lines
1 <?php
2
3 namespace {
4 /**
5 * Rule checking the value's length
6 *
7 * PHP version 5
8 *
9 * LICENSE:
10 *
11 * Copyright (c) 2006-2010, Alexey Borzov <avb@php.net>,
12 * Bertrand Mansion <golgote@mamasam.com>
13 * All rights reserved.
14 *
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
17 * are met:
18 *
19 * * Redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer.
21 * * Redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution.
24 * * The names of the authors may not be used to endorse or promote products
25 * derived from this software without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
28 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
29 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
31 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
32 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
33 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
34 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
35 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
36 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
37 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 *
39 * @category HTML
40 * @package HTML_QuickForm2
41 * @author Alexey Borzov <avb@php.net>
42 * @author Bertrand Mansion <golgote@mamasam.com>
43 * @license http://opensource.org/licenses/bsd-license.php New BSD License
44 * @version SVN: $Id: Length.php 299480 2010-05-19 06:55:03Z avb $
45 * @link http://pear.php.net/package/HTML_QuickForm2
46 */
47 /**
48 * Base class for HTML_QuickForm2 rules
49 */
50 // require_once 'HTML/QuickForm2/Rule.php';
51 /**
52 * Rule checking the value's length
53 *
54 * The rule needs an "allowed length" parameter for its work, it can be either
55 * - a scalar: the value will be valid if it is exactly this long
56 * - an array: the value will be valid if its length is between the given values
57 * (inclusive). If one of these evaluates to 0, then length will be compared
58 * only with the remaining one.
59 * See {@link mergeConfig()} for description of possible ways to pass
60 * configuration parameters.
61 *
62 * The Rule considers empty fields as valid and doesn't try to compare their
63 * lengths with provided limits.
64 *
65 * For convenience this Rule is also registered with the names 'minlength' and
66 * 'maxlength' (having, respectively, 'max' and 'min' parameters set to 0):
67 * <code>
68 * $password->addRule('minlength', 'The password should be at least 6 characters long', 6);
69 * $message->addRule('maxlength', 'Your message is too verbose', 1000);
70 * </code>
71 *
72 * @category HTML
73 * @package HTML_QuickForm2
74 * @author Alexey Borzov <avb@php.net>
75 * @author Bertrand Mansion <golgote@mamasam.com>
76 * @version Release: @package_version@
77 */
78 class HTML_QuickForm2_Rule_Length extends \HTML_QuickForm2_Rule
79 {
80 /**
81 * Validates the owner element
82 *
83 * @return bool whether length of the element's value is within allowed range
84 */
85 protected function validateOwner()
86 {
87 if (0 == ($valueLength = \strlen($this->owner->getValue()))) {
88 return \true;
89 }
90 $allowedLength = $this->getConfig();
91 if (\is_scalar($allowedLength)) {
92 return $valueLength == $allowedLength;
93 } else {
94 return (empty($allowedLength['min']) || $valueLength >= $allowedLength['min']) && (empty($allowedLength['max']) || $valueLength <= $allowedLength['max']);
95 }
96 }
97 protected function getJavascriptCallback()
98 {
99 $allowedLength = $this->getConfig();
100 if (\is_scalar($allowedLength)) {
101 $check = "length == {$allowedLength}";
102 } else {
103 $checks = array();
104 if (!empty($allowedLength['min'])) {
105 $checks[] = "length >= {$allowedLength['min']}";
106 }
107 if (!empty($allowedLength['max'])) {
108 $checks[] = "length <= {$allowedLength['max']}";
109 }
110 $check = \implode(' && ', $checks);
111 }
112 return "function() { var length = " . $this->owner->getJavascriptValue() . ".length; if (0 == length) { return true; } else { return {$check}; } }";
113 }
114 /**
115 * Adds the 'min' and 'max' fields from one array to the other
116 *
117 * @param array Rule configuration, array with 'min' and 'max' keys
118 * @param array Additional configuration, fields will be added to
119 * $length if it doesn't contain such a key already
120 * @return array
121 */
122 protected static function mergeMinMaxLength($length, $config)
123 {
124 if (\array_key_exists('min', $config) || \array_key_exists('max', $config)) {
125 if (!\array_key_exists('min', $length) && \array_key_exists('min', $config)) {
126 $length['min'] = $config['min'];
127 }
128 if (!\array_key_exists('max', $length) && \array_key_exists('max', $config)) {
129 $length['max'] = $config['max'];
130 }
131 } else {
132 if (!\array_key_exists('min', $length)) {
133 $length['min'] = \reset($config);
134 }
135 if (!\array_key_exists('max', $length)) {
136 $length['max'] = \end($config);
137 }
138 }
139 return $length;
140 }
141 /**
142 * Merges length limits given on rule creation with those given to registerRule()
143 *
144 * "Global" length limits may be passed to
145 * {@link HTML_QuickForm2_Factory::registerRule()} in either of the
146 * following formats
147 * - scalar (exact length)
148 * - array(minlength, maxlength)
149 * - array(['min' => minlength, ]['max' => maxlength])
150 *
151 * "Local" length limits may be passed to the constructor in either of
152 * the following formats
153 * - scalar (if global config is unset then it is treated as an exact
154 * length, if 'min' or 'max' is in global config then it is treated
155 * as 'max' or 'min', respectively)
156 * - array(minlength, maxlength)
157 * - array(['min' => minlength, ]['max' => maxlength])
158 *
159 * As usual, global configuration overrides local one.
160 *
161 * @param int|array Local length limits
162 * @param int|array Global length limits, usually provided to {@link HTML_QuickForm2_Factory::registerRule()}
163 * @return int|array Merged length limits
164 */
165 public static function mergeConfig($localConfig, $globalConfig)
166 {
167 if (!isset($globalConfig)) {
168 $length = $localConfig;
169 } elseif (!\is_array($globalConfig)) {
170 $length = $globalConfig;
171 } else {
172 $length = self::mergeMinMaxLength(array(), $globalConfig);
173 if (isset($localConfig)) {
174 $length = self::mergeMinMaxLength($length, \is_array($localConfig) ? $localConfig : array($localConfig));
175 }
176 }
177 return $length;
178 }
179 /**
180 * Sets the allowed length limits
181 *
182 * $config can be either of the following
183 * - integer (rule checks for exact length)
184 * - array(minlength, maxlength)
185 * - array(['min' => minlength, ]['max' => maxlength])
186 *
187 * @param int|array Length limits
188 * @return HTML_QuickForm2_Rule
189 * @throws HTML_QuickForm2_InvalidArgumentException if bogus length limits
190 * were provided
191 */
192 public function setConfig($config)
193 {
194 if (\is_array($config)) {
195 $config = self::mergeMinMaxLength(array(), $config) + array('min' => 0, 'max' => 0);
196 }
197 if (\is_array($config) && ($config['min'] < 0 || $config['max'] < 0) || !\is_array($config) && $config < 0) {
198 throw new \HTML_QuickForm2_InvalidArgumentException('Length Rule requires limits to be nonnegative, ' . \preg_replace('/\\s+/', ' ', \var_export($config, \true)) . ' given');
199 } elseif (\is_array($config) && $config['min'] == 0 && $config['max'] == 0 || !\is_array($config) && 0 == $config) {
200 throw new \HTML_QuickForm2_InvalidArgumentException('Length Rule requires at least one non-zero limit, ' . \preg_replace('/\\s+/', ' ', \var_export($config, \true)) . ' given');
201 }
202 if (!empty($config['min']) && !empty($config['max'])) {
203 if ($config['min'] > $config['max']) {
204 list($config['min'], $config['max']) = array($config['max'], $config['min']);
205 } elseif ($config['min'] == $config['max']) {
206 $config = $config['min'];
207 }
208 }
209 return parent::setConfig($config);
210 }
211 }
212 }
213