Upgrade CakePHP from 2.2.5 to 2.9.5

This commit is contained in:
Brm Ko 2017-02-26 15:29:44 +01:00
parent 5a580df460
commit 235a541597
793 changed files with 60746 additions and 23753 deletions

View file

@ -4,22 +4,22 @@
*
* Provides the Model validation logic.
*
* PHP versions 5
*
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.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 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @package Cake.Model
* @since CakePHP(tm) v 2.2.0
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
App::uses('CakeValidationSet', 'Model/Validator');
App::uses('Hash', 'Utility');
/**
* ModelValidator object encapsulates all methods related to data validations for a model
@ -36,7 +36,7 @@ class ModelValidator implements ArrayAccess, IteratorAggregate, Countable {
/**
* Holds the CakeValidationSet objects array
*
* @var array
* @var CakeValidationSet[]
*/
protected $_fields = array();
@ -48,7 +48,7 @@ class ModelValidator implements ArrayAccess, IteratorAggregate, Countable {
protected $_model = array();
/**
* The validators $validate property, used for checking wheter validation
* The validators $validate property, used for checking whether validation
* rules definition changed in the model and should be refreshed in this class
*
* @var array
@ -90,10 +90,10 @@ class ModelValidator implements ArrayAccess, IteratorAggregate, Countable {
* Returns true if all fields pass validation. Will validate hasAndBelongsToMany associations
* that use the 'with' key as well. Since `Model::_saveMulti` is incapable of exiting a save operation.
*
* Will validate the currently set data. Use `Model::set()` or `Model::create()` to set the active data.
* Will validate the currently set data. Use `Model::set()` or `Model::create()` to set the active data.
*
* @param array $options An optional array of custom options to be made available in the beforeValidate callback
* @return boolean True if there are no errors
* @return bool True if there are no errors
*/
public function validates($options = array()) {
$errors = $this->errors($options);
@ -119,28 +119,27 @@ class ModelValidator implements ArrayAccess, IteratorAggregate, Countable {
* If you do not want this to happen, make a copy of `$data` before passing it
* to this method
*
* @param array $data Record data to validate. This should be an array indexed by association name.
* @param array &$data Record data to validate. This should be an array indexed by association name.
* @param array $options Options to use when validating record data (see above), See also $options of validates().
* @return array|boolean If atomic: True on success, or false on failure.
* @return array|bool If atomic: True on success, or false on failure.
* Otherwise: array similar to the $data array passed, but values are set to true/false
* depending on whether each record validated successfully.
*/
public function validateAssociated(&$data, $options = array()) {
$model = $this->getModel();
$options = array_merge(array('atomic' => true, 'deep' => false), $options);
$options += array('atomic' => true, 'deep' => false);
$model->validationErrors = $validationErrors = $return = array();
$model->create(null);
$return[$model->alias] = true;
if (!($model->set($data) && $model->validates($options))) {
$validationErrors[$model->alias] = $model->validationErrors;
$return[$model->alias] = false;
} else {
$return[$model->alias] = true;
}
$data = $model->data;
if (!empty($options['deep']) && isset($data[$model->alias])) {
$recordData = $data[$model->alias];
unset($data[$model->alias]);
$data = array_merge($data, $recordData);
$data += $recordData;
}
$associations = $model->getAssociated();
@ -156,11 +155,7 @@ class ModelValidator implements ArrayAccess, IteratorAggregate, Countable {
$data[$association] = $model->{$association}->data[$model->{$association}->alias];
}
if (is_array($validates)) {
if (in_array(false, Hash::flatten($validates), true)) {
$validates = false;
} else {
$validates = true;
}
$validates = !in_array(false, Hash::flatten($validates), true);
}
$return[$association] = $validates;
} elseif ($associations[$association] === 'hasMany') {
@ -201,16 +196,15 @@ class ModelValidator implements ArrayAccess, IteratorAggregate, Countable {
* If you do not want this to happen, make a copy of `$data` before passing it
* to this method
*
* @param array $data Record data to validate. This should be a numerically-indexed array
* @param array &$data Record data to validate. This should be a numerically-indexed array
* @param array $options Options to use when validating record data (see above), See also $options of validates().
* @return boolean True on success, or false on failure.
* @return mixed If atomic: True on success, or false on failure.
* Otherwise: array similar to the $data array passed, but values are set to true/false
* depending on whether each record validated successfully.
*/
public function validateMany(&$data, $options = array()) {
$model = $this->getModel();
$options = array_merge(array('atomic' => true, 'deep' => false), $options);
$options += array('atomic' => true, 'deep' => false);
$model->validationErrors = $validationErrors = $return = array();
foreach ($data as $key => &$record) {
if ($options['deep']) {
@ -232,10 +226,7 @@ class ModelValidator implements ArrayAccess, IteratorAggregate, Countable {
if (!$options['atomic']) {
return $return;
}
if (empty($model->validationErrors)) {
return true;
}
return false;
return empty($model->validationErrors);
}
/**
@ -244,6 +235,7 @@ class ModelValidator implements ArrayAccess, IteratorAggregate, Countable {
*
* @param string $options An optional array of custom options to be made available in the beforeValidate callback
* @return array Array of invalid fields
* @triggers Model.afterValidate $model
* @see ModelValidator::validates()
*/
public function errors($options = array()) {
@ -256,7 +248,15 @@ class ModelValidator implements ArrayAccess, IteratorAggregate, Countable {
return $model->validationErrors;
}
$fieldList = isset($options['fieldList']) ? $options['fieldList'] : array();
$fieldList = $model->whitelist;
if (empty($fieldList) && !empty($options['fieldList'])) {
if (!empty($options['fieldList'][$model->alias]) && is_array($options['fieldList'][$model->alias])) {
$fieldList = $options['fieldList'][$model->alias];
} else {
$fieldList = $options['fieldList'];
}
}
$exists = $model->exists();
$methods = $this->getMethods();
$fields = $this->_validationList($fieldList);
@ -319,13 +319,14 @@ class ModelValidator implements ArrayAccess, IteratorAggregate, Countable {
* params are passed then it returns an array with all CakeValidationSet objects for each field
*
* @param string $name [optional] The fieldname to fetch. Defaults to null.
* @return CakeValidationSet|array
* @return CakeValidationSet|array|null
*/
public function getField($name = null) {
$this->_parseRules();
if ($name !== null && !empty($this->_fields[$name])) {
return $this->_fields[$name];
} elseif ($name !== null) {
if ($name !== null) {
if (!empty($this->_fields[$name])) {
return $this->_fields[$name];
}
return null;
}
return $this->_fields;
@ -335,7 +336,7 @@ class ModelValidator implements ArrayAccess, IteratorAggregate, Countable {
* Sets the CakeValidationSet objects from the `Model::$validate` property
* If `Model::$validate` is not set or empty, this method returns false. True otherwise.
*
* @return boolean true if `Model::$validate` was processed, false otherwise
* @return bool true if `Model::$validate` was processed, false otherwise
*/
protected function _parseRules() {
if ($this->_validate === $this->_model->validate) {
@ -362,7 +363,7 @@ class ModelValidator implements ArrayAccess, IteratorAggregate, Countable {
* Sets the I18n domain for validation messages. This method is chainable.
*
* @param string $validationDomain [optional] The validation domain to be used.
* @return ModelValidator
* @return self
*/
public function setValidationDomain($validationDomain = null) {
if (empty($validationDomain)) {
@ -382,36 +383,22 @@ class ModelValidator implements ArrayAccess, IteratorAggregate, Countable {
}
/**
* Processes the Model's whitelist or passed fieldList and returns the list of fields
* to be validated
* Processes the passed fieldList and returns the list of fields to be validated
*
* @param array $fieldList list of fields to be used for validation
* @return array List of validation rules to be applied
* @return CakeValidationSet[] List of validation rules to be applied
*/
protected function _validationList($fieldList = array()) {
$model = $this->getModel();
$whitelist = $model->whitelist;
if (!empty($fieldList)) {
if (!empty($fieldList[$model->alias]) && is_array($fieldList[$model->alias])) {
$whitelist = $fieldList[$model->alias];
} else {
$whitelist = $fieldList;
}
if (empty($fieldList) || Hash::dimensions($fieldList) > 1) {
return $this->_fields;
}
unset($fieldList);
$validateList = array();
if (!empty($whitelist)) {
$this->validationErrors = array();
foreach ((array)$whitelist as $f) {
if (!empty($this->_fields[$f])) {
$validateList[$f] = $this->_fields[$f];
}
$this->validationErrors = array();
foreach ((array)$fieldList as $f) {
if (!empty($this->_fields[$f])) {
$validateList[$f] = $this->_fields[$f];
}
} else {
return $this->_fields;
}
return $validateList;
@ -422,7 +409,7 @@ class ModelValidator implements ArrayAccess, IteratorAggregate, Countable {
* set and data in the data set.
*
* @param array $options Array of options to use on Validation of with models
* @return boolean Failure of validation on with models.
* @return bool Failure of validation on with models.
* @see Model::validates()
*/
protected function _validateWithModels($options) {
@ -444,9 +431,6 @@ class ModelValidator implements ArrayAccess, IteratorAggregate, Countable {
$newData[] = $row[$join];
}
}
if (empty($newData)) {
continue;
}
foreach ($newData as $data) {
$data[$model->hasAndBelongsToMany[$assoc]['foreignKey']] = $model->id;
$model->{$join}->create($data);
@ -459,8 +443,9 @@ class ModelValidator implements ArrayAccess, IteratorAggregate, Countable {
/**
* Propagates beforeValidate event
*
* @param array $options
* @return boolean
* @param array $options Options to pass to callback.
* @return bool
* @triggers Model.beforeValidate $model, array($options)
*/
protected function _triggerBeforeValidate($options = array()) {
$model = $this->getModel();
@ -474,11 +459,11 @@ class ModelValidator implements ArrayAccess, IteratorAggregate, Countable {
}
/**
* Returns wheter a rule set is defined for a field or not
* Returns whether a rule set is defined for a field or not
*
* @param string $field name of the field to check
* @return boolean
**/
* @return bool
*/
public function offsetExists($field) {
$this->_parseRules();
return isset($this->_fields[$field]);
@ -489,7 +474,7 @@ class ModelValidator implements ArrayAccess, IteratorAggregate, Countable {
*
* @param string $field name of the field to check
* @return CakeValidationSet
**/
*/
public function offsetGet($field) {
$this->_parseRules();
return $this->_fields[$field];
@ -501,7 +486,7 @@ class ModelValidator implements ArrayAccess, IteratorAggregate, Countable {
* @param string $field name of the field to set
* @param array|CakeValidationSet $rules set of rules to apply to field
* @return void
**/
*/
public function offsetSet($field, $rules) {
$this->_parseRules();
if (!$rules instanceof CakeValidationSet) {
@ -513,11 +498,11 @@ class ModelValidator implements ArrayAccess, IteratorAggregate, Countable {
}
/**
* Unsets the rulset for a field
* Unsets the rule set for a field
*
* @param string $field name of the field to unset
* @return void
**/
*/
public function offsetUnset($field) {
$this->_parseRules();
unset($this->_fields[$field]);
@ -527,7 +512,7 @@ class ModelValidator implements ArrayAccess, IteratorAggregate, Countable {
* Returns an iterator for each of the fields to be validated
*
* @return ArrayIterator
**/
*/
public function getIterator() {
$this->_parseRules();
return new ArrayIterator($this->_fields);
@ -537,35 +522,35 @@ class ModelValidator implements ArrayAccess, IteratorAggregate, Countable {
* Returns the number of fields having validation rules
*
* @return int
**/
*/
public function count() {
$this->_parseRules();
return count($this->_fields);
}
/**
* Adds a new rule to a field's rule set. If second argumet is an array or instance of
* Adds a new rule to a field's rule set. If second argument is an array or instance of
* CakeValidationSet then rules list for the field will be replaced with second argument and
* third argument will be ignored.
*
* ## Example:
*
* {{{
* ```
* $validator
* ->add('title', 'required', array('rule' => 'notEmpty', 'required' => true))
* ->add('title', 'required', array('rule' => 'notBlank', 'required' => true))
* ->add('user_id', 'valid', array('rule' => 'numeric', 'message' => 'Invalid User'))
*
* $validator->add('password', array(
* 'size' => array('rule' => array('between', 8, 20)),
* 'size' => array('rule' => array('lengthBetween', 8, 20)),
* 'hasSpecialCharacter' => array('rule' => 'validateSpecialchar', 'message' => 'not valid')
* ));
* }}}
* ```
*
* @param string $field The name of the field from wich the rule will be removed
* @param string $field The name of the field where the rule is to be added
* @param string|array|CakeValidationSet $name name of the rule to be added or list of rules for the field
* @param array|CakeValidationRule $rule or list of rules to be added to the field's rule set
* @return ModelValidator this instance
**/
* @return self
*/
public function add($field, $name, $rule = null) {
$this->_parseRules();
if ($name instanceof CakeValidationSet) {
@ -595,16 +580,16 @@ class ModelValidator implements ArrayAccess, IteratorAggregate, Countable {
*
* ## Example:
*
* {{{
* ```
* $validator
* ->remove('title', 'required')
* ->remove('user_id')
* }}}
* ```
*
* @param string $field The name of the field from wich the rule will be removed
* @param string $field The name of the field from which the rule will be removed
* @param string $rule the name of the rule to be removed
* @return ModelValidator this instance
**/
* @return self
*/
public function remove($field, $rule = null) {
$this->_parseRules();
if ($rule === null) {