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
Callback.php
173 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Rule checking the value via a callback function (method) |
| 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: Callback.php 294057 2010-01-26 21:10:28Z 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 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( |
| 100 | $config['callback'], array_merge(array($value), $config['arguments']) |
| 101 | ); |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Merges local configuration with that provided for registerRule() |
| 106 | * |
| 107 | * "Global" configuration may be passed to |
| 108 | * {@link HTML_QuickForm2_Factory::registerRule()} in either of the |
| 109 | * following formats |
| 110 | * - callback |
| 111 | * - array(['callback' => callback, ]['arguments' => array(...)]) |
| 112 | * |
| 113 | * "Local" configuration may be passed to the constructor in either of |
| 114 | * the following formats |
| 115 | * - callback or arguments (interpretation depends on whether the global |
| 116 | * configuration already contains the callback) |
| 117 | * - array(['callback' => callback, ]['arguments' => array(...)]) |
| 118 | * |
| 119 | * As usual, global config overrides local one. It is a good idea to use the |
| 120 | * associative array format to prevent ambiguity. |
| 121 | * |
| 122 | * @param mixed Local configuration |
| 123 | * @param mixed Global configuration |
| 124 | * @return mixed Merged configuration |
| 125 | */ |
| 126 | public static function mergeConfig($localConfig, $globalConfig) |
| 127 | { |
| 128 | if (!isset($globalConfig)) { |
| 129 | $config = $localConfig; |
| 130 | |
| 131 | } else { |
| 132 | if (!is_array($globalConfig) || |
| 133 | !isset($globalConfig['callback']) && !isset($globalConfig['arguments']) |
| 134 | ) { |
| 135 | $config = array('callback' => $globalConfig); |
| 136 | } else { |
| 137 | $config = $globalConfig; |
| 138 | } |
| 139 | if (is_array($localConfig) && (isset($localConfig['callback']) |
| 140 | || isset($localConfig['arguments'])) |
| 141 | ) { |
| 142 | $config += $localConfig; |
| 143 | } elseif(isset($localConfig)) { |
| 144 | $config += array('callback' => $localConfig, 'arguments' => $localConfig); |
| 145 | } |
| 146 | } |
| 147 | return $config; |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * Sets the callback to use for validation and its additional arguments |
| 152 | * |
| 153 | * @param callback|array Callback or array ('callback' => validation callback, |
| 154 | * 'arguments' => additional arguments) |
| 155 | * @return HTML_QuickForm2_Rule |
| 156 | * @throws HTML_QuickForm2_InvalidArgumentException if callback is missing or invalid |
| 157 | */ |
| 158 | public function setConfig($config) |
| 159 | { |
| 160 | if (!is_array($config) || !isset($config['callback'])) { |
| 161 | $config = array('callback' => $config); |
| 162 | } |
| 163 | if (!is_callable($config['callback'], false, $callbackName)) { |
| 164 | throw new HTML_QuickForm2_InvalidArgumentException( |
| 165 | 'Callback Rule requires a valid callback, \'' . $callbackName . |
| 166 | '\' was given' |
| 167 | ); |
| 168 | } |
| 169 | return parent::setConfig($config + array('arguments' => array())); |
| 170 | } |
| 171 | } |
| 172 | ?> |
| 173 |