File tree Expand file tree Collapse file tree 5 files changed +64
-0
lines changed Expand file tree Collapse file tree 5 files changed +64
-0
lines changed Original file line number Diff line number Diff line change @@ -54,6 +54,17 @@ public function first(?callable $callable = null): mixed
5454 : array_find ($ this ->items , $ callable );
5555 }
5656
57+ /**
58+ * @template TNewValue of mixed
59+ * @param callable(TValue $item): TNewValue $callable
60+ *
61+ * @return self<TKey, TNewValue>
62+ */
63+ public function map (callable $ callable ): self
64+ {
65+ return new self (array_map ($ callable , $ this ->items ));
66+ }
67+
5768 public function isNotEmpty (): bool
5869 {
5970 return !empty ($ this ->items );
@@ -79,4 +90,12 @@ public function some(callable $callable): bool
7990 {
8091 return array_any ($ this ->items , $ callable );
8192 }
93+
94+ /**
95+ * @return array<TKey, TValue>
96+ */
97+ public function all (): array
98+ {
99+ return $ this ->items ;
100+ }
82101}
Original file line number Diff line number Diff line change @@ -12,4 +12,13 @@ public function evaluate(mixed $value): bool
1212 static fn (RuleEvaluator $ rule ) => $ rule ->evaluate ($ value )
1313 );
1414 }
15+
16+ public function validationMessages (): array
17+ {
18+ return [
19+ 'and ' => $ this ->rules
20+ ->map (self ::resolveValidationMessages (...))
21+ ->all ()
22+ ];
23+ }
1524}
Original file line number Diff line number Diff line change 55use Nuxtifyts \PhpDto \Support \Collection ;
66use Nuxtifyts \PhpDto \Validation \Contracts \RuleEvaluator ;
77use Nuxtifyts \PhpDto \Validation \Contracts \RuleGroup ;
8+ use Nuxtifyts \PhpDto \Validation \Rules \ValidationRule ;
89
910abstract class LogicalRule implements RuleGroup, RuleEvaluator
1011{
@@ -28,4 +29,23 @@ public function addRule(RuleEvaluator $rule): static
2829 $ this ->rules ->push ($ rule );
2930 return $ this ;
3031 }
32+
33+ /**
34+ * @return array<string, mixed>
35+ */
36+ abstract public function validationMessages (): array ;
37+
38+ /**
39+ * @return ?array<string, mixed>
40+ */
41+ protected static function resolveValidationMessages (?RuleEvaluator $ rule ): ?array
42+ {
43+ return match (true ) {
44+ $ rule instanceof LogicalRule => $ rule ->validationMessages (),
45+ $ rule instanceof ValidationRule => [
46+ $ rule ->name => $ rule ->validationMessage ()
47+ ],
48+ default => null
49+ };
50+ }
3151}
Original file line number Diff line number Diff line change @@ -12,4 +12,13 @@ public function evaluate(mixed $value): bool
1212 static fn (RuleEvaluator $ rule ) => $ rule ->evaluate ($ value )
1313 );
1414 }
15+
16+ public function validationMessages (): array
17+ {
18+ return [
19+ 'or ' => $ this ->rules
20+ ->map (self ::resolveValidationMessages (...))
21+ ->all ()
22+ ];
23+ }
1524}
Original file line number Diff line number Diff line change @@ -26,4 +26,11 @@ public function evaluate(mixed $value): bool
2626 {
2727 return (bool ) $ this ->rules ->first ()?->evaluate($ value );
2828 }
29+
30+ public function validationMessages (): array
31+ {
32+ return [
33+ 'singular ' => self ::resolveValidationMessages ($ this ->rules ->first ())
34+ ];
35+ }
2936}
You can’t perform that action at this time.
0 commit comments