Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
6 / 6 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
Type | |
100.00% |
6 / 6 |
|
100.00% |
2 / 2 |
3 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
execute | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
2 |
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\Types; |
13 | use Selen\MongoDB\Validator\Model\ValidateResult; |
14 | use Selen\MongoDB\Validator\ValueValidateInterface; |
15 | |
16 | #[Attribute(Attribute::TARGET_PROPERTY)] |
17 | class Type 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 | if (Types::validate($value, ...$this->names)) { |
30 | return $result->setResult(true); |
31 | } |
32 | |
33 | $format = 'Invalid type. expected type %s.'; |
34 | $mes = \sprintf($format, \implode(', ', $this->names)); |
35 | return $result->setResult(false)->setMessage($mes); |
36 | } |
37 | } |