Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
17 / 17 |
|
100.00% |
1 / 1 |
CRAP | |
100.00% |
1 / 1 |
| ValidatorEvaluationTrait | |
100.00% |
17 / 17 |
|
100.00% |
1 / 1 |
11 | |
100.00% |
1 / 1 |
| evaluateValidator | |
100.00% |
17 / 17 |
|
100.00% |
1 / 1 |
11 | |||
| 1 | <?php |
| 2 | declare(strict_types=1); |
| 3 | /** |
| 4 | * Pop PHP Framework (https://www.popphp.org/) |
| 5 | * |
| 6 | * @link https://github.com/popphp/popphp-framework |
| 7 | * @author Nick Sagona, III <nick@popphp.org> |
| 8 | * @copyright Copyright (c) 2009-2026 Nick Sagona, III |
| 9 | * @license https://www.popphp.org/license New BSD License |
| 10 | */ |
| 11 | |
| 12 | /** |
| 13 | * @namespace |
| 14 | */ |
| 15 | namespace Pop\Form; |
| 16 | |
| 17 | /** |
| 18 | * Validator evaluation trait |
| 19 | * |
| 20 | * @category Pop |
| 21 | * @package Pop\Form |
| 22 | * @author Nick Sagona, III <nick@popphp.org> |
| 23 | * @copyright Copyright (c) 2009-2026 Nick Sagona, III |
| 24 | * @license https://www.popphp.org/license New BSD License |
| 25 | * @version 5.0.0 |
| 26 | */ |
| 27 | trait ValidatorEvaluationTrait |
| 28 | { |
| 29 | |
| 30 | /** |
| 31 | * Evaluate a validator (a Pop\Validator\ValidatorInterface instance or a callable) |
| 32 | * against a value and return any resulting error messages |
| 33 | * |
| 34 | * @param mixed $validator |
| 35 | * @param mixed $value |
| 36 | * @param array $formValues |
| 37 | * @return array |
| 38 | */ |
| 39 | protected function evaluateValidator(mixed $validator, mixed $value, array $formValues = []): array |
| 40 | { |
| 41 | $messages = []; |
| 42 | |
| 43 | if ($validator instanceof \Pop\Validator\ValidatorInterface) { |
| 44 | if (!$validator->evaluate($value)) { |
| 45 | $messages[] = $validator->getMessage(); |
| 46 | } |
| 47 | } else if (is_callable($validator)) { |
| 48 | $result = call_user_func_array($validator, [$value, $formValues]); |
| 49 | if ($result instanceof \Pop\Validator\ValidatorInterface) { |
| 50 | if (!$result->evaluate($value)) { |
| 51 | $messages[] = $result->getMessage(); |
| 52 | } |
| 53 | } else if (is_array($result)) { |
| 54 | foreach ($result as $val) { |
| 55 | if ($val instanceof \Pop\Validator\ValidatorInterface) { |
| 56 | if (!$val->evaluate($value)) { |
| 57 | $messages[] = $val->getMessage(); |
| 58 | } |
| 59 | } |
| 60 | } |
| 61 | } else if ($result !== null) { |
| 62 | $messages[] = $result; |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | return $messages; |
| 67 | } |
| 68 | |
| 69 | } |