1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20:
21:
22: App::uses('CakeValidationSet', 'Model/Validator');
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_merge(array('atomic' => true, 'deep' => false), $options);
131: $model->validationErrors = $validationErrors = $return = array();
132: $model->create(null);
133: if (!($model->set($data) && $model->validates($options))) {
134: $validationErrors[$model->alias] = $model->validationErrors;
135: $return[$model->alias] = false;
136: } else {
137: $return[$model->alias] = true;
138: }
139: $data = $model->data;
140: if (!empty($options['deep']) && isset($data[$model->alias])) {
141: $recordData = $data[$model->alias];
142: unset($data[$model->alias]);
143: $data = array_merge($data, $recordData);
144: }
145:
146: $associations = $model->getAssociated();
147: foreach ($data as $association => &$values) {
148: $validates = true;
149: if (isset($associations[$association])) {
150: if (in_array($associations[$association], array('belongsTo', 'hasOne'))) {
151: if ($options['deep']) {
152: $validates = $model->{$association}->validateAssociated($values, $options);
153: } else {
154: $model->{$association}->create(null);
155: $validates = $model->{$association}->set($values) && $model->{$association}->validates($options);
156: $data[$association] = $model->{$association}->data[$model->{$association}->alias];
157: }
158: if (is_array($validates)) {
159: if (in_array(false, Hash::flatten($validates), true)) {
160: $validates = false;
161: } else {
162: $validates = true;
163: }
164: }
165: $return[$association] = $validates;
166: } elseif ($associations[$association] === 'hasMany') {
167: $validates = $model->{$association}->validateMany($values, $options);
168: $return[$association] = $validates;
169: }
170: if (!$validates || (is_array($validates) && in_array(false, $validates, true))) {
171: $validationErrors[$association] = $model->{$association}->validationErrors;
172: }
173: }
174: }
175:
176: $model->validationErrors = $validationErrors;
177: if (isset($validationErrors[$model->alias])) {
178: $model->validationErrors = $validationErrors[$model->alias];
179: unset($validationErrors[$model->alias]);
180: $model->validationErrors = array_merge($model->validationErrors, $validationErrors);
181: }
182: if (!$options['atomic']) {
183: return $return;
184: }
185: if ($return[$model->alias] === false || !empty($model->validationErrors)) {
186: return false;
187: }
188: return true;
189: }
190:
191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210:
211: public function validateMany(&$data, $options = array()) {
212: $model = $this->getModel();
213: $options = array_merge(array('atomic' => true, 'deep' => false), $options);
214: $model->validationErrors = $validationErrors = $return = array();
215: foreach ($data as $key => &$record) {
216: if ($options['deep']) {
217: $validates = $model->validateAssociated($record, $options);
218: } else {
219: $model->create(null);
220: $validates = $model->set($record) && $model->validates($options);
221: $data[$key] = $model->data;
222: }
223: if ($validates === false || (is_array($validates) && in_array(false, Hash::flatten($validates), true))) {
224: $validationErrors[$key] = $model->validationErrors;
225: $validates = false;
226: } else {
227: $validates = true;
228: }
229: $return[$key] = $validates;
230: }
231: $model->validationErrors = $validationErrors;
232: if (!$options['atomic']) {
233: return $return;
234: }
235: if (empty($model->validationErrors)) {
236: return true;
237: }
238: return false;
239: }
240:
241: 242: 243: 244: 245: 246: 247: 248:
249: public function errors($options = array()) {
250: if (!$this->_triggerBeforeValidate($options)) {
251: return false;
252: }
253: $model = $this->getModel();
254:
255: if (!$this->_parseRules()) {
256: return $model->validationErrors;
257: }
258:
259: $fieldList = isset($options['fieldList']) ? $options['fieldList'] : array();
260: $exists = $model->exists();
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 && !empty($this->_fields[$name])) {
327: return $this->_fields[$name];
328: } elseif ($name !== null) {
329: return null;
330: }
331: return $this->_fields;
332: }
333:
334: 335: 336: 337: 338: 339:
340: protected function _parseRules() {
341: if ($this->_validate === $this->_model->validate) {
342: return true;
343: }
344:
345: if (empty($this->_model->validate)) {
346: $this->_validate = array();
347: $this->_fields = array();
348: return false;
349: }
350:
351: $this->_validate = $this->_model->validate;
352: $this->_fields = array();
353: $methods = $this->getMethods();
354: foreach ($this->_validate as $fieldName => $ruleSet) {
355: $this->_fields[$fieldName] = new CakeValidationSet($fieldName, $ruleSet);
356: $this->_fields[$fieldName]->setMethods($methods);
357: }
358: return true;
359: }
360:
361: 362: 363: 364: 365: 366:
367: public function setValidationDomain($validationDomain = null) {
368: if (empty($validationDomain)) {
369: $validationDomain = 'default';
370: }
371: $this->getModel()->validationDomain = $validationDomain;
372: return $this;
373: }
374:
375: 376: 377: 378: 379:
380: public function getModel() {
381: return $this->_model;
382: }
383:
384: 385: 386: 387: 388: 389: 390:
391: protected function _validationList($fieldList = array()) {
392: $model = $this->getModel();
393: $whitelist = $model->whitelist;
394:
395: if (!empty($fieldList)) {
396: if (!empty($fieldList[$model->alias]) && is_array($fieldList[$model->alias])) {
397: $whitelist = $fieldList[$model->alias];
398: } else {
399: $whitelist = $fieldList;
400: }
401: }
402: unset($fieldList);
403:
404: $validateList = array();
405: if (!empty($whitelist)) {
406: $this->validationErrors = array();
407:
408: foreach ((array)$whitelist as $f) {
409: if (!empty($this->_fields[$f])) {
410: $validateList[$f] = $this->_fields[$f];
411: }
412: }
413: } else {
414: return $this->_fields;
415: }
416:
417: return $validateList;
418: }
419:
420: 421: 422: 423: 424: 425: 426: 427:
428: protected function _validateWithModels($options) {
429: $valid = true;
430: $model = $this->getModel();
431:
432: foreach ($model->hasAndBelongsToMany as $assoc => $association) {
433: if (empty($association['with']) || !isset($model->data[$assoc])) {
434: continue;
435: }
436: list($join) = $model->joinModel($model->hasAndBelongsToMany[$assoc]['with']);
437: $data = $model->data[$assoc];
438:
439: $newData = array();
440: foreach ((array)$data as $row) {
441: if (isset($row[$model->hasAndBelongsToMany[$assoc]['associationForeignKey']])) {
442: $newData[] = $row;
443: } elseif (isset($row[$join]) && isset($row[$join][$model->hasAndBelongsToMany[$assoc]['associationForeignKey']])) {
444: $newData[] = $row[$join];
445: }
446: }
447: if (empty($newData)) {
448: continue;
449: }
450: foreach ($newData as $data) {
451: $data[$model->hasAndBelongsToMany[$assoc]['foreignKey']] = $model->id;
452: $model->{$join}->create($data);
453: $valid = ($valid && $model->{$join}->validator()->validates($options));
454: }
455: }
456: return $valid;
457: }
458:
459: 460: 461: 462: 463: 464:
465: protected function _triggerBeforeValidate($options = array()) {
466: $model = $this->getModel();
467: $event = new CakeEvent('Model.beforeValidate', $model, array($options));
468: list($event->break, $event->breakOn) = array(true, false);
469: $model->getEventManager()->dispatch($event);
470: if ($event->isStopped()) {
471: return false;
472: }
473: return true;
474: }
475:
476: 477: 478: 479: 480: 481:
482: public function offsetExists($field) {
483: $this->_parseRules();
484: return isset($this->_fields[$field]);
485: }
486:
487: 488: 489: 490: 491: 492:
493: public function offsetGet($field) {
494: $this->_parseRules();
495: return $this->_fields[$field];
496: }
497:
498: 499: 500: 501: 502: 503: 504:
505: public function offsetSet($field, $rules) {
506: $this->_parseRules();
507: if (!$rules instanceof CakeValidationSet) {
508: $rules = new CakeValidationSet($field, $rules);
509: $methods = $this->getMethods();
510: $rules->setMethods($methods);
511: }
512: $this->_fields[$field] = $rules;
513: }
514:
515: 516: 517: 518: 519: 520:
521: public function offsetUnset($field) {
522: $this->_parseRules();
523: unset($this->_fields[$field]);
524: }
525:
526: 527: 528: 529: 530:
531: public function getIterator() {
532: $this->_parseRules();
533: return new ArrayIterator($this->_fields);
534: }
535:
536: 537: 538: 539: 540:
541: public function count() {
542: $this->_parseRules();
543: return count($this->_fields);
544: }
545:
546: 547: 548: 549: 550: 551: 552: 553: 554: 555: 556: 557: 558: 559: 560: 561: 562: 563: 564: 565: 566: 567: 568:
569: public function add($field, $name, $rule = null) {
570: $this->_parseRules();
571: if ($name instanceof CakeValidationSet) {
572: $this->_fields[$field] = $name;
573: return $this;
574: }
575:
576: if (!isset($this->_fields[$field])) {
577: $rule = (is_string($name)) ? array($name => $rule) : $name;
578: $this->_fields[$field] = new CakeValidationSet($field, $rule);
579: } else {
580: if (is_string($name)) {
581: $this->_fields[$field]->setRule($name, $rule);
582: } else {
583: $this->_fields[$field]->setRules($name);
584: }
585: }
586:
587: $methods = $this->getMethods();
588: $this->_fields[$field]->setMethods($methods);
589:
590: return $this;
591: }
592:
593: 594: 595: 596: 597: 598: 599: 600: 601: 602: 603: 604: 605: 606: 607:
608: public function remove($field, $rule = null) {
609: $this->_parseRules();
610: if ($rule === null) {
611: unset($this->_fields[$field]);
612: } else {
613: $this->_fields[$field]->removeRule($rule);
614: }
615: return $this;
616: }
617: }
618: