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 / Compare.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
Compare.php
226 lines
1 <?php
2
3 namespace {
4 /**
5 * Rule comparing the value of the field with some other value
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: Compare.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 comparing the value of the field with some other value
53 *
54 * The Rule needs two configuration parameters for its work
55 * - comparison operator (defaults to equality)
56 * - operand to compare with; this can be either a constant or another form
57 * element (its value will be used)
58 * See {@link mergeConfig()} for description of possible ways to pass
59 * configuration parameters.
60 *
61 * Note that 'less than [or equal]' and 'greater than [or equal]' operators
62 * compare the operands numerically, since this is considered as more useful
63 * approach by the authors.
64 *
65 * For convenience, this Rule is already registered in the Factory with the
66 * names 'eq', 'neq', 'lt', 'gt', 'lte', 'gte' corresponding to the relevant
67 * operators:
68 * <code>
69 * $password->addRule('eq', 'Passwords do not match', $passwordRepeat);
70 * $orderQty->addRule('lte', 'Should not order more than 10 of these', 10);
71 * </code>
72 *
73 * @category HTML
74 * @package HTML_QuickForm2
75 * @author Alexey Borzov <avb@php.net>
76 * @author Bertrand Mansion <golgote@mamasam.com>
77 * @version Release: @package_version@
78 */
79 class HTML_QuickForm2_Rule_Compare extends \HTML_QuickForm2_Rule
80 {
81 /**
82 * Possible comparison operators
83 * @var array
84 */
85 protected $operators = array('==', '!=', '===', '!==', '<', '<=', '>', '>=');
86 protected function doOperation($a, $b, $operator)
87 {
88 switch ($operator) {
89 case "==":
90 return $a == $b;
91 case "!=":
92 return $a != $b;
93 case "===":
94 return $a === $b;
95 case "!==":
96 return $a !== $b;
97 case ">":
98 return $a > $b;
99 case "<=":
100 return $a <= $b;
101 case "<":
102 return $a < $b;
103 case ">=":
104 return $a >= $b;
105 default:
106 return \true;
107 }
108 }
109 /**
110 * Validates the owner element
111 *
112 * @return bool whether (element_value operator operand) expression is true
113 */
114 protected function validateOwner()
115 {
116 $value = $this->owner->getValue();
117 $config = $this->getConfig();
118 if ($config['operand'] instanceof \HTML_QuickForm2_Node) {
119 $b = $config['operand']->getValue();
120 } else {
121 $b = $config['operand'];
122 }
123 if (!\in_array($config['operator'], array('===', '!=='))) {
124 $a = \floatval($value);
125 $b = \floatval($b);
126 } else {
127 $a = \strval($value);
128 $b = \strval($b);
129 }
130 return $this->doOperation($a, $b, $config['operator']);
131 }
132 protected function getJavascriptCallback()
133 {
134 $config = $this->getConfig();
135 $operand1 = $this->owner->getJavascriptValue();
136 $operand2 = $config['operand'] instanceof \HTML_QuickForm2_Node ? $config['operand']->getJavascriptValue() : "'" . \strtr($config['operand'], array("\r" => '\\r', "\n" => '\\n', "\t" => '\\t', "'" => "\\'", '"' => '\\"', '\\' => '\\\\')) . "'";
137 if (!\in_array($config['operator'], array('===', '!=='))) {
138 $check = "Number({$operand1}) {$config['operator']} Number({$operand2})";
139 } else {
140 $check = "String({$operand1}) {$config['operator']} String({$operand2})";
141 }
142 return "function () { return {$check}; }";
143 }
144 /**
145 * Merges local configuration with that provided for registerRule()
146 *
147 * "Global" configuration may be passed to
148 * {@link HTML_QuickForm2_Factory::registerRule()} in
149 * either of the following formats
150 * - operator
151 * - array(operator[, operand])
152 * - array(['operator' => operator, ]['operand' => operand])
153 * "Local" configuration may be passed to the constructor in either of
154 * the following formats
155 * - operand
156 * - array([operator, ]operand)
157 * - array(['operator' => operator, ]['operand' => operand])
158 *
159 * As usual, global configuration overrides local one.
160 *
161 * @param mixed Local configuration
162 * @param mixed Global configuration
163 * @return mixed Merged configuration
164 */
165 public static function mergeConfig($localConfig, $globalConfig)
166 {
167 $config = null;
168 if (\is_array($globalConfig) && 0 < \count($globalConfig)) {
169 $config = self::toCanonicalForm($globalConfig, 'operator');
170 }
171 if (\is_array($localConfig) && 0 < \count($localConfig)) {
172 $config = (isset($config) ? $config : array()) + self::toCanonicalForm($localConfig);
173 }
174 return $config;
175 }
176 /**
177 * Converts configuration data to a canonical associative array form
178 *
179 * @param mixed Configuration data
180 * @param string Array key to assign $config to if it is scalar
181 * @return array Associative array that may contain 'operand' and 'operator' keys
182 */
183 protected static function toCanonicalForm($config, $key = 'operand')
184 {
185 if (!\is_array($config)) {
186 return array($key => $config);
187 } elseif (\array_key_exists('operator', $config) || \array_key_exists('operand', $config)) {
188 return $config;
189 } elseif (1 == \count($config)) {
190 return array($key => \end($config));
191 } else {
192 return array('operator' => \reset($config), 'operand' => \end($config));
193 }
194 }
195 /**
196 * Sets the comparison operator and operand to compare to
197 *
198 * $config can be either of the following
199 * - operand
200 * - array([operator, ]operand)
201 * - array(['operator' => operator, ]['operand' => operand])
202 * If operator is missing it will default to '==='
203 *
204 * @param mixed Configuration data
205 * @return HTML_QuickForm2_Rule
206 * @throws HTML_QuickForm2_InvalidArgumentException if a bogus comparison
207 * operator is used for configuration, if an operand is missing
208 */
209 public function setConfig($config)
210 {
211 if (0 == \count($config)) {
212 throw new \HTML_QuickForm2_InvalidArgumentException('Compare Rule requires an argument to compare with');
213 }
214 $config = self::toCanonicalForm($config);
215 $config += array('operator' => '===');
216 if (!\in_array($config['operator'], $this->operators)) {
217 throw new \HTML_QuickForm2_InvalidArgumentException('Compare Rule requires a valid comparison operator, ' . \preg_replace('/\\s+/', ' ', \var_export($config['operator'], \true)) . ' given');
218 }
219 if (\in_array($config['operator'], array('==', '!='))) {
220 $config['operator'] .= '=';
221 }
222 return parent::setConfig($config);
223 }
224 }
225 }
226