cakephp2-php8/lib/Cake/Model/Validator/CakeValidationSet.php

371 lines
8.2 KiB
PHP
Raw Normal View History

<?php
/**
* CakeValidationSet.
*
* Provides the Model validation logic.
*
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
2012-04-23 11:02:26 +00:00
* @package Cake.Model.Validator
* @since CakePHP(tm) v 2.2.0
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
2012-09-19 21:10:54 +00:00
App::uses('CakeValidationRule', 'Model/Validator');
/**
* CakeValidationSet object. Holds all validation rules for a field and exposes
* methods to dynamically add or remove validation rules
*
2012-04-23 11:02:26 +00:00
* @package Cake.Model.Validator
* @link http://book.cakephp.org/2.0/en/data-validation.html
*/
class CakeValidationSet implements ArrayAccess, IteratorAggregate, Countable {
/**
2012-05-11 02:09:53 +00:00
* Holds the CakeValidationRule objects
2012-04-23 11:02:26 +00:00
*
* @var array
*/
protected $_rules = array();
/**
2012-05-11 02:09:53 +00:00
* List of methods available for validation
2012-04-23 11:02:26 +00:00
*
* @var array
2013-01-11 14:06:54 +00:00
*/
protected $_methods = array();
/**
* I18n domain for validation messages.
*
* @var string
2013-01-11 14:06:54 +00:00
*/
protected $_validationDomain = null;
/**
2012-05-11 02:09:53 +00:00
* Whether the validation is stopped
2012-04-23 11:02:26 +00:00
*
* @var boolean
*/
public $isStopped = false;
/**
* Holds the fieldname
2012-04-23 11:02:26 +00:00
*
* @var string
*/
public $field = null;
/**
* Holds the original ruleSet
2012-04-23 11:02:26 +00:00
*
* @var array
*/
public $ruleSet = array();
/**
* Constructor
2012-04-23 11:02:26 +00:00
*
2014-05-31 21:36:05 +00:00
* @param string $fieldName The fieldname.
* @param array $ruleSet Rules set.
*/
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 CakeValidationRule($validateProp);
}
$this->ruleSet = $ruleSet;
}
/**
* Sets the list of methods to use for validation
*
2014-05-31 21:36:05 +00:00
* @param array &$methods Methods list
* @return void
2013-01-11 14:06:54 +00:00
*/
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;
}
/**
2012-05-11 02:09:53 +00:00
* Runs all validation rules in this set and returns a list of
* validation errors
2012-04-23 11:02:26 +00:00
*
2012-11-28 22:30:47 +00:00
* @param array $data Data array
* @param boolean $isUpdate Is record being updated or created
* @return array list of validation errors for this field
*/
public function validate($data, $isUpdate = false) {
$this->reset();
$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;
}
2012-04-30 00:26:24 +00:00
$rule->process($this->field, $data, $this->_methods);
}
if ($checkRequired || !$rule->isValid()) {
$errors[] = $this->_processValidationResponse($name, $rule);
if ($rule->isLast()) {
break;
}
}
}
return $errors;
}
/**
2013-03-05 07:05:14 +00:00
* Resets internal state for all validation rules in this set
*
* @return void
2013-01-11 14:06:54 +00:00
*/
public function reset() {
foreach ($this->getRules() as $rule) {
$rule->reset();
}
}
/**
2012-05-11 02:09:53 +00:00
* Gets a rule for a given name if exists
2012-04-23 11:02:26 +00:00
*
2014-05-31 21:36:05 +00:00
* @param string $name Field name.
2012-05-11 02:09:53 +00:00
* @return CakeValidationRule
*/
2012-05-11 02:09:53 +00:00
public function getRule($name) {
if (!empty($this->_rules[$name])) {
return $this->_rules[$name];
}
}
/**
2012-05-11 02:09:53 +00:00
* Returns all rules for this validation set
2012-04-23 11:02:26 +00:00
*
* @return array
*/
public function getRules() {
return $this->_rules;
}
/**
2012-05-11 02:09:53 +00:00
* Sets a CakeValidationRule $rule with a $name
2012-04-23 11:02:26 +00:00
*
2012-05-11 02:09:53 +00:00
* ## Example:
*
* {{{
* $set
* ->setRule('required', array('rule' => 'notEmpty', 'required' => true))
* ->setRule('inRange', array('rule' => array('between', 4, 10))
* }}}
*
* @param string $name The name under which the rule should be set
* @param CakeValidationRule|array $rule The validation rule to be set
* @return $this
*/
2012-05-11 02:09:53 +00:00
public function setRule($name, $rule) {
if (!($rule instanceof CakeValidationRule)) {
$rule = new CakeValidationRule($rule);
}
2012-05-11 02:09:53 +00:00
$this->_rules[$name] = $rule;
return $this;
}
/**
* Removes a validation rule from the set
*
2012-05-11 02:09:53 +00:00
* ## Example:
*
* {{{
* $set
* ->removeRule('required')
* ->removeRule('inRange')
* }}}
*
* @param string $name The name under which the rule should be unset
* @return $this
*/
2012-05-11 02:09:53 +00:00
public function removeRule($name) {
unset($this->_rules[$name]);
return $this;
}
/**
* Sets the rules for a given field
2012-04-23 11:02:26 +00:00
*
2012-05-11 02:09:53 +00:00
* ## Example:
*
* {{{
* $set->setRules(array(
* 'required' => array('rule' => 'notEmpty', 'required' => true),
* 'inRange' => array('rule' => array('between', 4, 10)
* ));
* }}}
*
* @param array $rules The rules to be set
2013-03-05 07:05:14 +00:00
* @param boolean $mergeVars [optional] If true, merges vars instead of replace. Defaults to true.
* @return $this
*/
public function setRules($rules = array(), $mergeVars = true) {
if ($mergeVars === false) {
$this->_rules = array();
}
foreach ($rules as $name => $rule) {
$this->setRule($name, $rule);
}
return $this;
}
/**
* Fetches the correct error message for a failed validation
2012-04-23 11:02:26 +00:00
*
* @param string $name the name of the rule as it was configured
* @param CakeValidationRule $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);
}
$args = $this->_translateArgs($args);
$message = __d($this->_validationDomain, $result, $args);
} elseif (is_string($name)) {
if (is_array($rule->rule)) {
$args = array_slice($rule->rule, 1);
$args = $this->_translateArgs($args);
$message = __d($this->_validationDomain, $name, $args);
} else {
$message = __d($this->_validationDomain, $name);
}
} else {
$message = __d('cake', 'This field cannot be left blank');
}
return $message;
}
/**
* Applies translations to validator arguments.
*
* @param array $args The args to translate
* @return array Translated args.
*/
protected function _translateArgs($args) {
foreach ((array)$args as $k => $arg) {
if (is_string($arg)) {
$args[$k] = __d($this->_validationDomain, $arg);
}
}
return $args;
}
/**
2013-03-05 07:05:14 +00:00
* Returns whether an index exists in the rule set
*
* @param string $index name of the rule
* @return boolean
2013-01-11 14:06:54 +00:00
*/
public function offsetExists($index) {
return isset($this->_rules[$index]);
}
/**
* Returns a rule object by its index
*
* @param string $index name of the rule
* @return CakeValidationRule
2013-01-11 14:06:54 +00:00
*/
public function offsetGet($index) {
return $this->_rules[$index];
}
/**
* Sets or replace a validation rule.
*
* This is a wrapper for ArrayAccess. Use setRule() directly for
* chainable access.
*
2014-05-31 21:36:05 +00:00
* @param string $index Name of the rule.
* @param CakeValidationRule|array $rule Rule to add to $index.
2013-07-05 15:19:22 +00:00
* @return void
2014-05-31 21:36:05 +00:00
* @see http://www.php.net/manual/en/arrayobject.offsetset.php
2013-01-11 14:06:54 +00:00
*/
public function offsetSet($index, $rule) {
$this->setRule($index, $rule);
}
/**
* Unsets a validation rule
*
* @param string $index name of the rule
* @return void
2013-01-11 14:06:54 +00:00
*/
public function offsetUnset($index) {
unset($this->_rules[$index]);
}
/**
* Returns an iterator for each of the rules to be applied
*
* @return ArrayIterator
2013-01-11 14:06:54 +00:00
*/
public function getIterator() {
return new ArrayIterator($this->_rules);
}
/**
* Returns the number of rules in this set
*
* @return integer
2013-01-11 14:06:54 +00:00
*/
public function count() {
return count($this->_rules);
}
}