initial commit
[namibia] / module / Utility / src / Utility / Service / Validator.php
1 <?php
2 namespace Utility\Service;
3
4
5 /**
6  * Input validation from meta-data.
7  * @author andre.fourie
8  */
9 class Validator
10 {
11
12         /**
13          * @var array
14          */
15         protected $messages;
16
17
18         /**
19          * Validate a set of inputs from validation meta-data.
20          * @param array $validation
21          * @param array $input
22          * @return boolean
23          */
24         public function validateInputSet(array $validation, array $input)
25         {
26                 $valid = true;
27                 $this->messages = array();
28                 foreach ($validation as $field => $chain)
29                 {
30                         if (!isset($input[$field]))
31                         {
32                                 continue;
33                         }
34                         $validatorChain = new \Zend\Validator\ValidatorChain();
35                         foreach ($chain['Validate'] as $validator)
36                         {
37                                 $class = !isset($validator['I18nClass'])
38                                         ? '\\Zend\\Validator\\' . $validator['Class']
39                                         : '\\Zend\\I18n\\Validator\\' . $validator['I18nClass'];
40                                 $validatorChain->attach(new $class($validator['Options']));
41                         }
42                         if (!$validatorChain->isValid($input[$field])) {
43                                 $valid = false;
44                                 foreach ($validatorChain->getMessages() as $message) {
45                                         $this->messages[$field][] = $message;
46                                 }
47                         }
48                 }
49                 return $valid;
50         }
51
52         /**
53          * Validate a set of inputs from validation meta-data.
54          * @param array $validation
55          * @param array $input
56          * @return boolean
57          */
58         public function validateGroupedInputSet(array $validationGroups, array $input)
59         {
60                 $valid = true;
61                 $this->messages = array();
62                 foreach ($validationGroups as $group => $validationSet)
63                 {
64                         foreach ($validationSet as $field => $chain)
65                         {
66                                 if (!isset($input[$group][$field]))
67                                 {
68                                         continue;
69                                 }
70                                 $validatorChain = new \Zend\Validator\ValidatorChain();
71                                 foreach ($chain['Validate'] as $validator)
72                                 {
73                                         $class = !isset($validator['I18nClass'])
74                                                 ? '\\Zend\\Validator\\' . $validator['Class']
75                                                 : '\\Zend\\I18n\\Validator\\' . $validator['I18nClass'];
76                                         $validatorChain->attach(new $class($validator['Options']));
77                                 }
78                                 if (!$validatorChain->isValid($input[$group][$field])) {
79                                         $valid = false;
80                                         foreach ($validatorChain->getMessages() as $message) {
81                                                 isset($this->messages[$group])
82                                                         || $this->messages[$group] = array();
83                                                 $this->messages[$group][$field][] = $message;
84                                         }
85                                 }
86                         }
87                 }
88                 return $valid;
89         }
90
91         /**
92          * Retrieve validation error messages.
93          * @return array
94          */
95         public function getMessages()
96         {
97                 return $this->messages;
98         }
99
100
101 }