📄 ruleregistry.php
字号:
<?php/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: *//** * Registers rule objects and uses them for validation * * PHP versions 4 and 5 * * LICENSE: This source file is subject to version 3.01 of the PHP license * that is available through the world-wide-web at the following URI: * http://www.php.net/license/3_01.txt If you did not receive a copy of * the PHP License and are unable to obtain it through the web, please * send a note to license@php.net so we can mail you a copy immediately. * * @category HTML * @package HTML_QuickForm * @author Adam Daniel <adaniel1@eesus.jnj.com> * @author Bertrand Mansion <bmansion@mamasam.com> * @author Alexey Borzov <avb@php.net> * @copyright 2001-2007 The PHP Group * @license http://www.php.net/license/3_01.txt PHP License 3.01 * @version CVS: $Id: RuleRegistry.php 444 2008-04-11 13:38:22Z johmathe $ * @link http://pear.php.net/package/HTML_QuickForm *//** * Registers rule objects and uses them for validation * * @category HTML * @package HTML_QuickForm * @author Adam Daniel <adaniel1@eesus.jnj.com> * @author Bertrand Mansion <bmansion@mamasam.com> * @author Alexey Borzov <avb@php.net> * @version Release: 3.2.9 * @since 3.2 */class HTML_QuickForm_RuleRegistry{ /** * Array containing references to used rules * @var array * @access private */ var $_rules = array(); /** * Returns a singleton of HTML_QuickForm_RuleRegistry * * Usually, only one RuleRegistry object is needed, this is the reason * why it is recommended to use this method to get the validation object. * * @access public * @static * @return HTML_QuickForm_RuleRegistry */ static function &singleton() { static $obj; if (!isset($obj)) { $obj = new HTML_QuickForm_RuleRegistry(); } return $obj; } // end func singleton /** * Registers a new validation rule * * In order to use a custom rule in your form, you need to register it * first. For regular expressions, one can directly use the 'regex' type * rule in addRule(), this is faster than registering the rule. * * Functions and methods can be registered. Use the 'function' type. * When registering a method, specify the class name as second parameter. * * You can also register an HTML_QuickForm_Rule subclass with its own * validate() method. * * @param string $ruleName Name of validation rule * @param string $type Either: 'regex', 'function' or null * @param string $data1 Name of function, regular expression or * HTML_QuickForm_Rule object class name * @param string $data2 Object parent of above function or HTML_QuickForm_Rule file path * @access public * @return void */ function registerRule($ruleName, $type, $data1, $data2 = null) { $type = strtolower($type); if ($type == 'regex') { // Regular expression $rule =& $this->getRule('regex'); $rule->addData($ruleName, $data1); $GLOBALS['_HTML_QuickForm_registered_rules'][$ruleName] = $GLOBALS['_HTML_QuickForm_registered_rules']['regex']; } elseif ($type == 'function' || $type == 'callback') { // Callback function $rule =& $this->getRule('callback'); $rule->addData($ruleName, $data1, $data2, 'function' == $type); $GLOBALS['_HTML_QuickForm_registered_rules'][$ruleName] = $GLOBALS['_HTML_QuickForm_registered_rules']['callback']; } elseif (is_object($data1)) { // An instance of HTML_QuickForm_Rule $this->_rules[strtolower(get_class($data1))] = $data1; $GLOBALS['_HTML_QuickForm_registered_rules'][$ruleName] = array(strtolower(get_class($data1)), null); } else { // Rule class name $GLOBALS['_HTML_QuickForm_registered_rules'][$ruleName] = array(strtolower($data1), $data2); } } // end func registerRule /** * Returns a reference to the requested rule object * * @param string $ruleName Name of the requested rule * @access public * @return HTML_QuickForm_Rule */ function &getRule($ruleName) { list($class, $path) = $GLOBALS['_HTML_QuickForm_registered_rules'][$ruleName]; if (!isset($this->_rules[$class])) { if (!empty($path)) { include_once($path); } $this->_rules[$class] = new $class(); } $this->_rules[$class]->setName($ruleName); return $this->_rules[$class]; } // end func getRule /** * Performs validation on the given values * * @param string $ruleName Name of the rule to be used * @param mixed $values Can be a scalar or an array of values * to be validated * @param mixed $options Options used by the rule * @param mixed $multiple Whether to validate an array of values altogether * @access public * @return mixed true if no error found, int of valid values (when an array of values is given) or false if error */ function validate($ruleName, $values, $options = null, $multiple = false) { $rule =& $this->getRule($ruleName); if (is_array($values) && !$multiple) { $result = 0; foreach ($values as $value) { if ($rule->validate($value, $options) === true) { $result++; } } return ($result == 0) ? false : $result; } else { return $rule->validate($values, $options); } } // end func validate /** * Returns the validation test in javascript code * * @param array|HTML_QuickForm_element Element(s) the rule applies to * @param string Element name, in case $element is * not an array * @param array Rule data * @access public * @return string JavaScript for the rule */ function getValidationScript(&$element, $elementName, $ruleData) { $reset = (isset($ruleData['reset'])) ? $ruleData['reset'] : false; $rule =& $this->getRule($ruleData['type']);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -