Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
11 / 11 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
| MinLength | |
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 MinLength implements ValueValidateInterface |
| 19 | { |
| 20 | /** @var string */ |
| 21 | protected $messageFormat = 'Invalid value. Please specify a string of at least %s character.'; |
| 22 | |
| 23 | /** @var int */ |
| 24 | protected $length; |
| 25 | |
| 26 | /** |
| 27 | * 新しいインスタンスを構築します |
| 28 | * |
| 29 | * @param int $length 最小文字数を指定します |
| 30 | */ |
| 31 | public function __construct(int $length) |
| 32 | { |
| 33 | if ($length < 0) { |
| 34 | $mes = 'Invalid value. Values less than 0 cannot be specified.'; |
| 35 | throw new LogicException($mes); |
| 36 | } |
| 37 | $this->length = $length; |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * {@inheritDoc} |
| 42 | */ |
| 43 | public function execute($value, ValidateResult $result): ValidateResult |
| 44 | { |
| 45 | // NOTE: string型以外が来たときはバリデーションを行わない |
| 46 | if (!\is_string($value)) { |
| 47 | $mes = 'Skip validation. Executed only when the value is of string type'; |
| 48 | return $result->setResult(true)->setMessage($mes); |
| 49 | } |
| 50 | |
| 51 | if (\mb_strlen($value) < $this->length) { |
| 52 | $mes = \sprintf($this->messageFormat, $this->length); |
| 53 | return $result->setResult(false)->setMessage($mes); |
| 54 | } |
| 55 | return $result->setResult(true); |
| 56 | } |
| 57 | } |