|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace Maba\Bundle\RestBundle\Service; |
|
5
|
|
|
|
|
6
|
|
|
use InvalidArgumentException; |
|
7
|
|
|
use Maba\Bundle\RestBundle\Entity\RestRequestOptions; |
|
8
|
|
|
use Maba\Bundle\RestBundle\Exception\ConfigurationException; |
|
9
|
|
|
use Maba\Bundle\RestBundle\Service\PathAttributeResolver\PathAttributeResolverRegistry; |
|
10
|
|
|
use Paysera\Component\Normalization\NormalizerRegistryInterface; |
|
11
|
|
|
|
|
12
|
|
|
class RestRequestOptionsValidator |
|
13
|
|
|
{ |
|
14
|
|
|
private $normalizerRegistry; |
|
15
|
|
|
private $pathAttributeResolverRegistry; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @param NormalizerRegistryInterface $normalizerRegistry |
|
19
|
|
|
* @param PathAttributeResolverRegistry $pathAttributeResolverRegistry |
|
20
|
|
|
* @internal |
|
21
|
|
|
*/ |
|
22
|
94 |
|
public function __construct( |
|
23
|
|
|
NormalizerRegistryInterface $normalizerRegistry, |
|
24
|
|
|
PathAttributeResolverRegistry $pathAttributeResolverRegistry |
|
25
|
|
|
) { |
|
26
|
94 |
|
$this->normalizerRegistry = $normalizerRegistry; |
|
27
|
94 |
|
$this->pathAttributeResolverRegistry = $pathAttributeResolverRegistry; |
|
28
|
94 |
|
} |
|
29
|
|
|
|
|
30
|
94 |
|
public function validateRestRequestOptions(RestRequestOptions $options, string $methodName) |
|
31
|
|
|
{ |
|
32
|
94 |
|
$normalizer = $options->getResponseNormalizationType(); |
|
33
|
94 |
|
if ($normalizer !== null && !$this->normalizerRegistry->hasNormalizer($normalizer)) { |
|
34
|
1 |
|
throw new ConfigurationException(sprintf( |
|
35
|
1 |
|
'Normalizer %s does not exist (configured for %s)', |
|
36
|
1 |
|
$normalizer, |
|
37
|
1 |
|
$methodName |
|
38
|
|
|
)); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
93 |
|
if ($options->hasBodyDenormalization()) { |
|
42
|
85 |
|
$this->validateDenormalizer($options->getBodyDenormalizationType(), $methodName); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
91 |
|
foreach ($options->getQueryResolverOptionsList() as $queryResolverOptions) { |
|
46
|
85 |
|
$this->validateDenormalizer($queryResolverOptions->getDenormalizationType(), $methodName); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
89 |
|
foreach ($options->getPathAttributeResolverOptionsList() as $pathAttributeResolverOptions) { |
|
50
|
84 |
|
$this->validatePathAttributeResolver( |
|
51
|
84 |
|
$pathAttributeResolverOptions->getPathAttributeResolverType(), |
|
52
|
84 |
|
$methodName |
|
53
|
|
|
); |
|
54
|
|
|
} |
|
55
|
87 |
|
} |
|
56
|
|
|
|
|
57
|
89 |
|
private function validateDenormalizer(string $type, string $methodName) |
|
58
|
|
|
{ |
|
59
|
89 |
|
$denormalizerType = $this->normalizerRegistry->getDenormalizerType($type); |
|
60
|
89 |
|
if ($denormalizerType === NormalizerRegistryInterface::DENORMALIZER_TYPE_NONE) { |
|
61
|
4 |
|
throw new ConfigurationException(sprintf( |
|
62
|
4 |
|
'Denormalizer "%s" does not exist (configured for "%s")', |
|
63
|
4 |
|
$type, |
|
64
|
4 |
|
$methodName |
|
65
|
|
|
)); |
|
66
|
|
|
} |
|
67
|
86 |
|
} |
|
68
|
|
|
|
|
69
|
84 |
|
private function validatePathAttributeResolver(string $type, string $methodName) |
|
70
|
|
|
{ |
|
71
|
|
|
try { |
|
72
|
84 |
|
$this->pathAttributeResolverRegistry->getResolverByType($type); |
|
73
|
2 |
|
} catch (InvalidArgumentException $exception) { |
|
74
|
2 |
|
throw new ConfigurationException(sprintf( |
|
75
|
2 |
|
'Path attribute resolver "%s" does not exist (configured for "%s")', |
|
76
|
2 |
|
$type, |
|
77
|
2 |
|
$methodName |
|
78
|
|
|
)); |
|
79
|
|
|
} |
|
80
|
83 |
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|