|
1
|
|
|
<?php |
|
2
|
|
|
namespace Yoanm\SymfonyJsonRpcParamsSfConstraintsDoc\Listener; |
|
3
|
|
|
|
|
4
|
|
|
use Yoanm\JsonRpcServer\Domain\Exception\JsonRpcInvalidParamsException; |
|
5
|
|
|
use Yoanm\JsonRpcServerDoc\Domain\Model\ErrorDoc; |
|
6
|
|
|
use Yoanm\JsonRpcServerDoc\Domain\Model\Type\ArrayDoc; |
|
7
|
|
|
use Yoanm\JsonRpcServerDoc\Domain\Model\Type\ObjectDoc; |
|
8
|
|
|
use Yoanm\JsonRpcServerDoc\Domain\Model\Type\StringDoc; |
|
9
|
|
|
use Yoanm\SymfonyJsonRpcHttpServerDoc\Event\ServerDocCreatedEvent; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Class ServerDocCreatedListener |
|
13
|
|
|
*/ |
|
14
|
|
|
class ServerDocCreatedListener |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* @param ServerDocCreatedEvent $event |
|
18
|
|
|
*/ |
|
19
|
3 |
|
public function enhanceParamValidationErrorDoc(ServerDocCreatedEvent $event) : void |
|
20
|
|
|
{ |
|
21
|
3 |
|
$paramsValidationError = null; |
|
22
|
|
|
|
|
23
|
|
|
// Search for existing error in server errors list |
|
24
|
3 |
|
foreach ($event->getDoc()->getServerErrorList() as $serverError) { |
|
25
|
2 |
|
if (JsonRpcInvalidParamsException::CODE === $serverError->getCode()) { |
|
26
|
2 |
|
$paramsValidationError = $serverError; |
|
27
|
2 |
|
break; |
|
28
|
|
|
} |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
3 |
|
if (null === $paramsValidationError) { |
|
32
|
|
|
// In case not found => Create new one and append it to server error list |
|
33
|
1 |
|
$paramsValidationError = new ErrorDoc('Params validations error', JsonRpcInvalidParamsException::CODE); |
|
34
|
1 |
|
$event->getDoc()->addServerError($paramsValidationError); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
// In case error was already defined, the data doc will be overridden |
|
38
|
3 |
|
$paramsValidationError->setDataDoc( |
|
39
|
3 |
|
(new ObjectDoc()) |
|
40
|
3 |
|
->setAllowMissingSibling(false) |
|
41
|
3 |
|
->setAllowExtraSibling(false) |
|
42
|
3 |
|
->setRequired(true) |
|
43
|
3 |
|
->addSibling( |
|
44
|
3 |
|
(new ArrayDoc()) |
|
45
|
3 |
|
->setName(JsonRpcInvalidParamsException::DATA_VIOLATIONS_KEY) |
|
46
|
3 |
|
->setItemValidation( |
|
47
|
3 |
|
(new ObjectDoc()) |
|
48
|
3 |
|
->setAllowExtraSibling(false) |
|
49
|
3 |
|
->setRequired(true) |
|
50
|
3 |
|
->addSibling( |
|
51
|
3 |
|
(new StringDoc()) |
|
52
|
3 |
|
->setName('path') |
|
53
|
3 |
|
->setExample('[key]') |
|
54
|
3 |
|
->setRequired(true) |
|
55
|
3 |
|
) |
|
56
|
3 |
|
->addSibling( |
|
57
|
3 |
|
(new StringDoc()) |
|
58
|
3 |
|
->setName('message') |
|
59
|
3 |
|
->setRequired(true) |
|
60
|
3 |
|
) |
|
61
|
3 |
|
->addSibling( |
|
62
|
3 |
|
(new StringDoc()) |
|
63
|
3 |
|
->setName('code') |
|
64
|
3 |
|
) |
|
65
|
3 |
|
) |
|
66
|
3 |
|
) |
|
67
|
3 |
|
); |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|