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 / Callback.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
Callback.php
161 lines
1 <?php
2
3 namespace {
4 /**
5 * Rule checking the value via a callback function (method)
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: Callback.php 294057 2010-01-26 21:10:28Z 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 via a callback function (method)
53 *
54 * The Rule needs a valid callback as a configuration parameter for its work, it
55 * may also be given additional arguments to pass to the callback alongside the
56 * element's value. See {@link mergeConfig()} for description of possible ways
57 * to pass configuration parameters.
58 *
59 * The callback will be called with element's value as the first argument, if
60 * additional arguments were provided they'll be passed as well. It is expected
61 * to return false if the value is invalid and true if it is valid.
62 *
63 * Checking that the value is not empty:
64 * <code>
65 * $str->addRule('callback', 'The field should not be empty', 'strlen');
66 * </code>
67 * Checking that the value is in the given array:
68 * <code>
69 * $meta->addRule('callback', 'Unknown variable name',
70 * array('callback' => 'in_array',
71 * 'arguments' => array(array('foo', 'bar', 'baz'))));
72 * </code>
73 * The same, but with rule registering first:
74 * <code>
75 * HTML_QuickForm2_Factory::registerRule(
76 * 'in_array', 'HTML_QuickForm2_Rule_Callback',
77 * 'HTML/QuickForm2/Rule/Callback.php', 'in_array'
78 * );
79 * $meta->addRule('in_array', 'Unknown variable name', array(array('foo', 'bar', 'baz')));
80 * </code>
81 *
82 * @category HTML
83 * @package HTML_QuickForm2
84 * @author Alexey Borzov <avb@php.net>
85 * @author Bertrand Mansion <golgote@mamasam.com>
86 * @version Release: @package_version@
87 */
88 class HTML_QuickForm2_Rule_Callback extends \HTML_QuickForm2_Rule
89 {
90 /**
91 * Validates the owner element
92 *
93 * @return bool the value returned by a callback function
94 */
95 protected function validateOwner()
96 {
97 $value = $this->owner->getValue();
98 $config = $this->getConfig();
99 return (bool) \call_user_func_array($config['callback'], \array_merge(array($value), $config['arguments']));
100 }
101 /**
102 * Merges local configuration with that provided for registerRule()
103 *
104 * "Global" configuration may be passed to
105 * {@link HTML_QuickForm2_Factory::registerRule()} in either of the
106 * following formats
107 * - callback
108 * - array(['callback' => callback, ]['arguments' => array(...)])
109 *
110 * "Local" configuration may be passed to the constructor in either of
111 * the following formats
112 * - callback or arguments (interpretation depends on whether the global
113 * configuration already contains the callback)
114 * - array(['callback' => callback, ]['arguments' => array(...)])
115 *
116 * As usual, global config overrides local one. It is a good idea to use the
117 * associative array format to prevent ambiguity.
118 *
119 * @param mixed Local configuration
120 * @param mixed Global configuration
121 * @return mixed Merged configuration
122 */
123 public static function mergeConfig($localConfig, $globalConfig)
124 {
125 if (!isset($globalConfig)) {
126 $config = $localConfig;
127 } else {
128 if (!\is_array($globalConfig) || !isset($globalConfig['callback']) && !isset($globalConfig['arguments'])) {
129 $config = array('callback' => $globalConfig);
130 } else {
131 $config = $globalConfig;
132 }
133 if (\is_array($localConfig) && (isset($localConfig['callback']) || isset($localConfig['arguments']))) {
134 $config += $localConfig;
135 } elseif (isset($localConfig)) {
136 $config += array('callback' => $localConfig, 'arguments' => $localConfig);
137 }
138 }
139 return $config;
140 }
141 /**
142 * Sets the callback to use for validation and its additional arguments
143 *
144 * @param callback|array Callback or array ('callback' => validation callback,
145 * 'arguments' => additional arguments)
146 * @return HTML_QuickForm2_Rule
147 * @throws HTML_QuickForm2_InvalidArgumentException if callback is missing or invalid
148 */
149 public function setConfig($config)
150 {
151 if (!\is_array($config) || !isset($config['callback'])) {
152 $config = array('callback' => $config);
153 }
154 if (!\is_callable($config['callback'], \false, $callbackName)) {
155 throw new \HTML_QuickForm2_InvalidArgumentException('Callback Rule requires a valid callback, \'' . $callbackName . '\' was given');
156 }
157 return parent::setConfig($config + array('arguments' => array()));
158 }
159 }
160 }
161