PluginProbe ʕ •ᴥ•ʔ
Matomo Analytics – Powerful, Privacy-First Insights for WordPress / 1.3.1
Matomo Analytics – Powerful, Privacy-First Insights for WordPress v1.3.1
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 6 years ago Compare.php 6 years ago Each.php 6 years ago Empty.php 6 years ago Length.php 6 years ago MaxFileSize.php 6 years ago MimeType.php 6 years ago Nonempty.php 6 years ago NotCallback.php 6 years ago NotRegex.php 6 years ago Regex.php 6 years ago Required.php 6 years ago
Length.php
237 lines
1 <?php
2 /**
3 * Rule checking the value's length
4 *
5 * PHP version 5
6 *
7 * LICENSE:
8 *
9 * Copyright (c) 2006-2010, Alexey Borzov <avb@php.net>,
10 * Bertrand Mansion <golgote@mamasam.com>
11 * All rights reserved.
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
16 *
17 * * Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 * * Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in the
21 * documentation and/or other materials provided with the distribution.
22 * * The names of the authors may not be used to endorse or promote products
23 * derived from this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
26 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
27 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
29 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
33 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 *
37 * @category HTML
38 * @package HTML_QuickForm2
39 * @author Alexey Borzov <avb@php.net>
40 * @author Bertrand Mansion <golgote@mamasam.com>
41 * @license http://opensource.org/licenses/bsd-license.php New BSD License
42 * @version SVN: $Id: Length.php 299480 2010-05-19 06:55:03Z avb $
43 * @link http://pear.php.net/package/HTML_QuickForm2
44 */
45
46 /**
47 * Base class for HTML_QuickForm2 rules
48 */
49 // require_once 'HTML/QuickForm2/Rule.php';
50
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
91 $allowedLength = $this->getConfig();
92 if (is_scalar($allowedLength)) {
93 return $valueLength == $allowedLength;
94 } else {
95 return (empty($allowedLength['min']) || $valueLength >= $allowedLength['min']) &&
96 (empty($allowedLength['max']) || $valueLength <= $allowedLength['max']);
97 }
98 }
99
100 protected function getJavascriptCallback()
101 {
102 $allowedLength = $this->getConfig();
103 if (is_scalar($allowedLength)) {
104 $check = "length == {$allowedLength}";
105 } else {
106 $checks = array();
107 if (!empty($allowedLength['min'])) {
108 $checks[] = "length >= {$allowedLength['min']}";
109 }
110 if (!empty($allowedLength['max'])) {
111 $checks[] = "length <= {$allowedLength['max']}";
112 }
113 $check = implode(' && ', $checks);
114 }
115 return "function() { var length = " . $this->owner->getJavascriptValue() .
116 ".length; if (0 == length) { return true; } else { return {$check}; } }";
117 }
118
119 /**
120 * Adds the 'min' and 'max' fields from one array to the other
121 *
122 * @param array Rule configuration, array with 'min' and 'max' keys
123 * @param array Additional configuration, fields will be added to
124 * $length if it doesn't contain such a key already
125 * @return array
126 */
127 protected static function mergeMinMaxLength($length, $config)
128 {
129 if (array_key_exists('min', $config) || array_key_exists('max', $config)) {
130 if (!array_key_exists('min', $length) && array_key_exists('min', $config)) {
131 $length['min'] = $config['min'];
132 }
133 if (!array_key_exists('max', $length) && array_key_exists('max', $config)) {
134 $length['max'] = $config['max'];
135 }
136 } else {
137 if (!array_key_exists('min', $length)) {
138 $length['min'] = reset($config);
139 }
140 if (!array_key_exists('max', $length)) {
141 $length['max'] = end($config);
142 }
143 }
144 return $length;
145 }
146
147 /**
148 * Merges length limits given on rule creation with those given to registerRule()
149 *
150 * "Global" length limits may be passed to
151 * {@link HTML_QuickForm2_Factory::registerRule()} in either of the
152 * following formats
153 * - scalar (exact length)
154 * - array(minlength, maxlength)
155 * - array(['min' => minlength, ]['max' => maxlength])
156 *
157 * "Local" length limits may be passed to the constructor in either of
158 * the following formats
159 * - scalar (if global config is unset then it is treated as an exact
160 * length, if 'min' or 'max' is in global config then it is treated
161 * as 'max' or 'min', respectively)
162 * - array(minlength, maxlength)
163 * - array(['min' => minlength, ]['max' => maxlength])
164 *
165 * As usual, global configuration overrides local one.
166 *
167 * @param int|array Local length limits
168 * @param int|array Global length limits, usually provided to {@link HTML_QuickForm2_Factory::registerRule()}
169 * @return int|array Merged length limits
170 */
171 public static function mergeConfig($localConfig, $globalConfig)
172 {
173 if (!isset($globalConfig)) {
174 $length = $localConfig;
175
176 } elseif (!is_array($globalConfig)) {
177 $length = $globalConfig;
178
179 } else {
180 $length = self::mergeMinMaxLength(array(), $globalConfig);
181 if (isset($localConfig)) {
182 $length = self::mergeMinMaxLength(
183 $length, is_array($localConfig)? $localConfig: array($localConfig)
184 );
185 }
186 }
187 return $length;
188 }
189
190 /**
191 * Sets the allowed length limits
192 *
193 * $config can be either of the following
194 * - integer (rule checks for exact length)
195 * - array(minlength, maxlength)
196 * - array(['min' => minlength, ]['max' => maxlength])
197 *
198 * @param int|array Length limits
199 * @return HTML_QuickForm2_Rule
200 * @throws HTML_QuickForm2_InvalidArgumentException if bogus length limits
201 * were provided
202 */
203 public function setConfig($config)
204 {
205 if (is_array($config)) {
206 $config = self::mergeMinMaxLength(array(), $config)
207 + array('min' => 0, 'max' => 0);
208 }
209 if (is_array($config) && ($config['min'] < 0 || $config['max'] < 0) ||
210 !is_array($config) && $config < 0)
211 {
212 throw new HTML_QuickForm2_InvalidArgumentException(
213 'Length Rule requires limits to be nonnegative, ' .
214 preg_replace('/\s+/', ' ', var_export($config, true)) . ' given'
215 );
216
217 } elseif (is_array($config) && $config['min'] == 0 && $config['max'] == 0 ||
218 !is_array($config) && 0 == $config)
219 {
220 throw new HTML_QuickForm2_InvalidArgumentException(
221 'Length Rule requires at least one non-zero limit, ' .
222 preg_replace('/\s+/', ' ', var_export($config, true)) . ' given'
223 );
224 }
225
226 if (!empty($config['min']) && !empty($config['max'])) {
227 if ($config['min'] > $config['max']) {
228 list($config['min'], $config['max']) = array($config['max'], $config['min']);
229 } elseif ($config['min'] == $config['max']) {
230 $config = $config['min'];
231 }
232 }
233 return parent::setConfig($config);
234 }
235 }
236 ?>
237