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.php
matomo / app / libs / HTML / QuickForm2 Last commit date
Container 1 year ago Controller 2 years ago DataSource 2 years ago Element 1 year ago Renderer 1 year ago Rule 2 years ago Container.php 1 year ago Controller.php 1 year ago DataSource.php 2 years ago Element.php 2 years ago Exception.php 2 years ago Factory.php 2 years ago JavascriptBuilder.php 2 years ago Loader.php 2 years ago Node.php 1 year ago Renderer.php 1 year ago Rule.php 2 years ago
Rule.php
300 lines
1 <?php
2
3 namespace {
4 /**
5 * Base class for HTML_QuickForm2 rules
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: Rule.php 299706 2010-05-24 18:32:37Z avb $
45 * @link http://pear.php.net/package/HTML_QuickForm2
46 */
47 /**
48 * Abstract base class for HTML_QuickForm2 rules
49 *
50 * This class provides methods that allow chaining several rules together.
51 * Its validate() method executes the whole rule chain starting from this rule.
52 *
53 * @category HTML
54 * @package HTML_QuickForm2
55 * @author Alexey Borzov <avb@php.net>
56 * @author Bertrand Mansion <golgote@mamasam.com>
57 * @version Release: @package_version@
58 */
59 abstract class HTML_QuickForm2_Rule
60 {
61 /**
62 * Constant showing that validation should be run server-side
63 * @see HTML_QuickForm2_Node::addRule()
64 */
65 const RUNAT_SERVER = 1;
66 /**
67 * Constant showing that validation should be run client-side
68 * @see HTML_QuickForm2_Node::addRule()
69 */
70 const RUNAT_CLIENT = 2;
71 /**
72 * An element whose value will be validated by this rule
73 * @var HTML_QuickForm2_Node
74 */
75 protected $owner;
76 /**
77 * An error message to display if validation fails
78 * @var string
79 */
80 protected $message;
81 /**
82 * Configuration data for the rule
83 * @var mixed
84 */
85 protected $config;
86 /**
87 * Rules chained to this via "and" and "or" operators
88 *
89 * The contents can be described as "disjunctive normal form", where an outer
90 * array represents a disjunction of conjunctive clauses represented by inner
91 * arrays.
92 *
93 * @var array
94 */
95 protected $chainedRules = array(array());
96 /**
97 * Class constructor
98 *
99 * @param HTML_QuickForm2_Node Element to validate
100 * @param string Error message to display if validation fails
101 * @param mixed Configuration data for the rule
102 */
103 public function __construct(\HTML_QuickForm2_Node $owner, $message = '', $config = null)
104 {
105 $this->setOwner($owner);
106 $this->setMessage($message);
107 $this->setConfig($config);
108 }
109 /**
110 * Merges local configuration with that provided for registerRule()
111 *
112 * Default behaviour is for global config to override local one, different
113 * Rules may implement more complex merging behaviours.
114 *
115 * @param mixed Local configuration
116 * @param mixed Global configuration, usually provided to {@link HTML_QuickForm2_Factory::registerRule()}
117 * @return mixed Merged configuration
118 */
119 public static function mergeConfig($localConfig, $globalConfig)
120 {
121 return \is_null($globalConfig) ? $localConfig : $globalConfig;
122 }
123 /**
124 * Sets configuration data for the rule
125 *
126 * @param mixed Rule configuration data (specific for a Rule)
127 * @return HTML_QuickForm2_Rule
128 * @throws HTML_QuickForm2_InvalidArgumentException in case of invalid
129 * configuration data
130 */
131 public function setConfig($config)
132 {
133 $this->config = $config;
134 return $this;
135 }
136 /**
137 * Returns the rule's configuration data
138 *
139 * @return mixed Configuration data (specific for a Rule)
140 */
141 public function getConfig()
142 {
143 return $this->config;
144 }
145 /**
146 * Sets the error message output by the rule
147 *
148 * @param string Error message to display if validation fails
149 * @return HTML_QuickForm2_Rule
150 */
151 public function setMessage($message)
152 {
153 $this->message = (string) $message;
154 return $this;
155 }
156 /**
157 * Returns the error message output by the rule
158 *
159 * @return string Error message
160 */
161 public function getMessage()
162 {
163 return $this->message;
164 }
165 /**
166 * Sets the element that will be validated by this rule
167 *
168 * @param HTML_QuickForm2_Node Element to validate
169 */
170 public function setOwner(\HTML_QuickForm2_Node $owner)
171 {
172 if (null !== $this->owner) {
173 $this->owner->removeRule($this);
174 }
175 $this->owner = $owner;
176 }
177 /**
178 * Adds a rule to the chain with an "and" operator
179 *
180 * Evaluation is short-circuited, next rule will not be evaluated if the
181 * previous one returns false. The method is named this way because "and" is
182 * a reserved word in PHP.
183 *
184 * @param HTML_QuickForm2_Rule
185 * @return HTML_QuickForm2_Rule first rule in the chain (i.e. $this)
186 * @throws HTML_QuickForm2_InvalidArgumentException when trying to add
187 * a "required" rule to the chain
188 */
189 public function and_(\HTML_QuickForm2_Rule $next)
190 {
191 if ($next instanceof \HTML_QuickForm2_Rule_Required) {
192 throw new \HTML_QuickForm2_InvalidArgumentException('and_(): Cannot add a "required" rule');
193 }
194 $this->chainedRules[\count($this->chainedRules) - 1][] = $next;
195 return $this;
196 }
197 /**
198 * Adds a rule to the chain with an "or" operator
199 *
200 * Evaluation is short-circuited, next rule will not be evaluated if the
201 * previous one returns true. The method is named this way because "or" is
202 * a reserved word in PHP.
203 *
204 * @param HTML_QuickForm2_Rule
205 * @return HTML_QuickForm2_Rule first rule in the chain (i.e. $this)
206 * @throws HTML_QuickForm2_InvalidArgumentException when trying to add
207 * a "required" rule to the chain
208 */
209 public function or_(\HTML_QuickForm2_Rule $next)
210 {
211 if ($next instanceof \HTML_QuickForm2_Rule_Required) {
212 throw new \HTML_QuickForm2_InvalidArgumentException('or_(): Cannot add a "required" rule');
213 }
214 $this->chainedRules[] = array($next);
215 return $this;
216 }
217 /**
218 * Performs validation
219 *
220 * The whole rule chain is executed. Note that the side effect of this
221 * method is setting the error message on element if validation fails
222 *
223 * @return boolean Whether the element is valid
224 */
225 public function validate()
226 {
227 $globalValid = \false;
228 $localValid = $this->validateOwner();
229 foreach ($this->chainedRules as $item) {
230 foreach ($item as $multiplier) {
231 if (!($localValid = $localValid && $multiplier->validate())) {
232 break;
233 }
234 }
235 if ($globalValid = $globalValid || $localValid) {
236 break;
237 }
238 $localValid = \true;
239 }
240 $globalValid or $this->setOwnerError();
241 return $globalValid;
242 }
243 /**
244 * Validates the owner element
245 *
246 * @return bool Whether owner element is valid according to the rule
247 */
248 protected abstract function validateOwner();
249 /**
250 * Sets the error message on the owner element
251 */
252 protected function setOwnerError()
253 {
254 if (\strlen($this->getMessage()) && !$this->owner->getError()) {
255 $this->owner->setError($this->getMessage());
256 }
257 }
258 /**
259 * Returns the client-side validation callback
260 *
261 * This essentially builds a Javascript version of validateOwner() method,
262 * with element ID and Rule configuration hardcoded.
263 *
264 * @return string Javascript function to validate the element's value
265 * @throws HTML_QuickForm2_Exception if Rule can only be run server-side
266 */
267 protected function getJavascriptCallback()
268 {
269 throw new \HTML_QuickForm2_Exception(\get_class($this) . ' does not implement javascript validation');
270 }
271 /**
272 * Returns the client-side representation of the Rule
273 *
274 * The Javascript object returned contains the following fields:
275 * - callback: {@see getJavascriptCallback()}
276 * - elementId: element ID to set error for if validation fails
277 * - errorMessage: error message to set if validation fails
278 * - chained: chained rules, array of arrays like in $chainedRules property
279 *
280 * @return string
281 * @throws HTML_QuickForm2_Exception if Rule or its chained Rules can only
282 * be run server-side
283 */
284 public function getJavascript()
285 {
286 $js = "{\n\tcallback: " . $this->getJavascriptCallback() . ",\n" . "\telementId: '" . $this->owner->getId() . "',\n" . "\terrorMessage: '" . \strtr($this->getMessage(), array("\r" => '\\r', "\n" => '\\n', "\t" => '\\t', "'" => "\\'", '"' => '\\"', '\\' => '\\\\')) . "',\n\tchained: [";
287 $chained = array();
288 foreach ($this->chainedRules as $item) {
289 $multipliers = array();
290 foreach ($item as $multiplier) {
291 $multipliers[] = $multiplier->getJavascript();
292 }
293 $chained[] = '[' . \implode(",\n", $multipliers) . ']';
294 }
295 $js .= \implode(",\n", $chained) . "]\n}";
296 return $js;
297 }
298 }
299 }
300