|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
/** |
|
5
|
|
|
* |
|
6
|
|
|
* This file is part of Aura for PHP. |
|
7
|
|
|
* |
|
8
|
|
|
* @license http://opensource.org/licenses/mit-license.php MIT |
|
9
|
|
|
* |
|
10
|
|
|
*/ |
|
11
|
|
|
namespace Aura\Filter\Failure; |
|
12
|
|
|
|
|
13
|
|
|
use ArrayObject; |
|
14
|
|
|
use Aura\Filter_Interface\FailureInterface; |
|
15
|
|
|
use Aura\Filter_Interface\FailureCollectionInterface; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* |
|
19
|
|
|
* A collection of Failure objects. |
|
20
|
|
|
* |
|
21
|
|
|
* @package Aura.Filter |
|
22
|
|
|
* |
|
23
|
|
|
*/ |
|
24
|
|
|
class FailureCollection extends ArrayObject implements FailureCollectionInterface |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* |
|
28
|
|
|
* Constructor. |
|
29
|
|
|
* |
|
30
|
|
|
* @return self |
|
31
|
|
|
* |
|
32
|
|
|
*/ |
|
33
|
|
|
public function __construct() |
|
34
|
|
|
{ |
|
35
|
|
|
parent::__construct(array()); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* |
|
40
|
|
|
* Is the failure collection empty? |
|
41
|
|
|
* |
|
42
|
|
|
* |
|
43
|
|
|
*/ |
|
44
|
|
|
public function isEmpty(): bool |
|
45
|
|
|
{ |
|
46
|
|
|
return count($this) === 0; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* |
|
51
|
|
|
* Set a failure on a field, removing all previous failures. |
|
52
|
|
|
* |
|
53
|
|
|
* @param string $field The field that failed. |
|
54
|
|
|
* |
|
55
|
|
|
* @param string $message The failure message. |
|
56
|
|
|
* |
|
57
|
|
|
* @param array $args The arguments passed to the rule specification. |
|
58
|
|
|
* |
|
59
|
|
|
* |
|
60
|
|
|
*/ |
|
61
|
|
|
public function set(string $field, string $message, array $args = array()): FailureInterface |
|
62
|
|
|
{ |
|
63
|
|
|
$failure = $this->newFailure($field, $message, $args); |
|
64
|
|
|
$this[$field] = array($failure); |
|
65
|
|
|
return $failure; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* |
|
70
|
|
|
* Adds an additional failure on a field. |
|
71
|
|
|
* |
|
72
|
|
|
* @param string $field The field that failed. |
|
73
|
|
|
* |
|
74
|
|
|
* @param string $message The failure message. |
|
75
|
|
|
* |
|
76
|
|
|
* @param array $args The arguments passed to the rule specification. |
|
77
|
|
|
* |
|
78
|
|
|
*/ |
|
79
|
|
|
public function add(string $field, string $message, array $args = array()): FailureInterface |
|
80
|
|
|
{ |
|
81
|
|
|
$failure = $this->newFailure($field, $message, $args); |
|
82
|
|
|
$this[$field][] = $failure; |
|
83
|
|
|
return $failure; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* |
|
88
|
|
|
* Factory method to return a new Failure object. |
|
89
|
|
|
* |
|
90
|
|
|
* @param string $field The field that failed. |
|
91
|
|
|
* |
|
92
|
|
|
* @param string $message The failure message. |
|
93
|
|
|
* |
|
94
|
|
|
* @param array $args The arguments passed to the rule specification. |
|
95
|
|
|
* |
|
96
|
|
|
* |
|
97
|
|
|
*/ |
|
98
|
|
|
protected function newFailure(string $field, string $message, array $args = array()): FailureInterface |
|
99
|
|
|
{ |
|
100
|
|
|
return new Failure($field, $message, $args); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* |
|
105
|
|
|
* Returns all failures for a field. |
|
106
|
|
|
* |
|
107
|
|
|
* @param string $field The field name. |
|
108
|
|
|
* |
|
109
|
|
|
* |
|
110
|
|
|
* @return array<int, Failure> |
|
111
|
|
|
*/ |
|
112
|
|
|
public function forField(string $field): array |
|
113
|
|
|
{ |
|
114
|
|
|
if (! isset($this[$field])) { |
|
115
|
|
|
return array(); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
return $this[$field]; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* |
|
123
|
|
|
* Returns all failure messages for all fields. |
|
124
|
|
|
* |
|
125
|
|
|
* |
|
126
|
|
|
* @return array<string, array<int, string>> |
|
127
|
|
|
*/ |
|
128
|
|
|
public function getMessages(): array |
|
129
|
|
|
{ |
|
130
|
|
|
$messages = array(); |
|
131
|
|
|
foreach ($this as $field => $failures) { |
|
132
|
|
|
$messages[$field] = $this->getMessagesForField($field); |
|
133
|
|
|
} |
|
134
|
|
|
return $messages; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* |
|
139
|
|
|
* Returns all failure messages for one field. |
|
140
|
|
|
* |
|
141
|
|
|
* @param string $field The field name. |
|
142
|
|
|
* |
|
143
|
|
|
* @return array<int, string> |
|
144
|
|
|
* |
|
145
|
|
|
*/ |
|
146
|
|
|
public function getMessagesForField(string $field): array |
|
147
|
|
|
{ |
|
148
|
|
|
if (! isset($this[$field])) { |
|
149
|
|
|
return array(); |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
$messages = array(); |
|
153
|
|
|
|
|
154
|
|
|
if ($this[$field] instanceof FailureCollection) { |
|
155
|
|
|
$messages = $this[$field]->getMessages(); |
|
156
|
|
|
} else { |
|
157
|
|
|
foreach ($this[$field] as $failure) { |
|
158
|
|
|
$messages[] = $failure->getMessage(); |
|
159
|
|
|
} |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
return $messages; |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* |
|
167
|
|
|
* Returns a single string of all failure messages for all fields. |
|
168
|
|
|
* |
|
169
|
|
|
* @param string $prefix Prefix each line with this string. |
|
170
|
|
|
* |
|
171
|
|
|
* |
|
172
|
|
|
*/ |
|
173
|
|
|
public function getMessagesAsString(string $prefix = ''): string |
|
174
|
|
|
{ |
|
175
|
|
|
$string = ''; |
|
176
|
|
|
foreach ($this as $field => $failures) { |
|
177
|
|
|
foreach ($failures as $failure) { |
|
178
|
|
|
$message = $failure->getMessage(); |
|
179
|
|
|
$string .= "{$prefix}{$field}: {$message}" . PHP_EOL; |
|
180
|
|
|
} |
|
181
|
|
|
} |
|
182
|
|
|
return $string; |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
/** |
|
186
|
|
|
* |
|
187
|
|
|
* Returns a single string of all failure messages for one field. |
|
188
|
|
|
* |
|
189
|
|
|
* @param string $field The field name. |
|
190
|
|
|
* |
|
191
|
|
|
* @param string $prefix Prefix each line with this string. |
|
192
|
|
|
* |
|
193
|
|
|
* |
|
194
|
|
|
*/ |
|
195
|
|
|
public function getMessagesForFieldAsString(string $field, string $prefix = ''): string |
|
196
|
|
|
{ |
|
197
|
|
|
$string = ''; |
|
198
|
|
|
foreach ($this->forField($field) as $failure) { |
|
199
|
|
|
$message = $failure->getMessage(); |
|
200
|
|
|
$string .= "{$prefix}{$message}" . PHP_EOL; |
|
201
|
|
|
} |
|
202
|
|
|
return $string; |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
public function addSubfieldFailures($field, $spec) |
|
206
|
|
|
{ |
|
207
|
|
|
$failure_collection = new FailureCollection(); |
|
208
|
|
|
|
|
209
|
|
|
foreach ($spec->getMessage() as $f => $messages) { |
|
210
|
|
|
foreach ($messages as $message) { |
|
211
|
|
|
$failure_collection->add($f, $message, $spec->getArgs()); |
|
212
|
|
|
} |
|
213
|
|
|
} |
|
214
|
|
|
|
|
215
|
|
|
$this[$field] = $failure_collection; |
|
216
|
|
|
} |
|
217
|
|
|
} |
|
218
|
|
|
|