Total Complexity | 8 |
Total Lines | 38 |
Duplicated Lines | 0 % |
Coverage | 91.3% |
Changes | 2 | ||
Bugs | 0 | Features | 0 |
1 | <?php declare(strict_types=1); |
||
16 | class RequiredWithAll implements ValidatorInterface |
||
17 | { |
||
18 | 2 | public static function validate($value, array $options = [], Datatype $datatype, ?Model $model = null) |
|
19 | { |
||
20 | 2 | if (!$model) { |
|
21 | throw new ValidatorException("RequiredWith needs a model"); |
||
22 | } |
||
23 | |||
24 | 2 | $expectedFields = $options['fields']; |
|
25 | 2 | if (!is_array($expectedFields)) { |
|
26 | $expectedFields = [$expectedFields]; |
||
27 | } |
||
28 | 2 | $found = true; |
|
29 | 2 | $data = $model->getData(); |
|
30 | 2 | foreach ($expectedFields as $ef) { |
|
31 | 2 | if (!array_key_exists($ef, $data)) { |
|
32 | 1 | $found = false; |
|
33 | 1 | break; |
|
34 | } |
||
35 | } |
||
36 | |||
37 | 2 | if ($found && empty($value)) { |
|
38 | 1 | throw new ValidatorException("Field is required when all fields " . implode(',', $expectedFields) . ' are present'); |
|
39 | } |
||
40 | |||
41 | 1 | return $value; |
|
42 | } |
||
43 | |||
44 | 1 | public static function getMetadata(): ValidatorMetadata |
|
54 | ) |
||
55 | ] |
||
59 |