marcosh /
php-validation-dsl
| 1 | <?php |
||||||
| 2 | |||||||
| 3 | declare(strict_types=1); |
||||||
| 4 | |||||||
| 5 | namespace Marcosh\PhpValidationDSL\Basic; |
||||||
| 6 | |||||||
| 7 | use Marcosh\PhpValidationDSL\Result\ValidationResult; |
||||||
| 8 | use Marcosh\PhpValidationDSL\Translator\Translator; |
||||||
| 9 | use Marcosh\PhpValidationDSL\Validation; |
||||||
| 10 | use function is_callable; |
||||||
| 11 | use function preg_match; |
||||||
| 12 | |||||||
| 13 | final class Regex implements Validation |
||||||
| 14 | { |
||||||
| 15 | public const MESSAGE = 'regex.match-failed'; |
||||||
| 16 | |||||||
| 17 | /** |
||||||
| 18 | * @var string |
||||||
| 19 | */ |
||||||
| 20 | private $pattern; |
||||||
| 21 | |||||||
| 22 | /** |
||||||
| 23 | * @var callable with signature $pattern -> $data -> string[] |
||||||
| 24 | */ |
||||||
| 25 | private $errorFormatter; |
||||||
| 26 | |||||||
| 27 | private function __construct(string $pattern, ?callable $errorFormatter = null) |
||||||
| 28 | { |
||||||
| 29 | $this->pattern = $pattern; |
||||||
| 30 | $this->errorFormatter = is_callable($errorFormatter) ? |
||||||
| 31 | $errorFormatter : |
||||||
| 32 | /** |
||||||
| 33 | * @template T |
||||||
| 34 | * @param string $pattern |
||||||
| 35 | * @param mixed $data |
||||||
| 36 | * @psalm-param T $data |
||||||
| 37 | * @return string[] |
||||||
| 38 | * @psalm-return array{0:string} |
||||||
| 39 | */ |
||||||
| 40 | function (string $pattern, $data): array { |
||||||
|
0 ignored issues
–
show
The parameter
$data is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. Loading history...
|
|||||||
| 41 | return [self::MESSAGE]; |
||||||
| 42 | }; |
||||||
| 43 | } |
||||||
| 44 | |||||||
| 45 | public static function withPattern(string $pattern): self |
||||||
| 46 | { |
||||||
| 47 | return new self($pattern); |
||||||
| 48 | } |
||||||
| 49 | |||||||
| 50 | public static function withPatternAndFormatter(string $pattern, callable $errorFormatter): self |
||||||
| 51 | { |
||||||
| 52 | return new self($pattern, $errorFormatter); |
||||||
| 53 | } |
||||||
| 54 | |||||||
| 55 | public static function withPatternAndTranslator(string $pattern, Translator $translator): self |
||||||
| 56 | { |
||||||
| 57 | return new self( |
||||||
| 58 | $pattern, |
||||||
| 59 | /** |
||||||
| 60 | * @template T |
||||||
| 61 | * @param string $pattern |
||||||
| 62 | * @param mixed $data |
||||||
| 63 | * @psalm-param T $data |
||||||
| 64 | * @return string[] |
||||||
| 65 | * @psalm-return array{0:string} |
||||||
| 66 | */ |
||||||
| 67 | function (string $pattern, $data) use ($translator): array { |
||||||
|
0 ignored issues
–
show
The parameter
$data is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. Loading history...
The parameter
$pattern is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. Loading history...
|
|||||||
| 68 | return [$translator->translate(self::MESSAGE)]; |
||||||
| 69 | } |
||||||
| 70 | ); |
||||||
| 71 | } |
||||||
| 72 | |||||||
| 73 | public function validate($data, array $context = []): ValidationResult |
||||||
| 74 | { |
||||||
| 75 | $match = preg_match($this->pattern, $data); |
||||||
| 76 | |||||||
| 77 | if (false === $match || 0 === $match) { |
||||||
| 78 | return ValidationResult::errors(($this->errorFormatter)($this->pattern, $data)); |
||||||
| 79 | } |
||||||
| 80 | |||||||
| 81 | return ValidationResult::valid($data); |
||||||
| 82 | } |
||||||
| 83 | } |
||||||
| 84 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.