PageRenderTime 41ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/Cake/Model/Validator/CakeValidationRule.php

https://bitbucket.org/ManiAdil/jardinorient
PHP | 352 lines | 138 code | 34 blank | 180 comment | 37 complexity | 637610c62a5219e27c598f45bd93a810 MD5 | raw file
  1. <?php
  2. /**
  3. * CakeValidationRule.
  4. *
  5. * Provides the Model validation logic.
  6. *
  7. * PHP versions 5
  8. *
  9. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  10. * Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. *
  12. * Licensed under The MIT License
  13. * Redistributions of files must retain the above copyright notice.
  14. *
  15. * @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  16. * @link http://cakephp.org CakePHP(tm) Project
  17. * @package Cake.Model.Validator
  18. * @since CakePHP(tm) v 2.2.0
  19. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  20. */
  21. App::uses('Validation', 'Utility');
  22. /**
  23. * CakeValidationRule object. Represents a validation method, error message and
  24. * rules for applying such method to a field.
  25. *
  26. * @package Cake.Model.Validator
  27. * @link http://book.cakephp.org/2.0/en/data-validation.html
  28. */
  29. class CakeValidationRule {
  30. /**
  31. * Whether the field passed this validation rule
  32. *
  33. * @var mixed
  34. */
  35. protected $_valid = true;
  36. /**
  37. * Holds whether the record being validated exists in datasource or not
  38. *
  39. * @var boolean
  40. */
  41. protected $_recordExists = false;
  42. /**
  43. * Validation method
  44. *
  45. * @var mixed
  46. */
  47. protected $_rule = null;
  48. /**
  49. * Validation method arguments
  50. *
  51. * @var array
  52. */
  53. protected $_ruleParams = array();
  54. /**
  55. * Holds passed in options
  56. *
  57. * @var array
  58. */
  59. protected $_passedOptions = array();
  60. /**
  61. * The 'rule' key
  62. *
  63. * @var mixed
  64. */
  65. public $rule = 'blank';
  66. /**
  67. * The 'required' key
  68. *
  69. * @var mixed
  70. */
  71. public $required = null;
  72. /**
  73. * The 'allowEmpty' key
  74. *
  75. * @var boolean
  76. */
  77. public $allowEmpty = null;
  78. /**
  79. * The 'on' key
  80. *
  81. * @var string
  82. */
  83. public $on = null;
  84. /**
  85. * The 'last' key
  86. *
  87. * @var boolean
  88. */
  89. public $last = true;
  90. /**
  91. * The 'message' key
  92. *
  93. * @var string
  94. */
  95. public $message = null;
  96. /**
  97. * Constructor
  98. *
  99. * @param array $validator [optional] The validator properties
  100. */
  101. public function __construct($validator = array()) {
  102. $this->_addValidatorProps($validator);
  103. }
  104. /**
  105. * Checks if the rule is valid
  106. *
  107. * @return boolean
  108. */
  109. public function isValid() {
  110. if (!$this->_valid || (is_string($this->_valid) && !empty($this->_valid))) {
  111. return false;
  112. }
  113. return true;
  114. }
  115. /**
  116. * Returns whether the field can be left blank according to this rule
  117. *
  118. * @return boolean
  119. */
  120. public function isEmptyAllowed() {
  121. return $this->skip() || $this->allowEmpty === true;
  122. }
  123. /**
  124. * Checks if the field is required according to the `required` property
  125. *
  126. * @return boolean
  127. */
  128. public function isRequired() {
  129. if (in_array($this->required, array('create', 'update'), true)) {
  130. if ($this->required === 'create' && !$this->isUpdate() || $this->required === 'update' && $this->isUpdate()) {
  131. return true;
  132. } else {
  133. return false;
  134. }
  135. }
  136. return $this->required;
  137. }
  138. /**
  139. * Checks whether the field failed the `field should be present` validation
  140. *
  141. * @param string $field Field name
  142. * @param array $data Data to check rule against
  143. * @return boolean
  144. */
  145. public function checkRequired($field, &$data) {
  146. return (
  147. (!array_key_exists($field, $data) && $this->isRequired() === true) ||
  148. (
  149. array_key_exists($field, $data) && (empty($data[$field]) &&
  150. !is_numeric($data[$field])) && $this->allowEmpty === false
  151. )
  152. );
  153. }
  154. /**
  155. * Checks if the allowEmpty key applies
  156. *
  157. * @param string $field Field name
  158. * @param array $data data to check rule against
  159. * @return boolean
  160. */
  161. public function checkEmpty($field, &$data) {
  162. if (empty($data[$field]) && $data[$field] != '0' && $this->allowEmpty === true) {
  163. return true;
  164. }
  165. return false;
  166. }
  167. /**
  168. * Checks if the validation rule should be skipped
  169. *
  170. * @return boolean True if the ValidationRule can be skipped
  171. */
  172. public function skip() {
  173. if (!empty($this->on)) {
  174. if ($this->on == 'create' && $this->isUpdate() || $this->on == 'update' && !$this->isUpdate()) {
  175. return true;
  176. }
  177. }
  178. return false;
  179. }
  180. /**
  181. * Returns whethere this rule should break validation process for associated field
  182. * after it fails
  183. *
  184. * @return boolean
  185. */
  186. public function isLast() {
  187. return (bool)$this->last;
  188. }
  189. /**
  190. * Gets the validation error message
  191. *
  192. * @return string
  193. */
  194. public function getValidationResult() {
  195. return $this->_valid;
  196. }
  197. /**
  198. * Gets an array with the rule properties
  199. *
  200. * @return array
  201. */
  202. protected function _getPropertiesArray() {
  203. $rule = $this->rule;
  204. if (!is_string($rule)) {
  205. unset($rule[0]);
  206. }
  207. return array(
  208. 'rule' => $rule,
  209. 'required' => $this->required,
  210. 'allowEmpty' => $this->allowEmpty,
  211. 'on' => $this->on,
  212. 'last' => $this->last,
  213. 'message' => $this->message
  214. );
  215. }
  216. /**
  217. * Sets the recordExists configuration value for this rule,
  218. * ir refers to wheter the model record it is validating exists
  219. * exists in the collection or not (create or update operation)
  220. *
  221. * If called with no parameters it will return whether this rule
  222. * is configured for update operations or not.
  223. *
  224. * @param boolean $exists Boolean to indicate if records exists
  225. * @return boolean
  226. */
  227. public function isUpdate($exists = null) {
  228. if ($exists === null) {
  229. return $this->_recordExists;
  230. }
  231. return $this->_recordExists = $exists;
  232. }
  233. /**
  234. * Dispatches the validation rule to the given validator method
  235. *
  236. * @param string $field Field name
  237. * @param array $data Data array
  238. * @param array $methods Methods list
  239. * @return boolean True if the rule could be dispatched, false otherwise
  240. */
  241. public function process($field, &$data, &$methods) {
  242. $this->_valid = true;
  243. $this->_parseRule($field, $data);
  244. $validator = $this->_getPropertiesArray();
  245. $rule = strtolower($this->_rule);
  246. if (isset($methods[$rule])) {
  247. $this->_ruleParams[] = array_merge($validator, $this->_passedOptions);
  248. $this->_ruleParams[0] = array($field => $this->_ruleParams[0]);
  249. $this->_valid = call_user_func_array($methods[$rule], $this->_ruleParams);
  250. } elseif (class_exists('Validation') && method_exists('Validation', $this->_rule)) {
  251. $this->_valid = call_user_func_array(array('Validation', $this->_rule), $this->_ruleParams);
  252. } elseif (is_string($validator['rule'])) {
  253. $this->_valid = preg_match($this->_rule, $data[$field]);
  254. } else {
  255. trigger_error(__d('cake_dev', 'Could not find validation handler %s for %s', $this->_rule, $field), E_USER_WARNING);
  256. return false;
  257. }
  258. return true;
  259. }
  260. /**
  261. * Resets interal state for this rule, by default it will become valid
  262. * and it will set isUpdate() to false
  263. *
  264. * @return void
  265. */
  266. public function reset() {
  267. $this->_valid = true;
  268. $this->_recordExists = false;
  269. }
  270. /**
  271. * Returns passed options for this rule
  272. *
  273. * @param string|integer $key Array index
  274. * @return array
  275. */
  276. public function getOptions($key) {
  277. if (!isset($this->_passedOptions[$key])) {
  278. return null;
  279. }
  280. return $this->_passedOptions[$key];
  281. }
  282. /**
  283. * Sets the rule properties from the rule entry in validate
  284. *
  285. * @param array $validator [optional]
  286. * @return void
  287. */
  288. protected function _addValidatorProps($validator = array()) {
  289. if (!is_array($validator)) {
  290. $validator = array('rule' => $validator);
  291. }
  292. foreach ($validator as $key => $value) {
  293. if (isset($value) || !empty($value)) {
  294. if (in_array($key, array('rule', 'required', 'allowEmpty', 'on', 'message', 'last'))) {
  295. $this->{$key} = $validator[$key];
  296. } else {
  297. $this->_passedOptions[$key] = $value;
  298. }
  299. }
  300. }
  301. }
  302. /**
  303. * Parses the rule and sets the rule and ruleParams
  304. *
  305. * @param string $field Field name
  306. * @param array $data Data array
  307. * @return void
  308. */
  309. protected function _parseRule($field, &$data) {
  310. if (is_array($this->rule)) {
  311. $this->_rule = $this->rule[0];
  312. $this->_ruleParams = array_merge(array($data[$field]), array_values(array_slice($this->rule, 1)));
  313. } else {
  314. $this->_rule = $this->rule;
  315. $this->_ruleParams = array($data[$field]);
  316. }
  317. }
  318. }