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