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 | |||||
| 12 | final class IsAsAsserted implements Validation |
||||
| 13 | { |
||||
| 14 | public const NOT_AS_ASSERTED = 'is-as-asserted.not-as-asserted'; |
||||
| 15 | |||||
| 16 | /** |
||||
| 17 | * @var callable with signature $data -> bool |
||||
| 18 | */ |
||||
| 19 | private $assertion; |
||||
| 20 | |||||
| 21 | /** |
||||
| 22 | * @var callable with signature $data -> string[] |
||||
| 23 | */ |
||||
| 24 | private $errorFormatter; |
||||
| 25 | |||||
| 26 | private function __construct(callable $assertion, ?callable $errorFormatter = null) |
||||
| 27 | { |
||||
| 28 | $this->assertion = $assertion; |
||||
| 29 | $this->errorFormatter = is_callable($errorFormatter) ? |
||||
| 30 | $errorFormatter : |
||||
| 31 | /** |
||||
| 32 | * @template T |
||||
| 33 | * @param mixed $data |
||||
| 34 | * @psalm-param T $data |
||||
| 35 | * @return string[] |
||||
| 36 | * @psalm-return array{0:string} |
||||
| 37 | */ |
||||
| 38 | function ($data): array { |
||||
|
0 ignored issues
–
show
|
|||||
| 39 | return [self::NOT_AS_ASSERTED]; |
||||
| 40 | }; |
||||
| 41 | } |
||||
| 42 | |||||
| 43 | public static function withAssertion(callable $assertion): self |
||||
| 44 | { |
||||
| 45 | return new self($assertion); |
||||
| 46 | } |
||||
| 47 | |||||
| 48 | public static function withAssertionAndErrorFormatter(callable $assertion, callable $errorFormatter): self |
||||
| 49 | { |
||||
| 50 | return new self($assertion, $errorFormatter); |
||||
| 51 | } |
||||
| 52 | |||||
| 53 | public static function withAssertionAndTranslator(callable $assertion, Translator $translator): self |
||||
| 54 | { |
||||
| 55 | return new self( |
||||
| 56 | $assertion, |
||||
| 57 | /** |
||||
| 58 | * @template T |
||||
| 59 | * @param mixed $data |
||||
| 60 | * @psalm-param T $data |
||||
| 61 | * @return string[] |
||||
| 62 | * @psalm-return array{0:string} |
||||
| 63 | */ |
||||
| 64 | function ($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...
|
|||||
| 65 | return [$translator->translate(self::NOT_AS_ASSERTED)]; |
||||
| 66 | } |
||||
| 67 | ); |
||||
| 68 | } |
||||
| 69 | |||||
| 70 | public function validate($data, array $context = []): ValidationResult |
||||
| 71 | { |
||||
| 72 | if (! ($this->assertion)($data)) { |
||||
| 73 | return ValidationResult::errors(($this->errorFormatter)($data)); |
||||
| 74 | } |
||||
| 75 | |||||
| 76 | return ValidationResult::valid($data); |
||||
| 77 | } |
||||
| 78 | } |
||||
| 79 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.