1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Mbright\Validation\Failure; |
4
|
|
|
|
5
|
|
|
class FailureCollection extends \ArrayObject |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* Factory method to return a new Failure object. |
9
|
|
|
* |
10
|
|
|
* @param string $field The field that failed |
11
|
|
|
* @param string $message The failure message |
12
|
|
|
* @param array $args The arguments passed to the rule specification |
13
|
|
|
* |
14
|
|
|
* @return ValidationFailure |
15
|
|
|
*/ |
16
|
42 |
|
protected function newFailure(string $field, string $message, array $args = []): ValidationFailure |
17
|
|
|
{ |
18
|
42 |
|
return new ValidationFailure($field, $message, $args); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Returns bool indicating if the array is empty. |
23
|
|
|
* |
24
|
|
|
* @return bool |
25
|
|
|
*/ |
26
|
54 |
|
public function isEmpty(): bool |
27
|
|
|
{ |
28
|
54 |
|
return count($this) === 0; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Set a failure on a field, removing all previous failures. |
33
|
|
|
* |
34
|
|
|
* @param string $field The field that failed |
35
|
|
|
* @param string $message The failure message |
36
|
|
|
* @param array $args The arguments passed to the rule specification |
37
|
|
|
* |
38
|
|
|
* @return ValidationFailure |
39
|
|
|
*/ |
40
|
9 |
View Code Duplication |
public function set(string $field, string $message, array $args = []): ValidationFailure |
|
|
|
|
41
|
|
|
{ |
42
|
9 |
|
$failure = $this->newFailure($field, $message, $args); |
43
|
9 |
|
$this[$field] = array($failure); |
44
|
|
|
|
45
|
9 |
|
return $failure; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Adds an additional failure on a field. |
50
|
|
|
* |
51
|
|
|
* @param string $field The field that failed |
52
|
|
|
* @param string $message The failure message |
53
|
|
|
* @param array $args The arguments passed to the rule specification |
54
|
|
|
* |
55
|
|
|
* @return ValidationFailure |
56
|
|
|
*/ |
57
|
33 |
View Code Duplication |
public function add(string $field, string $message, array $args = []): ValidationFailure |
|
|
|
|
58
|
|
|
{ |
59
|
33 |
|
$failure = $this->newFailure($field, $message, $args); |
60
|
33 |
|
$this[$field][] = $failure; |
61
|
|
|
|
62
|
33 |
|
return $failure; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Returns all failures for a field. |
67
|
|
|
* |
68
|
|
|
* @param string $field The field name |
69
|
|
|
* |
70
|
|
|
* @return array |
71
|
|
|
*/ |
72
|
9 |
|
public function forField(string $field): array |
73
|
|
|
{ |
74
|
9 |
|
if (!isset($this[$field])) { |
75
|
3 |
|
return []; |
76
|
|
|
} |
77
|
|
|
|
78
|
6 |
|
return $this[$field]; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Returns all failure messages for all fields. |
83
|
|
|
* |
84
|
|
|
* @return array |
85
|
|
|
*/ |
86
|
24 |
|
public function getMessages(): array |
87
|
|
|
{ |
88
|
24 |
|
$messages = []; |
89
|
24 |
|
foreach ($this as $field => $failures) { |
90
|
24 |
|
$messages[$field] = $this->getMessagesForField($field); |
91
|
|
|
} |
92
|
|
|
|
93
|
24 |
|
return $messages; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Returns all failure messages for one field. |
98
|
|
|
* |
99
|
|
|
* @param string $field The field name |
100
|
|
|
* |
101
|
|
|
* @return array |
102
|
|
|
*/ |
103
|
27 |
|
public function getMessagesForField(string $field): array |
104
|
|
|
{ |
105
|
27 |
|
if (!isset($this[$field])) { |
106
|
3 |
|
return []; |
107
|
|
|
} |
108
|
24 |
|
$messages = []; |
109
|
|
|
|
110
|
|
|
/** @var ValidationFailure $failure */ |
111
|
24 |
|
foreach ($this[$field] as $failure) { |
112
|
24 |
|
$messages[] = $failure->getMessage(); |
113
|
|
|
} |
114
|
|
|
|
115
|
24 |
|
return $messages; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Returns a single string of all failure messages for all fields. |
120
|
|
|
* |
121
|
|
|
* @param string $prefix Prefix each line with this string |
122
|
|
|
* |
123
|
|
|
* @return string |
124
|
|
|
*/ |
125
|
3 |
|
public function getMessagesAsString($prefix = ''): string |
126
|
|
|
{ |
127
|
3 |
|
$string = ''; |
128
|
3 |
View Code Duplication |
foreach ($this as $field => $failures) { |
|
|
|
|
129
|
|
|
/** @var ValidationFailure $failure */ |
130
|
3 |
|
foreach ($failures as $failure) { |
131
|
3 |
|
$message = $failure->getMessage(); |
132
|
3 |
|
$string .= "{$prefix}{$field}: {$message}" . PHP_EOL; |
133
|
|
|
} |
134
|
|
|
} |
135
|
3 |
|
return $string; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Returns a single string of all failure messages for one field. |
140
|
|
|
* |
141
|
|
|
* @param string $field The field name |
142
|
|
|
* @param string $prefix Prefix each line with this string |
143
|
|
|
* |
144
|
|
|
* @return string |
145
|
|
|
*/ |
146
|
3 |
|
public function getMessagesForFieldAsstring($field, $prefix = ''): string |
147
|
|
|
{ |
148
|
3 |
|
$string = ''; |
149
|
3 |
View Code Duplication |
foreach ($this->forField($field) as $failure) { |
|
|
|
|
150
|
3 |
|
$message = $failure->getMessage(); |
151
|
3 |
|
$string .= "{$prefix}{$message}" . PHP_EOL; |
152
|
|
|
} |
153
|
3 |
|
return $string; |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.