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