Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
11 / 11 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
MinSize | |
100.00% |
11 / 11 |
|
100.00% |
2 / 2 |
5 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
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\Schema\Validate\Values; |
10 | |
11 | use LogicException; |
12 | use Selen\Schema\Validate\Model\ValidateResult; |
13 | use Selen\Schema\Validate\ValueValidateInterface; |
14 | |
15 | /** |
16 | * 配列の最小要素数をバリデーションするクラス |
17 | */ |
18 | class MinSize implements ValueValidateInterface |
19 | { |
20 | /** @var string */ |
21 | protected $messageFormat = 'Invalid value. Please specify an array of %s or more elements.'; |
22 | |
23 | /** @var int */ |
24 | protected $size; |
25 | |
26 | /** |
27 | * 新しいインスタンスを構築します |
28 | * |
29 | * @param int $size 最小要素数を指定します |
30 | */ |
31 | public function __construct(int $size) |
32 | { |
33 | if ($size < 0) { |
34 | $mes = 'Invalid value. Values less than 0 cannot be specified.'; |
35 | throw new LogicException($mes); |
36 | } |
37 | $this->size = $size; |
38 | } |
39 | |
40 | /** |
41 | * {@inheritDoc} |
42 | */ |
43 | public function execute($value, ValidateResult $result): ValidateResult |
44 | { |
45 | // NOTE: 配列型以外が来たときはバリデーションを行わない |
46 | if (!\is_array($value)) { |
47 | $mes = 'Skip validation. Executed only when the value is of array type'; |
48 | return $result->setResult(true)->setMessage($mes); |
49 | } |
50 | |
51 | if (\count($value) < $this->size) { |
52 | $mes = \sprintf($this->messageFormat, $this->size); |
53 | return $result->setResult(false)->setMessage($mes); |
54 | } |
55 | return $result->setResult(true); |
56 | } |
57 | } |