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