1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the awurth/slim-validation package. |
5
|
|
|
* |
6
|
|
|
* (c) Alexis Wurth <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Awurth\SlimValidation; |
13
|
|
|
|
14
|
|
|
use InvalidArgumentException; |
15
|
|
|
use Respect\Validation\Rules\AllOf; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Configuration. |
19
|
|
|
* |
20
|
|
|
* @author Alexis Wurth <[email protected]> |
21
|
|
|
*/ |
22
|
|
|
class Configuration |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var mixed |
26
|
|
|
*/ |
27
|
|
|
protected $default; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
protected $group; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var string |
36
|
|
|
*/ |
37
|
|
|
protected $key; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var string |
41
|
|
|
*/ |
42
|
|
|
protected $message; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var string[] |
46
|
|
|
*/ |
47
|
|
|
protected $messages = []; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var AllOf |
51
|
|
|
*/ |
52
|
|
|
protected $rules; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Constructor. |
56
|
|
|
* |
57
|
|
|
* @param AllOf|array $options |
58
|
|
|
* @param string $key |
59
|
|
|
* @param string $group |
60
|
|
|
* @param string $default |
61
|
|
|
*/ |
62
|
|
|
public function __construct($options, string $key = null, string $group = null, $default = null) |
63
|
|
|
{ |
64
|
|
|
$this->key = $key; |
65
|
|
|
$this->group = $group; |
66
|
|
|
$this->default = $default; |
67
|
|
|
|
68
|
|
|
if ($options instanceof AllOf) { |
69
|
|
|
$this->rules = $options; |
70
|
|
|
} else { |
71
|
|
|
$this->setOptions($options); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$this->validateOptions(); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Gets the default value for non-existent request parameters, object properties or array keys. |
79
|
|
|
* |
80
|
|
|
* @return mixed |
81
|
|
|
*/ |
82
|
|
|
public function getDefault() |
83
|
|
|
{ |
84
|
|
|
return $this->default; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Gets the group to use for errors and values storage. |
89
|
|
|
* |
90
|
|
|
* @return string |
91
|
|
|
*/ |
92
|
|
|
public function getGroup() |
93
|
|
|
{ |
94
|
|
|
return $this->group; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Gets the key to use for errors and values storage. |
99
|
|
|
* |
100
|
|
|
* @return string |
101
|
|
|
*/ |
102
|
|
|
public function getKey() |
103
|
|
|
{ |
104
|
|
|
return $this->key; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Gets the error message. |
109
|
|
|
* |
110
|
|
|
* @return string|null |
111
|
|
|
*/ |
112
|
|
|
public function getMessage() |
113
|
|
|
{ |
114
|
|
|
return $this->message; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Gets individual rules messages. |
119
|
|
|
* |
120
|
|
|
* @return string[] |
121
|
|
|
*/ |
122
|
|
|
public function getMessages(): array |
123
|
|
|
{ |
124
|
|
|
return $this->messages; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Gets the validation rules. |
129
|
|
|
* |
130
|
|
|
* @return AllOf |
131
|
|
|
*/ |
132
|
|
|
public function getValidationRules(): AllOf |
133
|
|
|
{ |
134
|
|
|
return $this->rules; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Tells whether a group has been set. |
139
|
|
|
* |
140
|
|
|
* @return bool |
141
|
|
|
*/ |
142
|
|
|
public function hasGroup(): bool |
143
|
|
|
{ |
144
|
|
|
return !empty($this->group); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Tells whether a key has been set. |
149
|
|
|
* |
150
|
|
|
* @return bool |
151
|
|
|
*/ |
152
|
|
|
public function hasKey(): bool |
153
|
|
|
{ |
154
|
|
|
return !empty($this->key); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Tells whether a single message has been set. |
159
|
|
|
* |
160
|
|
|
* @return bool |
161
|
|
|
*/ |
162
|
|
|
public function hasMessage(): bool |
163
|
|
|
{ |
164
|
|
|
return !empty($this->message); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Tells whether individual rules messages have been set. |
169
|
|
|
* |
170
|
|
|
* @return bool |
171
|
|
|
*/ |
172
|
|
|
public function hasMessages(): bool |
173
|
|
|
{ |
174
|
|
|
return !empty($this->messages); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Sets the default value for non-existent request parameters, object properties or array keys. |
179
|
|
|
* |
180
|
|
|
* @param mixed $default |
181
|
|
|
*/ |
182
|
|
|
public function setDefault($default) |
183
|
|
|
{ |
184
|
|
|
$this->default = $default; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Sets the group to use for errors and values storage. |
189
|
|
|
* |
190
|
|
|
* @param string $group |
191
|
|
|
*/ |
192
|
|
|
public function setGroup(string $group) |
193
|
|
|
{ |
194
|
|
|
$this->group = $group; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* Sets the key to use for errors and values storage. |
199
|
|
|
* |
200
|
|
|
* @param string $key |
201
|
|
|
*/ |
202
|
|
|
public function setKey(string $key) |
203
|
|
|
{ |
204
|
|
|
$this->key = $key; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* Sets the error message. |
209
|
|
|
* |
210
|
|
|
* @param string $message |
211
|
|
|
*/ |
212
|
|
|
public function setMessage(string $message) |
213
|
|
|
{ |
214
|
|
|
$this->message = $message; |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* Sets individual rules messages. |
219
|
|
|
* |
220
|
|
|
* @param string[] $messages |
221
|
|
|
*/ |
222
|
|
|
public function setMessages(array $messages) |
223
|
|
|
{ |
224
|
|
|
$this->messages = $messages; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* Sets options from an array. |
229
|
|
|
* |
230
|
|
|
* @param array $options |
231
|
|
|
*/ |
232
|
|
|
public function setOptions(array $options) |
233
|
|
|
{ |
234
|
|
|
$availableOptions = [ |
235
|
|
|
'default', |
236
|
|
|
'group', |
237
|
|
|
'key', |
238
|
|
|
'message', |
239
|
|
|
'messages', |
240
|
|
|
'rules' |
241
|
|
|
]; |
242
|
|
|
|
243
|
|
|
foreach ($availableOptions as $option) { |
244
|
|
|
if (isset($options[$option])) { |
245
|
|
|
$this->$option = $options[$option]; |
246
|
|
|
} |
247
|
|
|
} |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* Sets the validation rules. |
252
|
|
|
* |
253
|
|
|
* @param AllOf $rules |
254
|
|
|
*/ |
255
|
|
|
public function setValidationRules(AllOf $rules) |
256
|
|
|
{ |
257
|
|
|
$this->rules = $rules; |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* Verifies that all mandatory options are set and valid. |
262
|
|
|
*/ |
263
|
|
|
public function validateOptions() |
264
|
|
|
{ |
265
|
|
|
if (!$this->rules instanceof AllOf) { |
266
|
|
|
throw new InvalidArgumentException('Validation rules are missing or invalid'); |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
if (!$this->hasKey()) { |
270
|
|
|
throw new InvalidArgumentException('A key must be set'); |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
if ($this->hasMessage() && !is_string($this->message)) { |
274
|
|
|
throw new InvalidArgumentException(sprintf('Expected custom message to be of type string, %s given', gettype($this->message))); |
275
|
|
|
} |
276
|
|
|
} |
277
|
|
|
} |
278
|
|
|
|