/
CakeValidationSet.php
316 lines (284 loc) · 6.86 KB
/
CakeValidationSet.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
<?php
/**
* CakeValidationSet.
*
* 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
* @package Cake.Model.Validator
* @since CakePHP(tm) v 2.2.0
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
App::uses('ModelValidator', 'Model');
App::uses('CakeValidationRule', 'Model/Validator');
/**
* CakeValidationSet object.
*
* @package Cake.Model.Validator
* @link http://book.cakephp.org/2.0/en/data-validation.html
*/
class CakeValidationSet implements ArrayAccess, IteratorAggregate, Countable {
/**
* Holds the ValidationRule objects
*
* @var array
*/
protected $_rules = array();
/**
* Set of methods available for validation
*
* @var array
**/
protected $_methods = array();
/**
* I18n domain for validation messages.
*
* @var string
**/
protected $_validationDomain = null;
/**
* If the validation is stopped
*
* @var boolean
*/
public $isStopped = false;
/**
* Holds the fieldname
*
* @var string
*/
public $field = null;
/**
* Holds the original ruleSet
*
* @var array
*/
public $ruleSet = array();
/**
* Constructor
*
* @param string $fieldName The fieldname
* @param array $ruleset
*/
public function __construct($fieldName, $ruleSet) {
$this->field = $fieldName;
if (!is_array($ruleSet) || (is_array($ruleSet) && isset($ruleSet['rule']))) {
$ruleSet = array($ruleSet);
}
foreach ($ruleSet as $index => $validateProp) {
$this->_rules[$index] = new CakeValidationRule($validateProp);
}
$this->ruleSet = $ruleSet;
}
/**
* Sets the list of methods to use for validation
*
* @return void
**/
public function setMethods(&$methods) {
$this->_methods =& $methods;
}
/**
* Sets the I18n domain for validation messages.
*
* @param string $validationDomain The validation domain to be used.
* @return void
*/
public function setValidationDomain($validationDomain) {
$this->_validationDomain = $validationDomain;
}
/**
* Validates a ModelField
*
* @return array list of validation errors for this field
*/
public function validate($data, $isUpdate = false) {
$errors = array();
foreach ($this->getRules() as $name => $rule) {
$rule->isUpdate($isUpdate);
if ($rule->skip()) {
continue;
}
$checkRequired = $rule->checkRequired($this->field, $data);
if (!$checkRequired && array_key_exists($this->field, $data)) {
if ($rule->checkEmpty($this->field, $data)) {
break;
}
$rule->process($this->field, $data, $this->_methods);
}
if ($checkRequired || !$rule->isValid()) {
$errors[] = $this->_processValidationResponse($name, $rule);
if ($rule->isLast()) {
break;
}
}
}
return $errors;
}
/**
* Gets a rule for a certain index
*
* @param mixed index
* @return ValidationRule
*/
public function getRule($index) {
if (!empty($this->_rules[$index])) {
return $this->_rules[$index];
}
}
/**
* Gets all rules for this ModelField
*
* @return array
*/
public function getRules() {
return $this->_rules;
}
/**
* Sets a ValidationRule $rule for key $key
*
* @param mixed $key The key under which the rule should be set
* @param CakeValidationRule|array $rule The validation rule to be set
* @return CakeValidationSet this instance
*/
public function setRule($key, $rule) {
if (!$rule instanceof CakeValidationRule) {
$rule = new CakeValidationRule($rule);
}
$this->_rules[$key] = $rule;
return $this;
}
/**
* Removes a validation rule from the set
*
* @param mixed $key The key under which the rule should be unset
* @return CakeValidationSet this instance
*/
public function removeRule($key) {
unset($this->_rules[$key]);
return $this;
}
/**
* Sets the rules for a given field
*
* @param array $rules The rules to be set
* @param bolean $mergeVars [optional] If true, merges vars instead of replace. Defaults to true.
* @return ModelField
*/
public function setRules($rules = array(), $mergeVars = true) {
if ($mergeVars === false) {
$this->_rules = $rules;
} else {
$this->_rules = array_merge($this->_rules, $rules);
}
return $this;
}
/**
* Fetches the correct error message for a failed validation
*
* @param string $name the name of the rule as it was configured
* @param CakeValidationRule $rule the object containing validation information
* @return string
*/
protected function _processValidationResponse($name, $rule) {
$message = $rule->getValidationResult();
if (is_string($message)) {
return $message;
}
$message = $rule->message;
if ($message !== null) {
$args = null;
if (is_array($message)) {
$result = $message[0];
$args = array_slice($message, 1);
} else {
$result = $message;
}
if (is_array($rule->rule) && $args === null) {
$args = array_slice($rule->rule, 1);
}
if (!empty($args)) {
foreach ($args as $k => $arg) {
$args[$k] = __d($this->_validationDomain, $arg);
}
}
$message = __d($this->_validationDomain, $result, $args);
} elseif (is_string($name)) {
if (is_array($rule->rule)) {
$args = array_slice($rule->rule, 1);
if (!empty($args)) {
foreach ($args as $k => $arg) {
$args[$k] = __d($this->_validationDomain, $arg);
}
}
$message = __d($this->_validationDomain, $name, $args);
} else {
$message = __d($this->_validationDomain, $name);
}
} else {
$message = __d('cake_dev', 'This field cannot be left blank');
}
return $message;
}
/**
* Returns wheter an index exists in the rule set
*
* @param string $index name of the rule
* @return boolean
**/
public function offsetExists($index) {
return isset($this->_rules[$index]);
}
/**
* Returns a rule object by its index
*
* @param string $index name of the rule
* @return CakeValidationRule
**/
public function offsetGet($index) {
return $this->_rules[$index];
}
/**
* Sets or replace a validation rule
*
* @param string $index name of the rule
* @param CakeValidationRule|array rule to add to $index
**/
public function offsetSet($index, $rule) {
$this->setRule($index, $rule);
}
/**
* Unsets a validation rule
*
* @param string $index name of the rule
* @return void
**/
public function offsetUnset($index) {
unset($this->_rules[$index]);
}
/**
* Returns an iterator for each of the rules to be applied
*
* @return ArrayIterator
**/
public function getIterator() {
return new ArrayIterator($this->_rules);
}
/**
* Returns the number of rules in this set
*
* @return int
**/
public function count() {
return count($this->_rules);
}
}