⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 input.php

📁 Bug tracker, and reporter.
💻 PHP
📖 第 1 页 / 共 2 页
字号:
    public function setData(array $data)    {        $this->_data = $data;        /**         * Reset to initial state         */        $this->_validFields = array();        $this->_invalidMessages = array();        $this->_invalidErrors = array();        $this->_missingFields = array();        $this->_unknownFields = array();        $this->_processed = false;        return $this;    }    /**     * @param mixed $escapeFilter     * @return Zend_Filter_Interface     */    public function setDefaultEscapeFilter($escapeFilter)    {        if (is_string($escapeFilter) || is_array($escapeFilter)) {            $escapeFilter = $this->_getFilter($escapeFilter);        }        if (!$escapeFilter instanceof Zend_Filter_Interface) {            require_once 'Zend/Filter/Exception.php';            throw new Zend_Filter_Exception('Escape filter specified does not implement Zend_Filter_Interface');        }        $this->_defaultEscapeFilter = $escapeFilter;        return $escapeFilter;    }    /**     * @param array $options     * @return Zend_Filter_Input     * @throws Zend_Filter_Exception if an unknown option is given     */    public function setOptions(array $options)    {        foreach ($options as $option => $value) {            switch ($option) {                case self::ESCAPE_FILTER:                    $this->setDefaultEscapeFilter($value);                    break;                case self::INPUT_NAMESPACE:                    $this->addNamespace($value);                    break;                case self::ALLOW_EMPTY:                case self::BREAK_CHAIN:                case self::MISSING_MESSAGE:                case self::NOT_EMPTY_MESSAGE:                case self::PRESENCE:                    $this->_defaults[$option] = $value;                    break;                default:                    require_once 'Zend/Filter/Exception.php';                    throw new Zend_Filter_Exception("Unknown option '$option'");                    break;            }        }        return $this;    }    /*     * Protected methods     */    /**     * @return void     */    protected function _filter()    {        foreach ($this->_filterRules as $ruleName => &$filterRule) {            /**             * Make sure we have an array representing this filter chain.             * Don't typecast to (array) because it might be a Zend_Filter object             */            if (!is_array($filterRule)) {                $filterRule = array($filterRule);            }            /**             * Filters are indexed by integer, metacommands are indexed by string.             * Pick out the filters.             */            $filterList = array();            foreach ($filterRule as $key => $value) {                if (is_int($key)) {                    $filterList[] = $value;                }            }            /**             * Use defaults for filter metacommands.             */            $filterRule[self::RULE] = $ruleName;            if (!isset($filterRule[self::FIELDS])) {                $filterRule[self::FIELDS] = $ruleName;            }            /**             * Load all the filter classes and add them to the chain.             */            if (!isset($filterRule[self::FILTER_CHAIN])) {                $filterRule[self::FILTER_CHAIN] = new Zend_Filter();                foreach ($filterList as $filter) {                    if (is_string($filter) || is_array($filter)) {                        $filter = $this->_getFilter($filter);                    }                    $filterRule[self::FILTER_CHAIN]->addFilter($filter);                }            }            /**             * If the ruleName is the special wildcard rule,             * then apply the filter chain to all input data.             * Else just process the field named by the rule.             */            if ($ruleName == self::RULE_WILDCARD) {                foreach (array_keys($this->_data) as $field)  {                    $this->_filterRule(array_merge($filterRule, array(self::FIELDS => $field)));                }            } else {                $this->_filterRule($filterRule);            }        }    }    /**     * @param array $filterRule     * @return void     */    protected function _filterRule(array $filterRule)    {        $field = $filterRule[self::FIELDS];        if (!array_key_exists($field, $this->_data)) {            return;        }        if (is_array($this->_data[$field])) {            foreach ($this->_data[$field] as $key => $value) {                $this->_data[$field][$key] = $filterRule[self::FILTER_CHAIN]->filter($value);            }        } else {            $this->_data[$field] =                $filterRule[self::FILTER_CHAIN]->filter($this->_data[$field]);        }    }    /**     * @return Zend_Filter_Interface     */    protected function _getDefaultEscapeFilter()    {        if ($this->_defaultEscapeFilter !== null) {            return $this->_defaultEscapeFilter;        }        return $this->setDefaultEscapeFilter($this->_defaults[self::ESCAPE_FILTER]);    }    /**     * @param string $rule     * @param string $field     * @return string     */    protected function _getMissingMessage($rule, $field)    {        $message = $this->_defaults[self::MISSING_MESSAGE];        $message = str_replace('%rule%', $rule, $message);        $message = str_replace('%field%', $field, $message);        return $message;    }    /**     * @return string     */    protected function _getNotEmptyMessage($rule, $field)    {        $message = $this->_defaults[self::NOT_EMPTY_MESSAGE];        $message = str_replace('%rule%', $rule, $message);        $message = str_replace('%field%', $field, $message);        return $message;    }    /**     * @return void     */    protected function _process()    {        if ($this->_processed === false) {            $this->_filter();            $this->_validate();            $this->_processed = true;        }    }    /**     * @return void     */    protected function _validate()    {        /**         * Special case: if there are no validators, treat all fields as valid.         */        if (!$this->_validatorRules) {            $this->_validFields = $this->_data;            $this->_data = array();            return;        }        foreach ($this->_validatorRules as $ruleName => &$validatorRule) {            /**             * Make sure we have an array representing this validator chain.             * Don't typecast to (array) because it might be a Zend_Validate object             */            if (!is_array($validatorRule)) {                $validatorRule = array($validatorRule);            }            /**             * Validators are indexed by integer, metacommands are indexed by string.             * Pick out the validators.             */            $validatorList = array();            foreach ($validatorRule as $key => $value) {                if (is_int($key)) {                    $validatorList[] = $value;                }            }            /**             * Use defaults for validation metacommands.             */            $validatorRule[self::RULE] = $ruleName;            if (!isset($validatorRule[self::FIELDS])) {                $validatorRule[self::FIELDS] = $ruleName;            }            if (!isset($validatorRule[self::BREAK_CHAIN])) {                $validatorRule[self::BREAK_CHAIN] = $this->_defaults[self::BREAK_CHAIN];            }            if (!isset($validatorRule[self::PRESENCE])) {                $validatorRule[self::PRESENCE] = $this->_defaults[self::PRESENCE];            }            if (!isset($validatorRule[self::ALLOW_EMPTY])) {                $validatorRule[self::ALLOW_EMPTY] = $this->_defaults[self::ALLOW_EMPTY];            }            if (!isset($validatorRule[self::MESSAGES])) {                $validatorRule[self::MESSAGES] = array();            } else if (!is_array($validatorRule[self::MESSAGES])) {                $validatorRule[self::MESSAGES] = array($validatorRule[self::MESSAGES]);            } else if (!array_intersect_key($validatorList, $validatorRule[self::MESSAGES])) {                $validatorRule[self::MESSAGES] = array($validatorRule[self::MESSAGES]);            }            /**             * Load all the validator classes and add them to the chain.             */            if (!isset($validatorRule[self::VALIDATOR_CHAIN])) {                $validatorRule[self::VALIDATOR_CHAIN] = new Zend_Validate();                $i = 0;                foreach ($validatorList as $validator) {                    if (is_string($validator) || is_array($validator)) {                        $validator = $this->_getValidator($validator);                    }                    if (isset($validatorRule[self::MESSAGES][$i])) {                        $value = $validatorRule[self::MESSAGES][$i];                        if (is_array($value)) {                            $validator->setMessages($value);                        } else {                            $validator->setMessage($value);                        }                    }                    $validatorRule[self::VALIDATOR_CHAIN]->addValidator($validator, $validatorRule[self::BREAK_CHAIN]);                    ++$i;                }                $validatorRule[self::VALIDATOR_CHAIN_COUNT] = $i;            }            /**             * If the ruleName is the special wildcard rule,             * then apply the validator chain to all input data.             * Else just process the field named by the rule.             */            if ($ruleName == self::RULE_WILDCARD) {                foreach (array_keys($this->_data) as $field)  {                    $this->_validateRule(array_merge($validatorRule, array(self::FIELDS => $field)));                }            } else {                $this->_validateRule($validatorRule);            }        }        /**         * Unset fields in $_data that have been added to other arrays.         * We have to wait until all rules have been processed because         * a given field may be referenced by multiple rules.         */        foreach (array_merge(array_keys($this->_missingFields), array_keys($this->_invalidMessages)) as $rule) {            foreach ((array) $this->_validatorRules[$rule][self::FIELDS] as $field) {                unset($this->_data[$field]);            }        }        foreach ($this->_validFields as $field => $value) {            unset($this->_data[$field]);        }        /**         * Anything left over in $_data is an unknown field.         */        $this->_unknownFields = $this->_data;    }    /**     * @param array $validatorRule     * @return void     */    protected function _validateRule(array $validatorRule)    {        /**         * Get one or more data values from input, and check for missing fields.         * Apply defaults if fields are missing.         */        $data = array();        foreach ((array) $validatorRule[self::FIELDS] as $field) {            if (array_key_exists($field, $this->_data)) {                $data[$field] = $this->_data[$field];            } else            if (array_key_exists(self::DEFAULT_VALUE, $validatorRule)) {                if (is_array($validatorRule[self::DEFAULT_VALUE])) {                    $key = array_search($field, (array) $validatorRule[self::FIELDS]);                    if (array_key_exists($key, $validatorRule[self::DEFAULT_VALUE])) {                        $data[$field] = $validatorRule[self::DEFAULT_VALUE][$key];                    }                } else {                    $data[$field] = $validatorRule[self::DEFAULT_VALUE];                }            } else            if ($validatorRule[self::PRESENCE] == self::PRESENCE_REQUIRED) {                $this->_missingFields[$validatorRule[self::RULE]][] =                    $this->_getMissingMessage($validatorRule[self::RULE], $field);            }        }        /**         * If any required fields are missing, break the loop.         */        if (isset($this->_missingFields[$validatorRule[self::RULE]]) && count($this->_missingFields[$validatorRule[self::RULE]]) > 0) {            return;        }        /**         * Evaluate the inputs against the validator chain.         */        if (count((array) $validatorRule[self::FIELDS]) > 1) {            if (!$validatorRule[self::VALIDATOR_CHAIN]->isValid($data)) {                $this->_invalidMessages[$validatorRule[self::RULE]] = $validatorRule[self::VALIDATOR_CHAIN]->getMessages();                $this->_invalidErrors[$validatorRule[self::RULE]] = $validatorRule[self::VALIDATOR_CHAIN]->getErrors();                return;            }        } else {            $failed = false;            foreach ($data as $fieldKey => $field) {                if (!is_array($field)) {                    $field = array($field);                }                foreach ($field as $value) {                    if (empty($value)) {                        if ($validatorRule[self::ALLOW_EMPTY] == true) {                            continue;                        }                        if ($validatorRule[self::VALIDATOR_CHAIN_COUNT] == 0) {                            $notEmptyValidator = $this->_getValidator('NotEmpty');                            $notEmptyValidator->setMessage($this->_getNotEmptyMessage($validatorRule[self::RULE], $fieldKey));                            $validatorRule[self::VALIDATOR_CHAIN]->addValidator($notEmptyValidator);                        }                    }                    if (!$validatorRule[self::VALIDATOR_CHAIN]->isValid($value)) {                        $this->_invalidMessages[$validatorRule[self::RULE]] =                            $validatorRule[self::VALIDATOR_CHAIN]->getMessages();                        $this->_invalidErrors[$validatorRule[self::RULE]] =                            $validatorRule[self::VALIDATOR_CHAIN]->getErrors();                        unset($this->_validFields[$fieldKey]);                        $failed = true;                        if ($validatorRule[self::BREAK_CHAIN]) {                            return;                        }                    }                }            }            if ($failed) {                return;            }        }        /**         * If we got this far, the inputs for this rule pass validation.         */        foreach ((array) $validatorRule[self::FIELDS] as $field) {            if (array_key_exists($field, $data)) {                $this->_validFields[$field] = $data[$field];            }        }    }    /**     * @param mixed $classBaseName     * @return Zend_Filter_Interface     */    protected function _getFilter($classBaseName)    {        return $this->_getFilterOrValidator(self::FILTER, $classBaseName);    }    /**     * @param mixed $classBaseName     * @return Zend_Validate_Interface     */    protected function _getValidator($classBaseName)    {        return $this->_getFilterOrValidator(self::VALIDATE, $classBaseName);    }    /**     * @param string $type     * @param mixed $classBaseName     * @return Zend_Filter_Interface|Zend_Validate_Interface     * @throws Zend_Filter_Exception     */    protected function _getFilterOrValidator($type, $classBaseName)    {        $args = array();        if (is_array($classBaseName)) {            $args = $classBaseName;            $classBaseName = array_shift($args);        }        $interfaceName = 'Zend_' . ucfirst($type) . '_Interface';        $className = $this->getPluginLoader($type)->load(ucfirst($classBaseName));        $class = new ReflectionClass($className);        if (!$class->implementsInterface($interfaceName)) {            require_once 'Zend/Filter/Exception.php';            throw new Zend_Filter_Exception("Class based on basename '$classBaseName' must implement the '$interfaceName' interface");        }        if ($class->hasMethod('__construct')) {            $object = $class->newInstanceArgs($args);        } else {            $object = $class->newInstance();        }        return $object;    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -