1 | <?php |
||
12 | abstract class ValueObject |
||
13 | { |
||
14 | /** |
||
15 | * This array contains VO parameters values which is already validated. |
||
16 | * |
||
17 | * @var array $params Validated params, which are available through magic method getParameterNameInCamelCase. |
||
18 | */ |
||
19 | private $params = []; |
||
20 | |||
21 | /** |
||
22 | * This array contains validation errors. |
||
23 | * |
||
24 | * @var array $errors Validation errors. |
||
25 | */ |
||
26 | private $errors = []; |
||
27 | |||
28 | /** |
||
29 | * Symfony validator instance. |
||
30 | * |
||
31 | * @var ValidatorInterface |
||
32 | */ |
||
33 | private $validator; |
||
34 | |||
35 | /** |
||
36 | * Returns validation rules. |
||
37 | * |
||
38 | * This rules describes REQUIRED parameters. |
||
39 | * Optional parameter must be resolved outside VO instance, |
||
40 | * so only in this way we can ensure that VO have all parameters and they all valid. |
||
41 | * |
||
42 | * @return array Array with validation rules. |
||
43 | */ |
||
44 | abstract protected function getRules(); |
||
45 | |||
46 | /** |
||
47 | * Constructor. |
||
48 | * |
||
49 | 10 | * Most important method. |
|
50 | * Here we validate received parameters against rules defined in method getRules, |
||
51 | 10 | * and call after validation lifecycle callback. |
|
52 | 10 | * |
|
53 | * @param array $params Parameters being validated. |
||
54 | * |
||
55 | 10 | * @throws ValidationException In case when validation failed. |
|
56 | 6 | */ |
|
57 | public function __construct(array $params) |
||
70 | |||
71 | 10 | /** |
|
72 | * Most important method. Here performs all validation stuff. |
||
73 | * |
||
74 | * @param array $params Parameters being validated. |
||
75 | */ |
||
76 | private function validate(array $params) |
||
94 | |||
95 | 9 | /** |
|
96 | 9 | * Gets validation constraints out from VO rules. |
|
97 | * |
||
98 | * One validation rule (parameter $rules) can have lot of constraints (Symfony constraints). |
||
99 | * We must check them all, |
||
100 | 9 | * so that's why here we build array with appropriate constraints for particular validation rule. |
|
101 | * This array contains only constraints for only one validation rule, |
||
102 | * for only one parameter which must be validated. |
||
103 | * |
||
104 | * @param array $rules Array of validation rules. |
||
105 | 9 | * |
|
106 | * @return array |
||
107 | */ |
||
108 | private function getConstraints(array $rules) |
||
128 | |||
129 | /** |
||
130 | 4 | * Validate parameter value against bunch of constraints. |
|
131 | * |
||
132 | 6 | * @param string $paramName Parameter name. |
|
133 | * @param string $value Parameter value. |
||
134 | * @param array $constraints Array of validation constraints. |
||
135 | */ |
||
136 | private function validateParameter($paramName, $value, array $constraints) |
||
151 | |||
152 | /** |
||
153 | * Sets error message for certain parameter. |
||
154 | * |
||
155 | 2 | * @param string $paramName Parameter name (key in array $parameters which is passed to __constructor method). |
|
156 | * @param string $message Custom error message. |
||
157 | 2 | */ |
|
158 | 2 | public function setError($paramName, $message) |
|
169 | 1 | ||
170 | /** |
||
171 | 1 | * Lifecycle callback - after validation. |
|
172 | * |
||
173 | * This method can be used for custom validation purposes. |
||
174 | * This method will be called each time after validation rules (validate method). |
||
175 | * |
||
176 | * @param array $params Array with parameters passed to VO. |
||
177 | */ |
||
178 | public function afterValidation(array $params) |
||
180 | |||
181 | /** |
||
182 | * Returns parameter value by parameter name. |
||
183 | * |
||
184 | * @param string $name Parameter name (key in array $parameters which is passed to __constructor method). |
||
185 | * @param array $arguments Array with arguments. |
||
186 | * |
||
187 | * @throws ParameterNotFoundException In case when VO don't have needed parameter. |
||
188 | * |
||
189 | * @return mixed Parameter value. |
||
190 | */ |
||
191 | public function __call($name, array $arguments) |
||
200 | |||
201 | /** |
||
202 | * Gets VO parameter as array. |
||
203 | * |
||
204 | * @return array Array which contains all VO parameters. |
||
205 | */ |
||
206 | public function toArray() |
||
210 | } |
||
211 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.