Total Complexity | 13 |
Total Lines | 116 |
Duplicated Lines | 0 % |
Coverage | 72% |
Changes | 0 |
1 | <?php |
||
5 | class Validator implements ValidatorInterface |
||
6 | { |
||
7 | const IS_REQUIRED = 1; |
||
8 | |||
9 | const IS_OPTIONAL = 0; |
||
10 | |||
11 | /** |
||
12 | * @var ValidatorChain |
||
13 | */ |
||
14 | protected $chain; |
||
15 | |||
16 | /** |
||
17 | * @var ValidatorChain[] |
||
18 | */ |
||
19 | protected $dataValidatorChain; |
||
20 | |||
21 | /** |
||
22 | * Error message |
||
23 | * |
||
24 | * @var string |
||
25 | */ |
||
26 | protected $message; |
||
27 | |||
28 | /** |
||
29 | * Error code |
||
30 | * |
||
31 | * @var string |
||
32 | */ |
||
33 | protected $messageCode; |
||
34 | |||
35 | /** |
||
36 | * Index array of data list need to validate |
||
37 | * |
||
38 | * 1 is required |
||
39 | * 0 is optional |
||
40 | * |
||
41 | * @var string[] |
||
42 | */ |
||
43 | protected $data; |
||
44 | |||
45 | /** |
||
46 | * @var mixed[] |
||
47 | */ |
||
48 | protected $values; |
||
49 | |||
50 | /** |
||
51 | * @param $name |
||
52 | * |
||
53 | * @return ValidatorChain |
||
54 | */ |
||
55 | 1 | public function isRequired($name) |
|
56 | { |
||
57 | 1 | return $this->add($name, static::IS_REQUIRED); |
|
58 | } |
||
59 | |||
60 | /** |
||
61 | * @param $name |
||
62 | * |
||
63 | * @return ValidatorChain |
||
64 | */ |
||
65 | 2 | public function isOptional($name) |
|
68 | } |
||
69 | |||
70 | 2 | public function isValid($values) |
|
71 | { |
||
72 | 2 | foreach ($this->data as $name => $isRequired) { |
|
73 | 2 | if ($isRequired === static::IS_OPTIONAL) { |
|
74 | 2 | if (!isset($values[$name])) { |
|
75 | 2 | continue; |
|
76 | } |
||
77 | } |
||
78 | |||
79 | 2 | if (!isset($values[$name])) { |
|
80 | 1 | return false; |
|
81 | } |
||
82 | |||
83 | 2 | if (!$this->dataValidatorChain[$name]->isValid($values[$name])) { |
|
84 | return false; |
||
85 | } |
||
86 | } |
||
87 | |||
88 | 2 | return true; |
|
89 | } |
||
90 | |||
91 | public function getMessage() |
||
93 | // TODO: Implement getMessage() method. |
||
94 | } |
||
95 | |||
96 | public function getMessageCode() |
||
98 | // TODO: Implement getMessageCode() method. |
||
99 | } |
||
100 | |||
101 | public function getStandardValue() |
||
103 | // TODO: Implement getStandardValue() method. |
||
104 | } |
||
105 | |||
106 | /** |
||
107 | * @param $name |
||
108 | * @param $isRequired |
||
109 | * |
||
110 | * @return ValidatorChain |
||
111 | */ |
||
112 | 2 | public function add($name, $isRequired) |
|
123 |