Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
8 / 8 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
| Regex | |
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\Schema\Validate\Values; |
| 10 | |
| 11 | use Selen\Schema\Validate\Model\ValidateResult; |
| 12 | use Selen\Schema\Validate\ValueValidateInterface; |
| 13 | |
| 14 | class Regex implements ValueValidateInterface |
| 15 | { |
| 16 | /** @var string */ |
| 17 | protected $messageFormat = 'Invalid value. expected value pattern %s.'; |
| 18 | |
| 19 | /** @var string */ |
| 20 | private $pattern; |
| 21 | |
| 22 | public function __construct(string $pattern) |
| 23 | { |
| 24 | $this->pattern = $pattern; |
| 25 | } |
| 26 | |
| 27 | public function execute($value, ValidateResult $result): ValidateResult |
| 28 | { |
| 29 | // NOTE: string型以外が来たときはバリデーションを行わない |
| 30 | if (!\is_string($value)) { |
| 31 | $mes = 'Skip validation. Executed only when the value is of string type'; |
| 32 | return $result->setResult(true)->setMessage($mes); |
| 33 | } |
| 34 | |
| 35 | if (!\preg_match("/{$this->pattern}/", $value)) { |
| 36 | $mes = \sprintf($this->messageFormat, $this->pattern); |
| 37 | return $result->setResult(false)->setMessage($mes); |
| 38 | } |
| 39 | return $result->setResult(true); |
| 40 | } |
| 41 | } |