1: <?php
2: 3: 4: 5: 6: 7:
8:
9: namespace Wei\Validator;
10:
11: 12: 13: 14: 15:
16: class All extends BaseValidator
17: {
18: 19: 20:
21: protected $notArrayMessage = '%name% must be of type array';
22:
23: 24: 25: 26: 27:
28: protected $invalidMessage = 'Some of the items is not valid';
29:
30: 31: 32:
33: protected $itemName = '%name%\'s %index% item';
34:
35: 36: 37:
38: protected $rules = array();
39:
40: 41: 42: 43: 44:
45: protected $validators = array();
46:
47: 48: 49: 50: 51: 52: 53:
54: public function __invoke($input, array $rules = array())
55: {
56: $rules && $this->storeOption('rules', $rules);
57:
58: return $this->isValid($input);
59: }
60:
61: 62: 63:
64: protected function doValidate($input)
65: {
66: if (!is_array($input) && !$input instanceof \Traversable) {
67: $this->addError('notArray');
68: return false;
69: }
70:
71: $index = 1;
72: $validator = null;
73: foreach ($input as $item) {
74: foreach ($this->rules as $rule => $options) {
75: if (!$this->validate->validateOne($rule, $item, $options, $validator)) {
76: $this->validators[$index][$rule] = $validator;
77: }
78: }
79: $index++;
80: }
81:
82:
83: if (count($this->validators)) {
84: $this->addError('invalid');
85: return false;
86: }
87: return true;
88: }
89:
90: 91: 92:
93: public function getMessages($name = null)
94: {
95: $this->loadTranslationMessages();
96: $translator = $this->t;
97:
98:
99:
100: $name = $translator($translator($this->itemName), array(
101: '%name%' => $translator($name ?: $this->name)
102: ));
103:
104: $messages = array();
105: foreach ($this->validators as $index => $validators) {
106:
107: foreach ($validators as $rule => $validator) {
108:
109: $validator->setName($translator($name, array(
110: '%index%' => $index
111: )));
112: foreach ($validator->getMessages() as $option => $message) {
113: $messages[$rule . '.' . $option . '.' . $index] = $message;
114: }
115: }
116: }
117: return $messages;
118: }
119: }
120: