ValidationSet.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  11. * @link https://cakephp.org CakePHP(tm) Project
  12. * @since 2.2.0
  13. * @license https://opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Validation;
  16. use ArrayAccess;
  17. use ArrayIterator;
  18. use Countable;
  19. use IteratorAggregate;
  20. /**
  21. * ValidationSet object. Holds all validation rules for a field and exposes
  22. * methods to dynamically add or remove validation rules
  23. */
  24. class ValidationSet implements ArrayAccess, IteratorAggregate, Countable
  25. {
  26. /**
  27. * Holds the ValidationRule objects
  28. *
  29. * @var \Cake\Validation\ValidationRule[]
  30. */
  31. protected $_rules = [];
  32. /**
  33. * Denotes whether the fieldname key must be present in data array
  34. *
  35. * @var bool|string|callable
  36. */
  37. protected $_validatePresent = false;
  38. /**
  39. * Denotes if a field is allowed to be empty
  40. *
  41. * @var bool|string|callable
  42. */
  43. protected $_allowEmpty = false;
  44. /**
  45. * Sets whether a field is required to be present in data array.
  46. *
  47. * If no argument is passed the currently set `validatePresent` value will be returned.
  48. *
  49. * @param bool|string|callable|null $validatePresent Deprecated since 3.6.0 ValidationSet::isPresenceRequired() is deprecated as a setter
  50. * Use ValidationSet::requirePresence() instead.
  51. * @return bool|string|callable
  52. */
  53. public function isPresenceRequired($validatePresent = null)
  54. {
  55. if ($validatePresent === null) {
  56. return $this->_validatePresent;
  57. }
  58. deprecationWarning(
  59. 'ValidationSet::isPresenceRequired() is deprecated as a setter. ' .
  60. 'Use ValidationSet::requirePresence() instead.'
  61. );
  62. return $this->requirePresence($validatePresent);
  63. }
  64. /**
  65. * Sets whether a field is required to be present in data array.
  66. *
  67. * @param bool|string|callable $validatePresent Valid values are true, false, 'create', 'update' or a callable.
  68. * @return $this
  69. */
  70. public function requirePresence($validatePresent)
  71. {
  72. $this->_validatePresent = $validatePresent;
  73. return $this;
  74. }
  75. /**
  76. * Sets whether a field value is allowed to be empty.
  77. *
  78. * If no argument is passed the currently set `allowEmpty` value will be returned.
  79. *
  80. * @param bool|string|callable|null $allowEmpty Deprecated since 3.6.0 ValidationSet::isEmptyAllowed() is deprecated as a setter.
  81. * Use ValidationSet::allowEmpty() instead.
  82. * @return bool|string|callable
  83. */
  84. public function isEmptyAllowed($allowEmpty = null)
  85. {
  86. if ($allowEmpty === null) {
  87. return $this->_allowEmpty;
  88. }
  89. deprecationWarning(
  90. 'ValidationSet::isEmptyAllowed() is deprecated as a setter. ' .
  91. 'Use ValidationSet::allowEmpty() instead.'
  92. );
  93. return $this->allowEmpty($allowEmpty);
  94. }
  95. /**
  96. * Sets whether a field value is allowed to be empty.
  97. *
  98. * @param bool|string|callable $allowEmpty Valid values are true, false,
  99. * 'create', 'update' or a callable.
  100. * @return $this
  101. */
  102. public function allowEmpty($allowEmpty)
  103. {
  104. $this->_allowEmpty = $allowEmpty;
  105. return $this;
  106. }
  107. /**
  108. * Gets a rule for a given name if exists
  109. *
  110. * @param string $name The name under which the rule is set.
  111. * @return \Cake\Validation\ValidationRule|null
  112. */
  113. public function rule($name)
  114. {
  115. if (!empty($this->_rules[$name])) {
  116. return $this->_rules[$name];
  117. }
  118. return null;
  119. }
  120. /**
  121. * Returns all rules for this validation set
  122. *
  123. * @return \Cake\Validation\ValidationRule[]
  124. */
  125. public function rules()
  126. {
  127. return $this->_rules;
  128. }
  129. /**
  130. * Sets a ValidationRule $rule with a $name
  131. *
  132. * ### Example:
  133. *
  134. * ```
  135. * $set
  136. * ->add('notBlank', ['rule' => 'notBlank'])
  137. * ->add('inRange', ['rule' => ['between', 4, 10])
  138. * ```
  139. *
  140. * @param string $name The name under which the rule should be set
  141. * @param \Cake\Validation\ValidationRule|array $rule The validation rule to be set
  142. * @return $this
  143. */
  144. public function add($name, $rule)
  145. {
  146. if (!($rule instanceof ValidationRule)) {
  147. $rule = new ValidationRule($rule);
  148. }
  149. $this->_rules[$name] = $rule;
  150. return $this;
  151. }
  152. /**
  153. * Removes a validation rule from the set
  154. *
  155. * ### Example:
  156. *
  157. * ```
  158. * $set
  159. * ->remove('notBlank')
  160. * ->remove('inRange')
  161. * ```
  162. *
  163. * @param string $name The name under which the rule should be unset
  164. * @return $this
  165. */
  166. public function remove($name)
  167. {
  168. unset($this->_rules[$name]);
  169. return $this;
  170. }
  171. /**
  172. * Returns whether an index exists in the rule set
  173. *
  174. * @param string $index name of the rule
  175. * @return bool
  176. */
  177. public function offsetExists($index)
  178. {
  179. return isset($this->_rules[$index]);
  180. }
  181. /**
  182. * Returns a rule object by its index
  183. *
  184. * @param string $index name of the rule
  185. * @return \Cake\Validation\ValidationRule
  186. */
  187. public function offsetGet($index)
  188. {
  189. return $this->_rules[$index];
  190. }
  191. /**
  192. * Sets or replace a validation rule
  193. *
  194. * @param string $index name of the rule
  195. * @param \Cake\Validation\ValidationRule|array $rule Rule to add to $index
  196. * @return void
  197. */
  198. public function offsetSet($index, $rule)
  199. {
  200. $this->add($index, $rule);
  201. }
  202. /**
  203. * Unsets a validation rule
  204. *
  205. * @param string $index name of the rule
  206. * @return void
  207. */
  208. public function offsetUnset($index)
  209. {
  210. unset($this->_rules[$index]);
  211. }
  212. /**
  213. * Returns an iterator for each of the rules to be applied
  214. *
  215. * @return \ArrayIterator
  216. */
  217. public function getIterator()
  218. {
  219. return new ArrayIterator($this->_rules);
  220. }
  221. /**
  222. * Returns the number of rules in this set
  223. *
  224. * @return int
  225. */
  226. public function count()
  227. {
  228. return count($this->_rules);
  229. }
  230. }