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