1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Marcosh\PhpValidationDSL\Combinator; |
6
|
|
|
|
7
|
|
|
use Marcosh\PhpValidationDSL\Result\ValidationResult; |
8
|
|
|
use Marcosh\PhpValidationDSL\Translator\Translator; |
9
|
|
|
use Marcosh\PhpValidationDSL\Validation; |
10
|
|
|
use Webmozart\Assert\Assert; |
11
|
|
|
use function is_callable; |
12
|
|
|
|
13
|
|
|
final class Any implements Validation |
14
|
|
|
{ |
15
|
|
|
public const NOT_EVEN_ONE = 'any.not-even-one'; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var Validation[] |
19
|
|
|
*/ |
20
|
|
|
private $validations; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var callable with signature $messages -> array |
24
|
|
|
*/ |
25
|
|
|
private $errorFormatter; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param Validation[] $validations |
29
|
|
|
* @param callable|null $errorFormatter |
30
|
|
|
*/ |
31
|
|
|
private function __construct(array $validations, ?callable $errorFormatter = null) |
32
|
|
|
{ |
33
|
|
|
Assert::allIsInstanceOf($validations, Validation::class); |
34
|
|
|
|
35
|
|
|
$this->validations = $validations; |
36
|
|
|
$this->errorFormatter = is_callable($errorFormatter) ? |
37
|
|
|
$errorFormatter : |
38
|
|
|
/** |
39
|
|
|
* @return array[] |
40
|
|
|
* |
41
|
|
|
* @psalm-return array<string, array> |
42
|
|
|
*/ |
43
|
|
|
function (array $messages): array { |
44
|
|
|
return [ |
45
|
|
|
self::NOT_EVEN_ONE => $messages |
46
|
|
|
]; |
47
|
|
|
}; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param Validation[] $validations |
52
|
|
|
* @return self |
53
|
|
|
*/ |
54
|
|
|
public static function validations(array $validations): self |
55
|
|
|
{ |
56
|
|
|
return new self($validations); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param Validation[] $validations |
61
|
|
|
* @param callable $errorFormatter |
62
|
|
|
* @return self |
63
|
|
|
*/ |
64
|
|
|
public static function validationsWithFormatter(array $validations, callable $errorFormatter): self |
65
|
|
|
{ |
66
|
|
|
return new self($validations, $errorFormatter); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public static function validationsWithTranslator(array $validations, Translator $translator): self |
70
|
|
|
{ |
71
|
|
|
return new self( |
72
|
|
|
$validations, |
73
|
|
|
/** |
74
|
|
|
* @return array[] |
75
|
|
|
* |
76
|
|
|
* @psalm-return array<string, array> |
77
|
|
|
*/ |
78
|
|
|
function (array $messages) use ($translator): array { |
79
|
|
|
return [ |
80
|
|
|
$translator->translate(self::NOT_EVEN_ONE) => $messages |
81
|
|
|
]; |
82
|
|
|
} |
83
|
|
|
); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @template T |
88
|
|
|
* @psalm-param T $data |
89
|
|
|
* @param mixed $data |
90
|
|
|
* @param array $context |
91
|
|
|
* @return ValidationResult |
92
|
|
|
*/ |
93
|
|
|
public function validate($data, array $context = []): ValidationResult |
94
|
|
|
{ |
95
|
|
|
$result = ValidationResult::errors([]); |
96
|
|
|
|
97
|
|
|
foreach ($this->validations as $validation) { |
98
|
|
|
$result = $result->meet($validation->validate($data, $context), 'array_merge'); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
return $result |
102
|
|
|
->mapErrors($this->errorFormatter) |
103
|
|
|
->map( |
104
|
|
|
/** |
105
|
|
|
* @return T |
106
|
|
|
*/ |
107
|
|
|
function () use ($data) { |
108
|
|
|
return $data; |
109
|
|
|
} |
110
|
|
|
); |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|