Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
8 / 8 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
| ArrayType | |
100.00% |
8 / 8 |
|
100.00% |
2 / 2 |
4 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| execute | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
3 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * @license MIT |
| 5 | * @author hazuki3417<hazuki3417@gmail.com> |
| 6 | * @copyright 2022 hazuki3417 all rights reserved. |
| 7 | */ |
| 8 | |
| 9 | namespace Selen\MongoDB\Validator\Attributes; |
| 10 | |
| 11 | use Attribute; |
| 12 | use Selen\Data\ArrayTypes; |
| 13 | use Selen\MongoDB\Validator\Model\ValidateResult; |
| 14 | use Selen\MongoDB\Validator\ValueValidateInterface; |
| 15 | |
| 16 | #[Attribute(Attribute::TARGET_PROPERTY)] |
| 17 | class ArrayType implements ValueValidateInterface |
| 18 | { |
| 19 | /** @var string[] */ |
| 20 | private $names; |
| 21 | |
| 22 | public function __construct(string ...$names) |
| 23 | { |
| 24 | $this->names = $names; |
| 25 | } |
| 26 | |
| 27 | public function execute($value, ValidateResult $result): ValidateResult |
| 28 | { |
| 29 | $format = 'Invalid type. expected array element type %s.'; |
| 30 | $mes = \sprintf($format, \implode(', ', $this->names)); |
| 31 | |
| 32 | if (!\is_array($value)) { |
| 33 | return $result->setResult(false)->setMessage($mes); |
| 34 | } |
| 35 | |
| 36 | if (!ArrayTypes::validate($value, ...$this->names)) { |
| 37 | return $result->setResult(false)->setMessage($mes); |
| 38 | } |
| 39 | return $result->setResult(true); |
| 40 | } |
| 41 | } |