1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of Dimtrovich/Validation. |
5
|
|
|
* |
6
|
|
|
* (c) 2023 Dimitri Sitchet Tomkeu <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view |
9
|
|
|
* the LICENSE file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Dimtrovich\Validation; |
13
|
|
|
|
14
|
|
|
use ArrayAccess; |
15
|
|
|
use ArrayIterator; |
16
|
|
|
use BlitzPHP\Contracts\Support\Arrayable; |
17
|
|
|
use IteratorAggregate; |
18
|
|
|
use Rakit\Validation\ErrorBag as RakitErrorBag; |
19
|
|
|
use Traversable; |
20
|
|
|
|
21
|
|
|
class ErrorBag extends RakitErrorBag implements Arrayable, ArrayAccess, IteratorAggregate |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* Returns key validation errors as a string |
25
|
|
|
*/ |
26
|
|
|
public function line(string $key, string $separator = ', ', string $format = ':message'): ?string |
27
|
|
|
{ |
28
|
|
|
if ([] === $errors = $this->get($key, $format)) { |
29
|
|
|
return null; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
return implode($separator, $errors); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* {@inheritDoc} |
37
|
|
|
*/ |
38
|
|
|
public function get(string $key, string $format = ':message'): array |
39
|
|
|
{ |
40
|
|
|
[$key, $ruleName] = $this->parsekey($key); |
41
|
|
|
$results = []; |
42
|
|
|
if ($this->isWildcardKey($key)) { |
43
|
|
|
$messages = $this->filterMessagesForWildcardKey($key, $ruleName); |
44
|
|
|
|
45
|
|
|
foreach ($messages as $explicitKey => $keyMessages) { |
46
|
|
|
foreach ($keyMessages as $rule => $message) { |
47
|
|
|
$results[$explicitKey][$rule] = $this->formatMessage($message, $format); |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
} else { |
51
|
|
|
$keyMessages = $this->messages[$key] ?? []; |
52
|
|
|
|
53
|
|
|
foreach ((array) $keyMessages as $rule => $message) { |
54
|
|
|
if ($ruleName && $ruleName !== $rule) { |
55
|
|
|
continue; |
56
|
|
|
} |
57
|
|
|
$results[$rule] = $this->formatMessage($message, $format); |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
return $results; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* {@inheritDoc} |
66
|
|
|
*/ |
67
|
|
|
public function all(string $format = ':message'): array |
68
|
|
|
{ |
69
|
|
|
$messages = $this->messages; |
70
|
|
|
|
71
|
|
|
$results = []; |
72
|
|
|
|
73
|
|
|
foreach ($messages as $key => $keyMessages) { |
74
|
|
|
foreach ((array) $keyMessages as $message) { |
75
|
|
|
$results[] = $this->formatMessage($message, $format); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
return $results; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Creates a new error bag from base errors |
84
|
|
|
*/ |
85
|
|
|
public static function fromBase(RakitErrorBag $bag): static |
86
|
|
|
{ |
87
|
2 |
|
return new static($bag->toArray()); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* {@inheritDoc} |
92
|
|
|
*/ |
93
|
|
|
protected function filterMessagesForWildcardKey(string $key, $ruleName = null): array |
94
|
|
|
{ |
95
|
|
|
$messages = $this->messages; |
96
|
|
|
$pattern = preg_quote($key, '#'); |
97
|
|
|
$pattern = str_replace('\*', '.*', $pattern); |
98
|
|
|
|
99
|
|
|
$filteredMessages = []; |
100
|
|
|
|
101
|
|
|
foreach ($messages as $k => $keyMessages) { |
102
|
|
|
if ((bool) preg_match('#^' . $pattern . '\z#u', $k) === false) { |
103
|
|
|
continue; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
foreach ((array) $keyMessages as $rule => $message) { |
107
|
|
|
if ($ruleName && $rule !== $ruleName) { |
108
|
|
|
continue; |
109
|
|
|
} |
110
|
|
|
$filteredMessages[$k][$rule] = $message; |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
return $filteredMessages; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* {@inheritDoc} |
119
|
|
|
*/ |
120
|
|
|
public function offsetExists(mixed $offset): bool |
121
|
|
|
{ |
122
|
|
|
return $this->has((string) $offset); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* {@inheritDoc} |
127
|
|
|
*/ |
128
|
|
|
public function offsetGet(mixed $offset): mixed |
129
|
|
|
{ |
130
|
|
|
return $this->get((string) $offset); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* {@inheritDoc} |
135
|
|
|
*/ |
136
|
|
|
public function offsetSet(mixed $offset, mixed $value): void |
137
|
|
|
{ |
138
|
|
|
$this->messages[$offset] = $value; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* {@inheritDoc} |
143
|
|
|
*/ |
144
|
|
|
public function offsetUnset(mixed $offset): void |
145
|
|
|
{ |
146
|
|
|
unset($this->messages[$offset]); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* {@inheritDoc} |
151
|
|
|
*/ |
152
|
|
|
public function getIterator(): Traversable |
153
|
|
|
{ |
154
|
|
|
return new ArrayIterator($this->messages); |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|