1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19:
20:
21: App::uses('CakeValidationSet', 'Model/Validator');
22: App::uses('Hash', 'Utility');
23:
24: 25: 26: 27: 28: 29: 30: 31: 32: 33:
34: class ModelValidator implements ArrayAccess, IteratorAggregate, Countable {
35:
36: 37: 38: 39: 40:
41: protected $_fields = array();
42:
43: 44: 45: 46: 47:
48: protected $_model = array();
49:
50: 51: 52: 53: 54: 55:
56: protected $_validate = array();
57:
58: 59: 60: 61: 62: 63:
64: protected $_methods = array();
65:
66: 67: 68: 69: 70:
71: protected $_modelMethods = array();
72:
73: 74: 75: 76: 77:
78: protected $_behaviors = array();
79:
80: 81: 82: 83: 84:
85: public function __construct(Model $Model) {
86: $this->_model = $Model;
87: }
88:
89: 90: 91: 92: 93: 94: 95: 96: 97:
98: public function validates($options = array()) {
99: $errors = $this->errors($options);
100: if (empty($errors) && $errors !== false) {
101: $errors = $this->_validateWithModels($options);
102: }
103: if (is_array($errors)) {
104: return count($errors) === 0;
105: }
106: return $errors;
107: }
108:
109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127:
128: public function validateAssociated(&$data, $options = array()) {
129: $model = $this->getModel();
130: $options += array('atomic' => true, 'deep' => false);
131: $model->validationErrors = $validationErrors = $return = array();
132: $model->create(null);
133: $return[$model->alias] = true;
134: if (!($model->set($data) && $model->validates($options))) {
135: $validationErrors[$model->alias] = $model->validationErrors;
136: $return[$model->alias] = false;
137: }
138: $data = $model->data;
139: if (!empty($options['deep']) && isset($data[$model->alias])) {
140: $recordData = $data[$model->alias];
141: unset($data[$model->alias]);
142: $data += $recordData;
143: }
144:
145: $associations = $model->getAssociated();
146: foreach ($data as $association => &$values) {
147: $validates = true;
148: if (isset($associations[$association])) {
149: if (in_array($associations[$association], array('belongsTo', 'hasOne'))) {
150: if ($options['deep']) {
151: $validates = $model->{$association}->validateAssociated($values, $options);
152: } else {
153: $model->{$association}->create(null);
154: $validates = $model->{$association}->set($values) && $model->{$association}->validates($options);
155: $data[$association] = $model->{$association}->data[$model->{$association}->alias];
156: }
157: if (is_array($validates)) {
158: $validates = !in_array(false, Hash::flatten($validates), true);
159: }
160: $return[$association] = $validates;
161: } elseif ($associations[$association] === 'hasMany') {
162: $validates = $model->{$association}->validateMany($values, $options);
163: $return[$association] = $validates;
164: }
165: if (!$validates || (is_array($validates) && in_array(false, $validates, true))) {
166: $validationErrors[$association] = $model->{$association}->validationErrors;
167: }
168: }
169: }
170:
171: $model->validationErrors = $validationErrors;
172: if (isset($validationErrors[$model->alias])) {
173: $model->validationErrors = $validationErrors[$model->alias];
174: unset($validationErrors[$model->alias]);
175: $model->validationErrors = array_merge($model->validationErrors, $validationErrors);
176: }
177: if (!$options['atomic']) {
178: return $return;
179: }
180: if ($return[$model->alias] === false || !empty($model->validationErrors)) {
181: return false;
182: }
183: return true;
184: }
185:
186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204:
205: public function validateMany(&$data, $options = array()) {
206: $model = $this->getModel();
207: $options += array('atomic' => true, 'deep' => false);
208: $model->validationErrors = $validationErrors = $return = array();
209: foreach ($data as $key => &$record) {
210: if ($options['deep']) {
211: $validates = $model->validateAssociated($record, $options);
212: } else {
213: $model->create(null);
214: $validates = $model->set($record) && $model->validates($options);
215: $data[$key] = $model->data;
216: }
217: if ($validates === false || (is_array($validates) && in_array(false, Hash::flatten($validates), true))) {
218: $validationErrors[$key] = $model->validationErrors;
219: $validates = false;
220: } else {
221: $validates = true;
222: }
223: $return[$key] = $validates;
224: }
225: $model->validationErrors = $validationErrors;
226: if (!$options['atomic']) {
227: return $return;
228: }
229: return empty($model->validationErrors);
230: }
231:
232: 233: 234: 235: 236: 237: 238: 239: 240:
241: public function errors($options = array()) {
242: if (!$this->_triggerBeforeValidate($options)) {
243: return false;
244: }
245: $model = $this->getModel();
246:
247: if (!$this->_parseRules()) {
248: return $model->validationErrors;
249: }
250:
251: $fieldList = $model->whitelist;
252: if (empty($fieldList) && !empty($options['fieldList'])) {
253: if (!empty($options['fieldList'][$model->alias]) && is_array($options['fieldList'][$model->alias])) {
254: $fieldList = $options['fieldList'][$model->alias];
255: } else {
256: $fieldList = $options['fieldList'];
257: }
258: }
259:
260: $exists = $model->exists($model->getID());
261: $methods = $this->getMethods();
262: $fields = $this->_validationList($fieldList);
263:
264: foreach ($fields as $field) {
265: $field->setMethods($methods);
266: $field->setValidationDomain($model->validationDomain);
267: $data = isset($model->data[$model->alias]) ? $model->data[$model->alias] : array();
268: $errors = $field->validate($data, $exists);
269: foreach ($errors as $error) {
270: $this->invalidate($field->field, $error);
271: }
272: }
273:
274: $model->getEventManager()->dispatch(new CakeEvent('Model.afterValidate', $model));
275: return $model->validationErrors;
276: }
277:
278: 279: 280: 281: 282: 283: 284: 285:
286: public function invalidate($field, $message = true) {
287: $this->getModel()->validationErrors[$field][] = $message;
288: }
289:
290: 291: 292: 293: 294: 295:
296: public function getMethods() {
297: $behaviors = $this->_model->Behaviors->enabled();
298: if (!empty($this->_methods) && $behaviors === $this->_behaviors) {
299: return $this->_methods;
300: }
301: $this->_behaviors = $behaviors;
302:
303: if (empty($this->_modelMethods)) {
304: foreach (get_class_methods($this->_model) as $method) {
305: $this->_modelMethods[strtolower($method)] = array($this->_model, $method);
306: }
307: }
308:
309: $methods = $this->_modelMethods;
310: foreach (array_keys($this->_model->Behaviors->methods()) as $method) {
311: $methods += array(strtolower($method) => array($this->_model, $method));
312: }
313:
314: return $this->_methods = $methods;
315: }
316:
317: 318: 319: 320: 321: 322: 323:
324: public function getField($name = null) {
325: $this->_parseRules();
326: if ($name !== null) {
327: if (!empty($this->_fields[$name])) {
328: return $this->_fields[$name];
329: }
330: return null;
331: }
332: return $this->_fields;
333: }
334:
335: 336: 337: 338: 339: 340:
341: protected function _parseRules() {
342: if ($this->_validate === $this->_model->validate) {
343: return true;
344: }
345:
346: if (empty($this->_model->validate)) {
347: $this->_validate = array();
348: $this->_fields = array();
349: return false;
350: }
351:
352: $this->_validate = $this->_model->validate;
353: $this->_fields = array();
354: $methods = $this->getMethods();
355: foreach ($this->_validate as $fieldName => $ruleSet) {
356: $this->_fields[$fieldName] = new CakeValidationSet($fieldName, $ruleSet);
357: $this->_fields[$fieldName]->setMethods($methods);
358: }
359: return true;
360: }
361:
362: 363: 364: 365: 366: 367:
368: public function setValidationDomain($validationDomain = null) {
369: if (empty($validationDomain)) {
370: $validationDomain = 'default';
371: }
372: $this->getModel()->validationDomain = $validationDomain;
373: return $this;
374: }
375:
376: 377: 378: 379: 380:
381: public function getModel() {
382: return $this->_model;
383: }
384:
385: 386: 387: 388: 389: 390:
391: protected function _validationList($fieldList = array()) {
392: if (empty($fieldList) || Hash::dimensions($fieldList) > 1) {
393: return $this->_fields;
394: }
395:
396: $validateList = array();
397: $this->validationErrors = array();
398: foreach ((array)$fieldList as $f) {
399: if (!empty($this->_fields[$f])) {
400: $validateList[$f] = $this->_fields[$f];
401: }
402: }
403:
404: return $validateList;
405: }
406:
407: 408: 409: 410: 411: 412: 413: 414:
415: protected function _validateWithModels($options) {
416: $valid = true;
417: $model = $this->getModel();
418:
419: foreach ($model->hasAndBelongsToMany as $assoc => $association) {
420: if (empty($association['with']) || !isset($model->data[$assoc])) {
421: continue;
422: }
423: list($join) = $model->joinModel($model->hasAndBelongsToMany[$assoc]['with']);
424: $data = $model->data[$assoc];
425:
426: $newData = array();
427: foreach ((array)$data as $row) {
428: if (isset($row[$model->hasAndBelongsToMany[$assoc]['associationForeignKey']])) {
429: $newData[] = $row;
430: } elseif (isset($row[$join]) && isset($row[$join][$model->hasAndBelongsToMany[$assoc]['associationForeignKey']])) {
431: $newData[] = $row[$join];
432: }
433: }
434: foreach ($newData as $data) {
435: $data[$model->hasAndBelongsToMany[$assoc]['foreignKey']] = $model->id;
436: $model->{$join}->create($data);
437: $valid = ($valid && $model->{$join}->validator()->validates($options));
438: }
439: }
440: return $valid;
441: }
442:
443: 444: 445: 446: 447: 448: 449:
450: protected function _triggerBeforeValidate($options = array()) {
451: $model = $this->getModel();
452: $event = new CakeEvent('Model.beforeValidate', $model, array($options));
453: list($event->break, $event->breakOn) = array(true, false);
454: $model->getEventManager()->dispatch($event);
455: if ($event->isStopped()) {
456: return false;
457: }
458: return true;
459: }
460:
461: 462: 463: 464: 465: 466:
467: public function offsetExists($field) {
468: $this->_parseRules();
469: return isset($this->_fields[$field]);
470: }
471:
472: 473: 474: 475: 476: 477:
478: public function offsetGet($field) {
479: $this->_parseRules();
480: return $this->_fields[$field];
481: }
482:
483: 484: 485: 486: 487: 488: 489:
490: public function offsetSet($field, $rules) {
491: $this->_parseRules();
492: if (!$rules instanceof CakeValidationSet) {
493: $rules = new CakeValidationSet($field, $rules);
494: $methods = $this->getMethods();
495: $rules->setMethods($methods);
496: }
497: $this->_fields[$field] = $rules;
498: }
499:
500: 501: 502: 503: 504: 505:
506: public function offsetUnset($field) {
507: $this->_parseRules();
508: unset($this->_fields[$field]);
509: }
510:
511: 512: 513: 514: 515:
516: public function getIterator() {
517: $this->_parseRules();
518: return new ArrayIterator($this->_fields);
519: }
520:
521: 522: 523: 524: 525:
526: public function count() {
527: $this->_parseRules();
528: return count($this->_fields);
529: }
530:
531: 532: 533: 534: 535: 536: 537: 538: 539: 540: 541: 542: 543: 544: 545: 546: 547: 548: 549: 550: 551: 552: 553:
554: public function add($field, $name, $rule = null) {
555: $this->_parseRules();
556: if ($name instanceof CakeValidationSet) {
557: $this->_fields[$field] = $name;
558: return $this;
559: }
560:
561: if (!isset($this->_fields[$field])) {
562: $rule = (is_string($name)) ? array($name => $rule) : $name;
563: $this->_fields[$field] = new CakeValidationSet($field, $rule);
564: } else {
565: if (is_string($name)) {
566: $this->_fields[$field]->setRule($name, $rule);
567: } else {
568: $this->_fields[$field]->setRules($name);
569: }
570: }
571:
572: $methods = $this->getMethods();
573: $this->_fields[$field]->setMethods($methods);
574:
575: return $this;
576: }
577:
578: 579: 580: 581: 582: 583: 584: 585: 586: 587: 588: 589: 590: 591: 592:
593: public function remove($field, $rule = null) {
594: $this->_parseRules();
595: if ($rule === null) {
596: unset($this->_fields[$field]);
597: } elseif (array_key_exists($field, $this->_fields)) {
598: $this->_fields[$field]->removeRule($rule);
599: }
600: return $this;
601: }
602: }
603: