1 | <?php |
||
23 | class ParameterJuicer implements ParameterJuicerInterface |
||
24 | { |
||
25 | const STRATEGY_IGNORE_EXTRA_VALUES = 0; |
||
26 | const STRATEGY_REFUSE_EXTRA_VALUES = 1; |
||
27 | const STRATEGY_ACCEPT_EXTRA_VALUES = 2; |
||
28 | |||
29 | /** @var array list of validators, must be callables */ |
||
30 | protected $validators = []; |
||
31 | |||
32 | /** @var array list of cleaners, must be callables */ |
||
33 | protected $cleaners = []; |
||
34 | |||
35 | /** @var array list of fields, this gives an information if the field |
||
36 | is mandatory or optional. */ |
||
37 | protected $fields = []; |
||
38 | |||
39 | /** |
||
40 | * getName |
||
41 | * |
||
42 | * @see ParameterJuicerInterface |
||
43 | */ |
||
44 | public function getName(): string |
||
48 | |||
49 | /** |
||
50 | * addField |
||
51 | * |
||
52 | * Declare a new field with no validators nor cleaner. It can be declared |
||
53 | * if the field is optional or mandatory. |
||
54 | * If the field already exists, it is overriden. |
||
55 | */ |
||
56 | public function addField(string $name, bool $is_mandatory = true): self |
||
62 | |||
63 | /** |
||
64 | * addFields |
||
65 | * |
||
66 | * Declare several fields at once.Existing fields are overriden. |
||
67 | */ |
||
68 | public function addFields(array $fields, $are_mandatory = true): self |
||
74 | |||
75 | /** |
||
76 | * removeField |
||
77 | * |
||
78 | * Remove an existing field with all validators or cleaners associated to |
||
79 | * it if any. It throws an exception if the field does not exist. |
||
80 | * |
||
81 | * @throws \InvalidArgumentException |
||
82 | */ |
||
83 | public function removeField(string $name): self |
||
98 | |||
99 | /** |
||
100 | * addValidator |
||
101 | * |
||
102 | * Add a new validator associated to a key. If the field is not already |
||
103 | * declared, it is created. |
||
104 | * |
||
105 | * @throws \InvalidArgumentException |
||
106 | */ |
||
107 | public function addValidator(string $name, callable $validator): self |
||
116 | |||
117 | /** |
||
118 | * addCleaner |
||
119 | * |
||
120 | * Add a new cleaner associated to a key. |
||
121 | * |
||
122 | * @throws \InvalidArgumentException |
||
123 | */ |
||
124 | public function addCleaner(string $name, callable $cleaner): self |
||
133 | |||
134 | /** |
||
135 | * addJuicer |
||
136 | * |
||
137 | * Add a juicer to clean a validate a subset of data. |
||
138 | * |
||
139 | * @throws \InvalidArgumentException |
||
140 | */ |
||
141 | public function addJuicer( |
||
152 | |||
153 | /** |
||
154 | * squash |
||
155 | * |
||
156 | * Clean & validate the given data according to the definition. |
||
157 | * |
||
158 | * @see ParameterJuicerInterface |
||
159 | */ |
||
160 | public function squash( |
||
171 | |||
172 | /** |
||
173 | * validate |
||
174 | * |
||
175 | * Trigger validation on values. |
||
176 | * |
||
177 | * @see ParameterJuicerInterface |
||
178 | */ |
||
179 | public function validate( |
||
209 | |||
210 | /** |
||
211 | * clean |
||
212 | * |
||
213 | * Clean and return values. |
||
214 | * |
||
215 | * @see ParameterJuicerInterface |
||
216 | */ |
||
217 | public function clean(array $values): array |
||
230 | |||
231 | /** |
||
232 | * applyStrategy |
||
233 | * |
||
234 | * Apply extra values strategies. |
||
235 | * |
||
236 | * @throws RuntimeException if no valid stratgies were provided. |
||
237 | */ |
||
238 | private function applyStrategy(ValidationException $exception, array $values, int $strategy): array |
||
275 | |||
276 | /** |
||
277 | * checkFieldExists |
||
278 | * |
||
279 | * Throw an exception if the field does not exist. |
||
280 | * |
||
281 | * @throws \InvalidArgumentException |
||
282 | */ |
||
283 | private function checkFieldExists(string $name): self |
||
297 | |||
298 | /** |
||
299 | * launchExceptionWhenFail |
||
300 | * |
||
301 | * Check if the validation exception has messages. If yes, the exception is |
||
302 | * thrown. |
||
303 | */ |
||
304 | private function launchExceptionWhenFail(ValidationException $exception): self |
||
319 | |||
320 | private function launchValidatorsFor(string $field, $value, int $strategy, ValidationException $exception): self |
||
336 | } |
||
337 | |||
338 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.