Skip to content

Commit b00916d

Browse files
committed
Adjusted logical rules
Added validation messages tree
1 parent ccd1006 commit b00916d

File tree

5 files changed

+64
-0
lines changed

5 files changed

+64
-0
lines changed

src/Support/Collection.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff 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
}

src/Validation/Logic/AndRule.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff 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
}

src/Validation/Logic/LogicalRule.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Nuxtifyts\PhpDto\Support\Collection;
66
use Nuxtifyts\PhpDto\Validation\Contracts\RuleEvaluator;
77
use Nuxtifyts\PhpDto\Validation\Contracts\RuleGroup;
8+
use Nuxtifyts\PhpDto\Validation\Rules\ValidationRule;
89

910
abstract 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
}

src/Validation/Logic/OrRule.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff 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
}

src/Validation/Logic/SingularRule.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff 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
}

0 commit comments

Comments
 (0)