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 | |||||||
10 | /** |
||||||
11 | * @template B |
||||||
12 | */ |
||||||
13 | abstract class Compare |
||||||
14 | { |
||||||
15 | public const MESSAGE = 'composing-bound.not-respecting-bound'; |
||||||
16 | |||||||
17 | /** @var mixed could be any comparable type */ |
||||||
18 | protected $comparisonBasis; |
||||||
19 | |||||||
20 | /** |
||||||
21 | * @var callable with signature $comparisonBasis -> $data -> string[] |
||||||
22 | */ |
||||||
23 | private $errorFormatter; |
||||||
24 | |||||||
25 | /** |
||||||
26 | * @param mixed $comparisonBasis |
||||||
27 | * @psalm-param B $comparisonBasis |
||||||
28 | * @param callable $errorFormatter |
||||||
29 | */ |
||||||
30 | private function __construct($comparisonBasis, callable $errorFormatter) |
||||||
31 | { |
||||||
32 | $this->comparisonBasis = $comparisonBasis; |
||||||
33 | $this->errorFormatter = $errorFormatter; |
||||||
34 | } |
||||||
35 | |||||||
36 | /** |
||||||
37 | * @psalm-param B $comparisonBasis |
||||||
38 | * @param mixed $comparisonBasis |
||||||
39 | * @return Compare |
||||||
40 | */ |
||||||
41 | public static function withBound($comparisonBasis): self |
||||||
42 | { |
||||||
43 | return self::withBoundAndFormatter( |
||||||
44 | $comparisonBasis, |
||||||
45 | /** |
||||||
46 | * @param mixed $comparisonBasis |
||||||
47 | * @psalm-param B $comparisonBasis |
||||||
48 | * @param mixed $data |
||||||
49 | * @psalm-param B $data |
||||||
50 | * @return string[] |
||||||
51 | * @psalm-return array{0:mixed} |
||||||
52 | */ |
||||||
53 | function ($comparisonBasis, $data): array { |
||||||
0 ignored issues
–
show
The parameter
$comparisonBasis 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. ![]() |
|||||||
54 | |||||||
55 | return [static::MESSAGE]; |
||||||
56 | } |
||||||
57 | ); |
||||||
58 | } |
||||||
59 | |||||||
60 | /** |
||||||
61 | * @param mixed $comparisonBasis |
||||||
62 | * @psalm-param B $comparisonBasis |
||||||
63 | * @param callable $errorFormatter |
||||||
64 | * @return Compare |
||||||
65 | */ |
||||||
66 | public static function withBoundAndFormatter($comparisonBasis, callable $errorFormatter): self |
||||||
67 | { |
||||||
68 | return new static($comparisonBasis, $errorFormatter); |
||||||
69 | } |
||||||
70 | |||||||
71 | /** |
||||||
72 | * @param mixed $comparisonBasis |
||||||
73 | * @psalm-param B $comparisonBasis |
||||||
74 | * @param Translator $translator |
||||||
75 | * @return Compare |
||||||
76 | */ |
||||||
77 | public static function withBoundAndTranslator($comparisonBasis, Translator $translator): self |
||||||
78 | { |
||||||
79 | return self::withBoundAndFormatter( |
||||||
80 | $comparisonBasis, |
||||||
81 | /** |
||||||
82 | * @param mixed $comparisonBasis |
||||||
83 | * @psalm-param B $comparisonBasis |
||||||
84 | * @param mixed $data |
||||||
85 | * @psalm-param B $data |
||||||
86 | * @return string[] |
||||||
87 | * @psalm-return array{0:mixed} |
||||||
88 | */ |
||||||
89 | function ($comparisonBasis, $data) use ($translator): array { |
||||||
0 ignored issues
–
show
The parameter
$comparisonBasis 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
$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. ![]() |
|||||||
90 | return [$translator->translate(static::MESSAGE)]; |
||||||
91 | } |
||||||
92 | ); |
||||||
93 | } |
||||||
94 | |||||||
95 | /** |
||||||
96 | * @param mixed $data |
||||||
97 | * @param array $context |
||||||
98 | * @return ValidationResult |
||||||
99 | */ |
||||||
100 | abstract public function validate($data, array $context = []): ValidationResult; |
||||||
101 | |||||||
102 | /** |
||||||
103 | * @param callable $assertion |
||||||
104 | * @psalm-param callable(B, B):bool $assertion |
||||||
105 | * @param mixed $data |
||||||
106 | * @psalm-param B $data |
||||||
107 | * @param array $context |
||||||
108 | * @return ValidationResult |
||||||
109 | */ |
||||||
110 | protected function validateAssertion(callable $assertion, $data, array $context = []): ValidationResult |
||||||
111 | { |
||||||
112 | $comparisonBasis = $this->comparisonBasis; |
||||||
113 | $errorFormatter = $this->errorFormatter; |
||||||
114 | |||||||
115 | return IsAsAsserted::withAssertionAndErrorFormatter( |
||||||
116 | /** |
||||||
117 | * @param mixed $data |
||||||
118 | * @psalm-param B $data |
||||||
119 | */ |
||||||
120 | function ($data) use ($comparisonBasis, $assertion): bool { |
||||||
121 | return $assertion($comparisonBasis, $data); |
||||||
122 | }, |
||||||
123 | /** |
||||||
124 | * @param mixed $data |
||||||
125 | * @psalm-param B $data |
||||||
126 | */ |
||||||
127 | function ($data) use ($comparisonBasis, $errorFormatter): array { |
||||||
128 | return $errorFormatter($comparisonBasis, $data); |
||||||
129 | } |
||||||
130 | )->validate($data, $context); |
||||||
131 | } |
||||||
132 | } |
||||||
133 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.