| Conditions | 7 |
| Paths | 7 |
| Total Lines | 19 |
| Code Lines | 12 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 12 |
| CRAP Score | 7.0222 |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
| 1 | <?php declare(strict_types=1); |
||
| 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 | $found = false; |
|
| 26 | 2 | $data = $model->getData(); |
|
| 27 | 2 | foreach ($expectedFields as $ef) { |
|
| 28 | 2 | if (array_key_exists($ef, $data) && !empty($data[$ef])) { |
|
| 29 | 2 | $found = true; |
|
| 30 | 2 | break; |
|
| 31 | } |
||
| 32 | } |
||
| 33 | 2 | if ($found && empty($value)) { |
|
| 34 | 1 | throw new ValidatorException("Field is required when at least one of fields " . implode(',', $expectedFields) . ' are present'); |
|
| 35 | } |
||
| 36 | 1 | return $value; |
|
| 37 | } |
||
| 54 |