1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace IndraGunawan\RestService\Validator; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException; |
6
|
|
|
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition; |
7
|
|
|
use Symfony\Component\Config\Definition\Builder\TreeBuilder; |
8
|
|
|
use Symfony\Component\Config\Definition\ConfigurationInterface; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* This is the class that validates and merges configuration. |
12
|
|
|
*/ |
13
|
|
|
class SpecificationConfiguration implements ConfigurationInterface |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* {@inheritdoc} |
17
|
|
|
*/ |
18
|
6 |
|
public function getConfigTreeBuilder() |
19
|
|
|
{ |
20
|
6 |
|
$treeBuilder = new TreeBuilder(); |
21
|
6 |
|
$rootNode = $treeBuilder->root('rest_service'); |
22
|
|
|
|
23
|
|
|
$rootNode |
|
|
|
|
24
|
6 |
|
->validate() |
25
|
|
|
->always(function ($v) { |
26
|
3 |
|
foreach ($v['operations'] as $name => $operation) { |
27
|
3 |
|
if (!$operation['requestProtocol']) { |
28
|
3 |
|
$v['operations'][$name]['requestProtocol'] = $v['protocol']; |
29
|
3 |
|
} |
30
|
3 |
|
if (!$operation['responseProtocol']) { |
31
|
3 |
|
$v['operations'][$name]['responseProtocol'] = $v['protocol']; |
32
|
3 |
|
} |
33
|
3 |
|
} |
34
|
|
|
|
35
|
3 |
|
return $v; |
36
|
6 |
|
}) |
37
|
6 |
|
->end() |
38
|
6 |
|
->children() |
39
|
6 |
|
->scalarNode('name') |
40
|
6 |
|
->cannotBeEmpty() |
41
|
6 |
|
->defaultNull() |
42
|
6 |
|
->end() // name |
43
|
6 |
|
->scalarNode('version') |
44
|
6 |
|
->cannotBeEmpty() |
45
|
6 |
|
->defaultNull() |
46
|
6 |
|
->end() // version |
47
|
6 |
|
->scalarNode('endpoint') |
48
|
6 |
|
->isRequired() |
49
|
6 |
|
->cannotBeEmpty() |
50
|
6 |
|
->end() // endpoint |
51
|
6 |
|
->scalarNode('description') |
52
|
6 |
|
->cannotBeEmpty() |
53
|
6 |
|
->defaultNull() |
54
|
6 |
|
->end() // description |
55
|
6 |
|
->scalarNode('documentationUrl') |
56
|
6 |
|
->cannotBeEmpty() |
57
|
6 |
|
->defaultNull() |
58
|
6 |
|
->end() // documentationUrl |
59
|
6 |
|
->scalarNode('protocol') |
60
|
6 |
|
->cannotBeEmpty() |
61
|
6 |
|
->defaultValue('rest_json') |
62
|
6 |
|
->validate() |
63
|
6 |
|
->ifNotInArray(['rest_json']) |
64
|
6 |
|
->thenInvalid('Invalid Protocol %s, Available protocols are "rest_json"') |
65
|
6 |
|
->end() |
66
|
6 |
|
->end() // protocol |
67
|
6 |
|
->append($this->getDefaultNode()) |
68
|
6 |
|
->append($this->getOperationNode()) |
69
|
6 |
|
->append($this->getShapeNode()) |
70
|
6 |
|
->append($this->getErrorShapeNode()) |
71
|
6 |
|
->end() // rest_service children |
72
|
|
|
; |
73
|
|
|
|
74
|
6 |
|
return $treeBuilder; |
75
|
|
|
} |
76
|
|
|
|
77
|
6 |
|
private function getDefaultNode() |
78
|
|
|
{ |
79
|
6 |
|
$treeBuilder = new TreeBuilder(); |
80
|
6 |
|
$node = $treeBuilder->root('defaults'); |
81
|
|
|
|
82
|
|
|
$node |
83
|
6 |
|
->useAttributeAsKey('name') |
84
|
6 |
|
->prototype('array') |
85
|
6 |
|
->children() |
86
|
6 |
|
->scalarNode('rule') |
87
|
6 |
|
->cannotBeEmpty() |
88
|
6 |
|
->defaultNull() |
89
|
6 |
|
->end() // rule |
90
|
6 |
|
->scalarNode('defaultValue') |
91
|
6 |
|
->cannotBeEmpty() |
92
|
6 |
|
->defaultValue('') |
93
|
6 |
|
->end() // defaultValue |
94
|
6 |
|
->end() // default children |
95
|
6 |
|
->end() // defaults prototype |
96
|
|
|
; |
97
|
|
|
|
98
|
6 |
|
return $node; |
99
|
|
|
} |
100
|
|
|
|
101
|
6 |
|
private function getOperationNode() |
102
|
|
|
{ |
103
|
6 |
|
$treeBuilder = new TreeBuilder(); |
104
|
|
|
|
105
|
|
|
$availableHttpMethods = [ |
106
|
6 |
|
'GET', 'POST', 'PUT', 'PATCH', |
107
|
6 |
|
'DELETE', 'HEAD', 'OPTIONS', |
108
|
6 |
|
]; |
109
|
|
|
|
110
|
|
|
$node = $treeBuilder |
|
|
|
|
111
|
6 |
|
->root('operations') |
112
|
6 |
|
->isRequired() |
113
|
6 |
|
->requiresAtLeastOneElement() |
114
|
6 |
|
->useAttributeAsKey('name') |
115
|
6 |
|
; |
116
|
|
|
|
117
|
6 |
|
$operationNode = $node->prototype('array'); |
118
|
|
|
|
119
|
|
|
$operationNode |
120
|
6 |
|
->children() |
121
|
6 |
|
->scalarNode('httpMethod') |
122
|
6 |
|
->isRequired() |
123
|
6 |
|
->beforeNormalization() |
124
|
6 |
|
->ifString() |
125
|
|
|
->then(function ($v) { |
126
|
5 |
|
return strtoupper($v); |
127
|
6 |
|
}) |
128
|
6 |
|
->end() |
129
|
6 |
|
->validate() |
130
|
6 |
|
->ifNotInArray($availableHttpMethods) |
131
|
6 |
|
->thenInvalid('Invalid HTTP Method %s, Available methods are "'.implode('", "', $availableHttpMethods).'"') |
132
|
6 |
|
->end() // validate |
133
|
6 |
|
->end() // httpMethod |
134
|
6 |
|
->scalarNode('requestUri') |
135
|
6 |
|
->isRequired() |
136
|
6 |
|
->cannotBeEmpty() |
137
|
6 |
|
->validate() |
138
|
6 |
|
->ifString() |
139
|
|
|
->then(function ($v) { |
140
|
3 |
|
return '/'.ltrim($v, '/'); |
141
|
6 |
|
}) |
142
|
6 |
|
->end() |
143
|
6 |
|
->end() |
144
|
6 |
|
->scalarNode('description') |
145
|
6 |
|
->cannotBeEmpty() |
146
|
6 |
|
->defaultNull() |
147
|
6 |
|
->end() // description |
148
|
6 |
|
->scalarNode('documentationUrl') |
149
|
6 |
|
->cannotBeEmpty() |
150
|
6 |
|
->defaultNull() |
151
|
6 |
|
->end() // documentationUrl |
152
|
6 |
|
->scalarNode('requestProtocol') |
153
|
6 |
|
->cannotBeEmpty() |
154
|
6 |
|
->defaultNull() |
155
|
6 |
|
->validate() |
156
|
6 |
|
->ifNotInArray(['rest_json', 'form_params']) |
157
|
6 |
|
->thenInvalid('Invalid Protocol %s, Available protocols are "rest_json", "form_params"') |
158
|
6 |
|
->end() |
159
|
6 |
|
->end() // requestProtocol |
160
|
6 |
|
->scalarNode('responseProtocol') |
161
|
6 |
|
->cannotBeEmpty() |
162
|
6 |
|
->defaultNull() |
163
|
6 |
|
->validate() |
164
|
6 |
|
->ifNotInArray(['rest_json']) |
165
|
6 |
|
->thenInvalid('Invalid Protocol %s, Available protocols are "rest_json"') |
166
|
6 |
|
->end() |
167
|
6 |
|
->end() // responseProtocol |
168
|
6 |
|
->booleanNode('strictRequest') |
169
|
6 |
|
->defaultFalse() |
170
|
6 |
|
->end() // strictRequest |
171
|
6 |
|
->booleanNode('strictResponse') |
172
|
6 |
|
->defaultFalse() |
173
|
6 |
|
->end() // strictResponse |
174
|
6 |
|
->booleanNode('sentEmptyField') |
175
|
6 |
|
->defaultTrue() |
176
|
6 |
|
->end() |
177
|
|
|
; |
178
|
|
|
|
179
|
|
|
$requestNode = $operationNode |
180
|
6 |
|
->children() |
181
|
6 |
|
->arrayNode('request') |
182
|
6 |
|
; |
183
|
6 |
|
$this->addShapeSection($requestNode); |
184
|
|
|
|
185
|
|
|
$responseNode = $operationNode |
186
|
6 |
|
->children() |
187
|
6 |
|
->arrayNode('response') |
188
|
6 |
|
; |
189
|
6 |
|
$this->addShapeSection($responseNode); |
190
|
|
|
|
191
|
|
|
$errorsNode = $operationNode |
192
|
6 |
|
->children() |
193
|
6 |
|
->arrayNode('errors') |
194
|
6 |
|
->prototype('array') |
195
|
6 |
|
; |
196
|
6 |
|
$this->addErrorShapeSection($errorsNode); |
197
|
|
|
|
198
|
6 |
|
return $node; |
199
|
|
|
} |
200
|
|
|
|
201
|
6 |
|
private function getShapeNode() |
202
|
|
|
{ |
203
|
6 |
|
$treeBuilder = new TreeBuilder(); |
204
|
|
|
$node = $treeBuilder |
205
|
6 |
|
->root('shapes') |
206
|
6 |
|
->useAttributeAsKey('name') |
207
|
6 |
|
; |
208
|
|
|
|
209
|
6 |
|
$this->addShapeSection($node->prototype('array'), false); |
|
|
|
|
210
|
|
|
|
211
|
6 |
|
return $node; |
212
|
|
|
} |
213
|
|
|
|
214
|
6 |
|
private function getErrorShapeNode() |
215
|
|
|
{ |
216
|
6 |
|
$treeBuilder = new TreeBuilder(); |
217
|
|
|
$node = $treeBuilder |
218
|
6 |
|
->root('errorShapes') |
219
|
6 |
|
->useAttributeAsKey('name') |
220
|
6 |
|
; |
221
|
|
|
|
222
|
6 |
|
$this->addErrorShapeSection($node->prototype('array'), false); |
|
|
|
|
223
|
|
|
|
224
|
6 |
|
return $node; |
225
|
|
|
} |
226
|
|
|
|
227
|
6 |
|
private function addShapeSection(ArrayNodeDefinition $rootNode, $fromOperation = true) |
228
|
|
|
{ |
229
|
6 |
|
$availableLocations = ['header', 'uri', 'query', 'body']; |
230
|
6 |
|
$availableTypes = ['map', 'list']; |
231
|
6 |
|
$availableMemberTypes = ['map', 'list', 'string', 'integer', 'float', 'number', 'boolean', 'datetime']; |
232
|
|
|
// map = object |
233
|
|
|
// list = array |
234
|
|
|
|
235
|
|
|
$rootNode |
236
|
6 |
|
->beforeNormalization() |
237
|
|
|
->always(function ($v) use ($fromOperation) { |
238
|
2 |
|
if (!$fromOperation) { |
239
|
2 |
|
if (isset($v['shape'])) { |
240
|
1 |
|
throw new InvalidConfigurationException('Unrecognized option "shape"'); |
241
|
|
|
} |
242
|
2 |
|
} |
243
|
|
|
|
244
|
2 |
|
if (isset($v['shape']) && (isset($v['type']) || isset($v['members']))) { |
245
|
|
|
throw new InvalidConfigurationException('Cannot combine "shape" with other properties.'); |
246
|
|
|
} |
247
|
|
|
|
248
|
2 |
|
return $v; |
249
|
6 |
|
}) |
250
|
6 |
|
->end() |
251
|
6 |
|
->children() |
252
|
6 |
|
->scalarNode('shape') |
253
|
6 |
|
->cannotBeEmpty() |
254
|
6 |
|
->defaultNull() |
255
|
6 |
|
->end() |
256
|
6 |
|
->scalarNode('extends') |
257
|
6 |
|
->cannotBeEmpty() |
258
|
6 |
|
->defaultNull() |
259
|
6 |
|
->end() |
260
|
6 |
|
->scalarNode('type') |
261
|
6 |
|
->cannotBeEmpty() |
262
|
6 |
|
->defaultValue('map') |
263
|
6 |
|
->validate() |
264
|
6 |
|
->ifNotInArray($availableTypes) |
265
|
6 |
|
->thenInvalid('Invalid type %s, Available types are "'.implode('", "', $availableTypes).'"') |
266
|
6 |
|
->end() // validate |
267
|
6 |
|
->end() // type |
268
|
6 |
|
->arrayNode('members') |
269
|
6 |
|
->beforeNormalization() |
270
|
6 |
|
->ifArray() |
271
|
|
|
->then(function ($v) { |
272
|
1 |
|
foreach ($v as $name => $member) { |
273
|
1 |
|
if (!isset($member['locationName'])) { |
274
|
1 |
|
$v[$name]['locationName'] = $name; |
275
|
1 |
|
} |
276
|
|
|
|
277
|
1 |
|
if (isset($member['shape']) |
278
|
1 |
|
&& (isset($member['location']) |
279
|
|
|
|| isset($member['defaultValue']) |
280
|
|
|
|| isset($member['format']) |
281
|
|
|
) |
282
|
1 |
|
) { |
283
|
|
|
throw new InvalidConfigurationException(sprintf( |
284
|
|
|
'Member "%s". Cannot combine "shape" with other properties.', |
285
|
|
|
$name |
286
|
|
|
)); |
287
|
|
|
} |
288
|
|
|
|
289
|
1 |
|
if (isset($member['shape']) && isset($member['type'])) { |
290
|
|
|
if (!in_array($member['type'], ['map', 'list'])) { |
291
|
|
|
throw new InvalidConfigurationException('type for shape only "map", "list"'); |
292
|
|
|
} |
293
|
1 |
|
} elseif (isset($member['type']) |
294
|
1 |
|
&& !in_array($member['type'], ['string', 'datetime']) |
295
|
1 |
|
&& isset($member['format']) |
296
|
1 |
|
) { |
297
|
|
|
throw new InvalidConfigurationException('"format" only for "string" or "datetime"'); |
298
|
|
|
} |
299
|
1 |
|
} |
300
|
|
|
|
301
|
1 |
|
return $v; |
302
|
6 |
|
}) |
303
|
6 |
|
->end() |
304
|
6 |
|
->useAttributeAsKey('name') |
305
|
6 |
|
->prototype('array') |
306
|
6 |
|
->children() |
307
|
6 |
|
->scalarNode('shape') |
308
|
6 |
|
->cannotBeEmpty() |
309
|
6 |
|
->defaultNull() |
310
|
6 |
|
->end() // shape |
311
|
6 |
|
->scalarNode('type') |
312
|
6 |
|
->cannotBeEmpty() |
313
|
6 |
|
->defaultValue('string') |
314
|
6 |
|
->validate() |
315
|
6 |
|
->ifNotInArray($availableMemberTypes) |
316
|
6 |
|
->thenInvalid('Invalid type %s, Available types are "'.implode('", "', $availableMemberTypes).'"') |
317
|
6 |
|
->end() // validate |
318
|
6 |
|
->end() // type |
319
|
6 |
|
->scalarNode('format') |
320
|
6 |
|
->cannotBeEmpty() |
321
|
6 |
|
->defaultNull() |
322
|
6 |
|
->end() // format |
323
|
6 |
|
->scalarNode('location') |
324
|
6 |
|
->defaultValue('body') |
325
|
6 |
|
->cannotBeEmpty() |
326
|
6 |
|
->validate() |
327
|
6 |
|
->ifNotInArray($availableLocations) |
328
|
6 |
|
->thenInvalid('Invalid shape location %s, Available methods are "'.implode('", "', $availableLocations).'"') |
329
|
6 |
|
->end() // validate |
330
|
6 |
|
->end() // location |
331
|
6 |
|
->scalarNode('locationName') |
332
|
6 |
|
->defaultNull() |
333
|
6 |
|
->cannotBeEmpty() |
334
|
6 |
|
->end() // locationName |
335
|
6 |
|
->scalarNode('rule') |
336
|
6 |
|
->defaultNull() |
337
|
6 |
|
->cannotBeEmpty() |
338
|
6 |
|
->end() // rule |
339
|
6 |
|
->scalarNode('defaultValue') |
340
|
6 |
|
->defaultValue('') |
341
|
6 |
|
->cannotBeEmpty() |
342
|
6 |
|
->end() |
343
|
6 |
|
->end() // member children |
344
|
6 |
|
->end() // members prototype |
345
|
6 |
|
->end() // members |
346
|
6 |
|
->end() // end |
347
|
|
|
; |
348
|
6 |
|
} |
349
|
|
|
|
350
|
6 |
|
private function addErrorShapeSection(ArrayNodeDefinition $rootNode, $fromOperation = true) |
351
|
|
|
{ |
352
|
6 |
|
$availableOperators = ['===', '!==', '==', '!=', '<', '<=', '>=', '>']; |
353
|
|
|
$rootNode |
354
|
6 |
|
->beforeNormalization() |
355
|
6 |
|
->always(function ($v) use ($fromOperation) { |
356
|
2 |
|
if (!$fromOperation) { |
357
|
2 |
|
if (isset($v['errorShape'])) { |
358
|
1 |
|
throw new InvalidConfigurationException('Unrecognized option "errorShape"'); |
359
|
|
|
} |
360
|
2 |
|
} |
361
|
|
|
|
362
|
2 |
|
if (isset($v['errorShape']) |
363
|
2 |
|
&& (isset($v['type']) |
364
|
|
|
|| isset($v['codeField']) |
365
|
|
|
|| isset($v['ifCode']) |
366
|
|
|
|| isset($v['messageField']) |
367
|
|
|
|| isset($v['defaultMessage']) |
368
|
|
|
) |
369
|
2 |
|
) { |
370
|
|
|
throw new InvalidConfigurationException('Cannot combine "shape" with other properties.'); |
371
|
|
|
} |
372
|
|
|
|
373
|
2 |
|
if (isset($v['type'])) { |
374
|
1 |
|
if ('field' === $v['type'] && !isset($v['codeField'])) { |
375
|
|
|
throw new InvalidConfigurationException('Error type "field", plesae provide "codeField" option.'); |
376
|
1 |
|
} elseif ('httpStatusCode' === $v['type'] && isset($v['codeField'])) { |
377
|
|
|
throw new InvalidConfigurationException('Error type "httpStatusCode", Unrecognized option "codeField".'); |
378
|
|
|
} |
379
|
1 |
|
} |
380
|
|
|
|
381
|
2 |
|
return $v; |
382
|
6 |
|
}) |
383
|
6 |
|
->end() |
384
|
6 |
|
->children() |
385
|
6 |
|
->scalarNode('errorShape') |
386
|
6 |
|
->cannotBeEmpty() |
387
|
6 |
|
->defaultNull() |
388
|
6 |
|
->end() // shape |
389
|
6 |
|
->scalarNode('type') |
390
|
6 |
|
->cannotBeEmpty() |
391
|
6 |
|
->validate() |
392
|
6 |
|
->ifNotInArray(['httpStatusCode', 'field']) |
393
|
6 |
|
->thenInvalid('Invalid error type %s, Available error types are "httpStatusCode", "field"') |
394
|
6 |
|
->end() // validate |
395
|
6 |
|
->end() // type |
396
|
6 |
|
->scalarNode('codeField') // if type = field |
397
|
6 |
|
->cannotBeEmpty() |
398
|
6 |
|
->defaultNull() |
399
|
6 |
|
->end() // codeField |
400
|
6 |
|
->scalarNode('ifCode') |
401
|
6 |
|
->cannotBeEmpty() |
402
|
6 |
|
->defaultNull() |
403
|
6 |
|
->end() // ifCode |
404
|
6 |
|
->scalarNode('operator') |
405
|
6 |
|
->cannotBeEmpty() |
406
|
6 |
|
->defaultValue('==') |
407
|
6 |
|
->validate() |
408
|
6 |
|
->ifNotInArray($availableOperators) |
409
|
6 |
|
->thenInvalid('Invalid operator type %s, Available operators are "'.implode('", "', $availableOperators).'"') |
410
|
6 |
|
->end() // validate |
411
|
6 |
|
->end() // operator |
412
|
6 |
|
->scalarNode('messageField') |
413
|
6 |
|
->cannotBeEmpty() |
414
|
6 |
|
->defaultNull() |
415
|
6 |
|
->end() // messageField |
416
|
6 |
|
->scalarNode('defaultMessage') |
417
|
6 |
|
->cannotBeEmpty() |
418
|
6 |
|
->defaultNull() |
419
|
6 |
|
->end() // defaultMessage |
420
|
6 |
|
->end() // error children |
421
|
|
|
; |
422
|
6 |
|
} |
423
|
|
|
} |
424
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: