| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305 |
- <?php
- /**
- * CakeValidationSet.
- *
- * Provides the Model validation logic.
- *
- * PHP versions 5
- *
- * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
- * Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
- *
- * Licensed under The MIT License
- * Redistributions of files must retain the above copyright notice.
- *
- * @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
- * @link http://cakephp.org CakePHP(tm) Project
- * @package Cake.Model.Validator
- * @since CakePHP(tm) v 2.2.0
- * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
- */
- App::uses('ModelValidator', 'Model');
- App::uses('CakeRule', 'Model/Validator');
- /**
- * CakeValidationSet object.
- *
- * @package Cake.Model.Validator
- * @link http://book.cakephp.org/2.0/en/data-validation.html
- */
- class CakeValidationSet implements ArrayAccess, IteratorAggregate, Countable {
- /**
- * Holds the ValidationRule objects
- *
- * @var array
- */
- protected $_rules = array();
- /**
- * Set of methods available for validation
- *
- * @var array
- **/
- protected $_methods = array();
- /**
- * I18n domain for validation messages.
- *
- * @var string
- **/
- protected $_validationDomain = null;
- /**
- * If the validation is stopped
- *
- * @var boolean
- */
- public $isStopped = false;
- /**
- * Holds the fieldname
- *
- * @var string
- */
- public $field = null;
- /**
- * Holds the original ruleSet
- *
- * @var array
- */
- public $ruleSet = array();
- /**
- * Constructor
- *
- * @param string $fieldName The fieldname
- * @param array $ruleset
- */
- public function __construct($fieldName, $ruleSet) {
- $this->field = $fieldName;
- if (!is_array($ruleSet) || (is_array($ruleSet) && isset($ruleSet['rule']))) {
- $ruleSet = array($ruleSet);
- }
- foreach ($ruleSet as $index => $validateProp) {
- $this->_rules[$index] = new CakeRule($validateProp);
- }
- $this->ruleSet = $ruleSet;
- }
- /**
- * Sets the list of methods to use for validation
- *
- * @return void
- **/
- public function setMethods(&$methods) {
- $this->_methods =& $methods;
- }
- /**
- * Sets the I18n domain for validation messages.
- *
- * @param string $validationDomain The validation domain to be used.
- * @return void
- */
- public function setValidationDomain($validationDomain) {
- $this->_validationDomain = $validationDomain;
- }
- /**
- * Validates a ModelField
- *
- * @return array list of validation errors for this field
- */
- public function validate($data, $isUpdate = false) {
- $errors = array();
- foreach ($this->getRules() as $name => $rule) {
- $rule->isUpdate($isUpdate);
- if ($rule->skip()) {
- continue;
- }
- $checkRequired = $rule->checkRequired($this->field, $data);
- if (!$checkRequired && array_key_exists($this->field, $data)) {
- if ($rule->checkEmpty($this->field, $data)) {
- break;
- }
- $rule->process($this->field, $data, $this->_methods);
- }
- if ($checkRequired || !$rule->isValid()) {
- $errors[] = $this->_processValidationResponse($name, $rule);
- if ($rule->isLast()) {
- break;
- }
- }
- }
- return $errors;
- }
- /**
- * Gets a rule for a certain index
- *
- * @param mixed index
- * @return ValidationRule
- */
- public function getRule($index) {
- if (!empty($this->_rules[$index])) {
- return $this->_rules[$index];
- }
- }
- /**
- * Gets all rules for this ModelField
- *
- * @return array
- */
- public function getRules() {
- return $this->_rules;
- }
- /**
- * Sets a ValidationRule $rule for key $key
- *
- * @param mixed $key The key under which the rule should be set
- * @param CakeRule|array $rule The validation rule to be set
- * @return ModelField
- */
- public function setRule($key, $rule) {
- if (!$rule instanceof CakeRule) {
- $rule = new CakeRule($rule);
- }
- $this->_rules[$key] = $rule;
- return $this;
- }
- /**
- * Sets the rules for a given field
- *
- * @param array $rules The rules to be set
- * @param bolean $mergeVars [optional] If true, merges vars instead of replace. Defaults to true.
- * @return ModelField
- */
- public function setRules($rules = array(), $mergeVars = true) {
- if ($mergeVars === false) {
- $this->_rules = $rules;
- } else {
- $this->_rules = array_merge($this->_rules, $rules);
- }
- return $this;
- }
- /**
- * Fetches the correct error message for a failed validation
- *
- * @param string $name the name of the rule as it was configured
- * @param CakeRule $rule the object containing validation information
- * @return string
- */
- protected function _processValidationResponse($name, $rule) {
- $message = $rule->getValidationResult();
- if (is_string($message)) {
- return $message;
- }
- $message = $rule->message;
- if ($message !== null) {
- $args = null;
- if (is_array($message)) {
- $result = $message[0];
- $args = array_slice($message, 1);
- } else {
- $result = $message;
- }
- if (is_array($rule->rule) && $args === null) {
- $args = array_slice($rule->rule, 1);
- }
- if (!empty($args)) {
- foreach ($args as $k => $arg) {
- $args[$k] = __d($this->_validationDomain, $arg);
- }
- }
- $message = __d($this->_validationDomain, $result, $args);
- } elseif (is_string($name)) {
- if (is_array($rule->rule)) {
- $args = array_slice($rule->rule, 1);
- if (!empty($args)) {
- foreach ($args as $k => $arg) {
- $args[$k] = __d($this->_validationDomain, $arg);
- }
- }
- $message = __d($this->_validationDomain, $name, $args);
- } else {
- $message = __d($this->_validationDomain, $name);
- }
- } else {
- $message = __d('cake_dev', 'This field cannot be left blank');
- }
- return $message;
- }
- /**
- * Returns wheter an index exists in the rule set
- *
- * @param string $index name of the rule
- * @return boolean
- **/
- public function offsetExists($index) {
- return isset($this->_rules[$index]);
- }
- /**
- * Returns a rule object by its index
- *
- * @param string $index name of the rule
- * @return CakeRule
- **/
- public function offsetGet($index) {
- return $this->_rules[$index];
- }
- /**
- * Sets or replace a validation rule
- *
- * @param string $index name of the rule
- * @param CakeRule|array rule to add to $index
- **/
- public function offsetSet($index, $rule) {
- $this->setRule($index, $rule);
- }
- /**
- * Unsets a validation rule
- *
- * @param string $index name of the rule
- * @return void
- **/
- public function offsetUnset($index) {
- unset($this->_rules[$index]);
- }
- /**
- * Returns an iterator for each of the rules to be applied
- *
- * @return ArrayIterator
- **/
- public function getIterator() {
- return new ArrayIterator($this->_rules);
- }
- /**
- * Returns the number of rules in this set
- *
- * @return int
- **/
- public function count() {
- return count($this->_rules);
- }
- }
|