2012-03-22 11:18:57 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* CakeRule.
|
|
|
|
*
|
|
|
|
* 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
|
2012-04-23 11:02:26 +00:00
|
|
|
* @package Cake.Model.Validator
|
|
|
|
* @since CakePHP(tm) v 2.2.0
|
2012-03-22 11:18:57 +00:00
|
|
|
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
|
|
|
|
*/
|
|
|
|
App::uses('ModelValidator', 'Model');
|
|
|
|
App::uses('CakeField', 'Model/Validator');
|
|
|
|
App::uses('Validation', 'Utility');
|
|
|
|
|
|
|
|
/**
|
2012-04-23 11:02:26 +00:00
|
|
|
* CakeRule object.
|
2012-03-22 11:18:57 +00:00
|
|
|
*
|
2012-04-23 11:02:26 +00:00
|
|
|
* @package Cake.Model.Validator
|
2012-03-22 11:18:57 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/data-validation.html
|
|
|
|
*/
|
|
|
|
class CakeRule {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The 'valid' value
|
2012-04-23 11:02:26 +00:00
|
|
|
*
|
2012-03-22 11:18:57 +00:00
|
|
|
* @var mixed
|
|
|
|
*/
|
|
|
|
protected $_valid = true;
|
|
|
|
|
|
|
|
/**
|
2012-04-23 11:02:26 +00:00
|
|
|
* Holds the index under which the Validator was attached
|
|
|
|
*
|
2012-03-22 11:18:57 +00:00
|
|
|
* @var mixed
|
|
|
|
*/
|
|
|
|
protected $_index = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create or Update transaction?
|
2012-04-23 11:02:26 +00:00
|
|
|
*
|
2012-03-22 11:18:57 +00:00
|
|
|
* @var boolean
|
|
|
|
*/
|
2012-04-29 03:55:05 +00:00
|
|
|
protected $_recordExists = false;
|
2012-03-22 11:18:57 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The parsed rule
|
2012-04-23 11:02:26 +00:00
|
|
|
*
|
2012-03-22 11:18:57 +00:00
|
|
|
* @var mixed
|
|
|
|
*/
|
|
|
|
protected $_rule = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The parsed rule parameters
|
2012-04-23 11:02:26 +00:00
|
|
|
*
|
2012-03-22 11:18:57 +00:00
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $_ruleParams = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Holds passed in options
|
2012-04-23 11:02:26 +00:00
|
|
|
*
|
2012-03-22 11:18:57 +00:00
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $_passedOptions = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The 'rule' key
|
2012-04-23 11:02:26 +00:00
|
|
|
*
|
2012-03-22 11:18:57 +00:00
|
|
|
* @var mixed
|
|
|
|
*/
|
|
|
|
public $rule = 'blank';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The 'required' key
|
2012-04-23 11:02:26 +00:00
|
|
|
*
|
2012-03-22 11:18:57 +00:00
|
|
|
* @var mixed
|
|
|
|
*/
|
|
|
|
public $required = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The 'allowEmpty' key
|
2012-04-23 11:02:26 +00:00
|
|
|
*
|
2012-03-22 11:18:57 +00:00
|
|
|
* @var boolean
|
|
|
|
*/
|
|
|
|
public $allowEmpty = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The 'on' key
|
2012-04-23 11:02:26 +00:00
|
|
|
*
|
2012-03-22 11:18:57 +00:00
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $on = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The 'last' key
|
2012-04-23 11:02:26 +00:00
|
|
|
*
|
2012-03-22 11:18:57 +00:00
|
|
|
* @var boolean
|
|
|
|
*/
|
|
|
|
public $last = true;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The 'message' key
|
2012-04-23 11:02:26 +00:00
|
|
|
*
|
2012-03-22 11:18:57 +00:00
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $message = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor
|
2012-04-23 11:02:26 +00:00
|
|
|
*
|
2012-03-22 11:18:57 +00:00
|
|
|
* @param array $validator [optional] The validator properties
|
|
|
|
* @param mixed $index [optional]
|
|
|
|
*/
|
2012-04-29 21:48:33 +00:00
|
|
|
public function __construct($index = null, $validator = array()) {
|
2012-03-22 11:18:57 +00:00
|
|
|
$this->_index = $index;
|
|
|
|
$this->_addValidatorProps($validator);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if the rule is valid
|
2012-04-23 11:02:26 +00:00
|
|
|
*
|
2012-03-22 11:18:57 +00:00
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function isValid() {
|
2012-04-23 11:02:26 +00:00
|
|
|
if (!$this->_valid || (is_string($this->_valid) && !empty($this->_valid))) {
|
2012-03-22 11:18:57 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if the field is required by the 'required' value
|
2012-04-23 11:02:26 +00:00
|
|
|
*
|
2012-03-22 11:18:57 +00:00
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function isRequired() {
|
2012-04-23 11:02:26 +00:00
|
|
|
if (is_bool($this->required)) {
|
2012-03-22 11:18:57 +00:00
|
|
|
return $this->required;
|
|
|
|
}
|
|
|
|
if (in_array($this->required, array('create', 'update'), true)) {
|
2012-04-29 03:55:05 +00:00
|
|
|
if ($this->required === 'create' && !$this->_recordExists || $this->required === 'update' && $this->_recordExists) {
|
2012-03-22 11:18:57 +00:00
|
|
|
$this->required = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->required;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if the field failed the required validation
|
2012-04-23 11:02:26 +00:00
|
|
|
*
|
2012-04-29 03:55:05 +00:00
|
|
|
* @param array $data data to check rule against
|
2012-03-22 11:18:57 +00:00
|
|
|
* @return boolean
|
|
|
|
*/
|
2012-04-29 21:48:33 +00:00
|
|
|
public function checkRequired($field, &$data) {
|
2012-04-29 05:36:24 +00:00
|
|
|
return (
|
2012-04-29 21:48:33 +00:00
|
|
|
(!isset($data[$field]) && $this->isRequired() === true) ||
|
2012-04-29 05:36:24 +00:00
|
|
|
(
|
2012-04-29 21:48:33 +00:00
|
|
|
isset($data[$field]) && (empty($data[$field]) &&
|
|
|
|
!is_numeric($data[$field])) && $this->allowEmpty === false
|
2012-04-29 05:36:24 +00:00
|
|
|
)
|
2012-03-22 11:18:57 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if the allowEmpty key applies
|
2012-04-23 11:02:26 +00:00
|
|
|
*
|
2012-04-29 03:55:05 +00:00
|
|
|
* @param array $data data to check rule against
|
2012-03-22 11:18:57 +00:00
|
|
|
* @return boolean
|
|
|
|
*/
|
2012-04-29 21:48:33 +00:00
|
|
|
public function checkEmpty($field, &$data) {
|
|
|
|
if (empty($data[$field]) && $data[$field] != '0' && $this->allowEmpty === true) {
|
2012-04-29 03:55:05 +00:00
|
|
|
return true;
|
2012-03-22 11:18:57 +00:00
|
|
|
}
|
2012-04-29 03:55:05 +00:00
|
|
|
return false;
|
2012-03-22 11:18:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if the Validation rule can be skipped
|
2012-04-23 11:02:26 +00:00
|
|
|
*
|
|
|
|
* @return boolean True if the ValidationRule can be skipped
|
2012-03-22 11:18:57 +00:00
|
|
|
*/
|
|
|
|
public function skip() {
|
|
|
|
if (!empty($this->on)) {
|
2012-04-29 03:55:05 +00:00
|
|
|
if ($this->on == 'create' && $this->_recordExists || $this->on == 'update' && !$this->_recordExists) {
|
2012-03-22 11:18:57 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if the 'last' key is true
|
2012-04-23 11:02:26 +00:00
|
|
|
*
|
2012-03-22 11:18:57 +00:00
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function isLast() {
|
|
|
|
return (bool) $this->last;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the validation error message
|
2012-04-23 11:02:26 +00:00
|
|
|
*
|
2012-03-22 11:18:57 +00:00
|
|
|
* @return string
|
|
|
|
*/
|
2012-04-29 03:55:05 +00:00
|
|
|
public function getValidationResult() {
|
|
|
|
return $this->_valid;
|
2012-03-22 11:18:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets an array with the rule properties
|
2012-04-23 11:02:26 +00:00
|
|
|
*
|
2012-03-22 11:18:57 +00:00
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getPropertiesArray() {
|
2012-04-29 07:10:08 +00:00
|
|
|
$rule = $this->rule;
|
|
|
|
if (!is_string($rule)) {
|
|
|
|
unset($rule[0]);
|
|
|
|
}
|
2012-03-22 11:18:57 +00:00
|
|
|
return array(
|
2012-04-29 07:10:08 +00:00
|
|
|
'rule' => $rule,
|
2012-03-22 11:18:57 +00:00
|
|
|
'required' => $this->required,
|
|
|
|
'allowEmpty' => $this->allowEmpty,
|
|
|
|
'on' => $this->on,
|
|
|
|
'last' => $this->last,
|
|
|
|
'message' => $this->message
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2012-04-29 03:55:05 +00:00
|
|
|
/**
|
|
|
|
* Sets the recordExists configuration value for this rule,
|
|
|
|
* ir refers to wheter the model record it is validating exists
|
|
|
|
* exists in the collection or not (create or update operation)
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
**/
|
|
|
|
public function isUpdate($exists = false) {
|
|
|
|
$this->_recordExists = $exists;
|
|
|
|
}
|
|
|
|
|
2012-03-22 11:18:57 +00:00
|
|
|
/**
|
|
|
|
* Dispatches the validation rule to the given validator method
|
2012-04-23 11:02:26 +00:00
|
|
|
*
|
2012-03-22 11:18:57 +00:00
|
|
|
* @return boolean True if the rule could be dispatched, false otherwise
|
|
|
|
*/
|
2012-04-29 21:48:33 +00:00
|
|
|
public function dispatchValidation($field, &$data, &$methods) {
|
|
|
|
$this->_parseRule($field, $data);
|
2012-03-22 11:18:57 +00:00
|
|
|
|
|
|
|
$validator = $this->getPropertiesArray();
|
2012-04-29 03:55:05 +00:00
|
|
|
$rule = strtolower($this->_rule);
|
2012-04-29 04:54:35 +00:00
|
|
|
if (isset($methods[$rule])) {
|
2012-03-22 11:18:57 +00:00
|
|
|
$this->_ruleParams[] = array_merge($validator, $this->_passedOptions);
|
2012-04-29 21:48:33 +00:00
|
|
|
$this->_ruleParams[0] = array($field => $this->_ruleParams[0]);
|
2012-04-29 04:54:35 +00:00
|
|
|
$this->_valid = call_user_func_array($methods[$rule], $this->_ruleParams);
|
|
|
|
} elseif (class_exists('Validation') && method_exists('Validation', $this->_rule)) {
|
2012-03-22 11:18:57 +00:00
|
|
|
$this->_valid = call_user_func_array(array('Validation', $this->_rule), $this->_ruleParams);
|
2012-04-29 05:09:43 +00:00
|
|
|
} elseif (is_string($validator['rule'])) {
|
2012-04-29 21:48:33 +00:00
|
|
|
$this->_valid = preg_match($this->_rule, $data[$field]);
|
2012-03-22 11:18:57 +00:00
|
|
|
} elseif (Configure::read('debug') > 0) {
|
2012-04-29 21:48:33 +00:00
|
|
|
trigger_error(__d('cake_dev', 'Could not find validation handler %s for %s', $this->_rule, $field), E_USER_WARNING);
|
2012-03-22 11:18:57 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-04-29 03:55:05 +00:00
|
|
|
public function getOptions($key) {
|
|
|
|
if (!isset($this->_passedOptions[$key])) {
|
|
|
|
return null;
|
2012-03-22 11:18:57 +00:00
|
|
|
}
|
2012-04-29 03:55:05 +00:00
|
|
|
return $this->_passedOptions[$key];
|
|
|
|
}
|
2012-03-22 11:18:57 +00:00
|
|
|
|
2012-04-29 03:55:05 +00:00
|
|
|
public function getName() {
|
|
|
|
return $this->_index;
|
2012-03-22 11:18:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the rule properties from the rule entry in validate
|
2012-04-23 11:02:26 +00:00
|
|
|
*
|
2012-03-22 11:18:57 +00:00
|
|
|
* @param array $validator [optional]
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
protected function _addValidatorProps($validator = array()) {
|
|
|
|
if (!is_array($validator)) {
|
|
|
|
$validator = array('rule' => $validator);
|
|
|
|
}
|
|
|
|
foreach ($validator as $key => $value) {
|
|
|
|
if (isset($value) || !empty($value)) {
|
|
|
|
if (in_array($key, array('rule', 'required', 'allowEmpty', 'on', 'message', 'last'))) {
|
2012-04-29 03:55:05 +00:00
|
|
|
$this->{$key} = $validator[$key];
|
2012-03-22 11:18:57 +00:00
|
|
|
} else {
|
|
|
|
$this->_passedOptions[$key] = $value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parses the rule and sets the rule and ruleParams
|
2012-04-23 11:02:26 +00:00
|
|
|
*
|
2012-03-22 11:18:57 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2012-04-29 21:48:33 +00:00
|
|
|
protected function _parseRule($field, &$data) {
|
2012-03-22 11:18:57 +00:00
|
|
|
if (is_array($this->rule)) {
|
|
|
|
$this->_rule = $this->rule[0];
|
2012-04-29 21:48:33 +00:00
|
|
|
$this->_ruleParams = array_merge(array($data[$field]), array_values(array_slice($this->rule, 1)));
|
2012-03-22 11:18:57 +00:00
|
|
|
} else {
|
|
|
|
$this->_rule = $this->rule;
|
2012-04-29 21:48:33 +00:00
|
|
|
$this->_ruleParams = array($data[$field]);
|
2012-03-22 11:18:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|