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