1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Author: Nil Portugués Calderó <[email protected]> |
4
|
|
|
* Date: 9/16/14 |
5
|
|
|
* Time: 9:10 PM |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace NilPortugues\Validator; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class AbstractValidator |
15
|
|
|
* @package NilPortugues\Validator |
16
|
|
|
*/ |
17
|
|
|
abstract class AbstractValidator |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var Validator |
21
|
|
|
*/ |
22
|
|
|
protected $validator; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var ValidatorFunctionMap |
26
|
|
|
*/ |
27
|
|
|
protected $functionMap; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var array |
31
|
|
|
*/ |
32
|
|
|
protected $conditions = []; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var array |
36
|
|
|
*/ |
37
|
|
|
protected $errors = []; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var array |
41
|
|
|
*/ |
42
|
|
|
protected $errorArray = []; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var string |
46
|
|
|
*/ |
47
|
|
|
private $propertyName; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param string $propertyName |
51
|
|
|
* @param Validator $validator |
52
|
|
|
* @param array $errorMessages |
53
|
|
|
* @param array $functionMap |
54
|
|
|
*/ |
55
|
|
|
public function __construct($propertyName, Validator $validator, array &$errorMessages, array &$functionMap) |
56
|
|
|
{ |
57
|
|
|
$this->propertyName = $propertyName; |
58
|
|
|
$this->validator = $validator; |
59
|
|
|
$this->functionMap = ValidatorFunctionMap::getInstance($this, $functionMap); |
60
|
|
|
$this->errorArray = $errorMessages; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param string $classMethod |
65
|
|
|
* @param array $arguments |
66
|
|
|
* @param array $errorMessageValues |
67
|
|
|
* @param bool $fetchValidatorErrors |
68
|
|
|
* |
69
|
|
|
* @return $this |
70
|
|
|
*/ |
71
|
|
|
protected function addCondition( |
72
|
|
|
$classMethod, |
73
|
|
|
array $arguments = [], |
74
|
|
|
array $errorMessageValues = [], |
75
|
|
|
$fetchValidatorErrors = false |
76
|
|
|
) { |
77
|
|
|
$classMethod = \explode("\\", $classMethod); |
78
|
|
|
$classMethod = \array_pop($classMethod); |
79
|
|
|
|
80
|
|
|
$this->conditions[$classMethod] = [ |
81
|
|
|
'key' => $classMethod, |
82
|
|
|
'arguments' => $arguments, |
83
|
|
|
'values' => $errorMessageValues, |
84
|
|
|
'is_validator' => $fetchValidatorErrors, |
85
|
|
|
]; |
86
|
|
|
|
87
|
|
|
return $this; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param $value |
92
|
|
|
* @param bool $stopOnError |
93
|
|
|
* |
94
|
|
|
* @return bool |
95
|
|
|
*/ |
96
|
|
|
public function validate($value, $stopOnError = false) |
97
|
|
|
{ |
98
|
|
|
$isValid = true; |
99
|
|
|
$this->errors = []; |
100
|
|
|
$this->conditions = \array_filter($this->conditions); |
101
|
|
|
|
102
|
|
|
foreach ($this->conditions as $condition) { |
103
|
|
|
$arguments = $condition['arguments']; |
104
|
|
|
|
105
|
|
|
if (false === \strpos('FileUpload::isUploaded', $condition['key'])) { |
106
|
|
|
$arguments = \array_merge([$value], $condition['arguments']); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
$isValid = $isValid && $this->functionMap->get( |
110
|
|
|
$this->propertyName, |
111
|
|
|
$condition['key'], |
112
|
|
|
$arguments, |
113
|
|
|
$condition['values'], |
114
|
|
|
$this->errorArray |
115
|
|
|
); |
116
|
|
|
|
117
|
|
|
if (false === $isValid) { |
118
|
|
|
if ($condition['is_validator']) { |
119
|
|
|
$this->getErrorsForValidatorsAsArguments($condition['arguments']); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
if (true === $stopOnError) { |
123
|
|
|
$isValid = false; |
124
|
|
|
break; |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
$this->conditions[] = []; |
130
|
|
|
|
131
|
|
|
return $isValid; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @param $arguments |
136
|
|
|
*/ |
137
|
|
|
private function getErrorsForValidatorsAsArguments($arguments) |
138
|
|
|
{ |
139
|
|
|
foreach ($arguments as $argument) { |
140
|
|
|
if ($argument instanceof AbstractValidator) { |
141
|
|
|
$this->errors = \array_merge($this->errors, $argument->getErrors()); |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @param string $error |
148
|
|
|
* @param string $propertyName |
149
|
|
|
* |
150
|
|
|
* @return $this |
151
|
|
|
*/ |
152
|
|
|
public function setError($error, $propertyName) |
153
|
|
|
{ |
154
|
|
|
$this->errors[$this->propertyName][$propertyName] = $error; |
155
|
|
|
|
156
|
|
|
return $this; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @return array |
161
|
|
|
*/ |
162
|
|
|
public function getErrors() |
163
|
|
|
{ |
164
|
|
|
return $this->errors; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Removes errors and conditions. Useful to use the library as an Assertion library. |
169
|
|
|
*/ |
170
|
|
|
public function reset() |
171
|
|
|
{ |
172
|
|
|
$this->errors = []; |
173
|
|
|
$this->conditions = []; |
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
|