1 | <?php |
||
30 | final class RequestBodyParamConverter implements ParamConverterInterface |
||
31 | { |
||
32 | private $serializer; |
||
33 | private $context = []; |
||
34 | private $validator; |
||
35 | private $validationErrorsArgument; |
||
36 | |||
37 | /** |
||
38 | * @param string[]|null $groups |
||
39 | */ |
||
40 | 15 | public function __construct( |
|
41 | Serializer $serializer, |
||
42 | ?array $groups = null, |
||
43 | ?string $version = null, |
||
44 | ValidatorInterface $validator = null, |
||
45 | ?string $validationErrorsArgument = null |
||
46 | ) { |
||
47 | 15 | $this->serializer = $serializer; |
|
48 | |||
49 | 15 | if (!empty($groups)) { |
|
50 | 1 | $this->context['groups'] = (array) $groups; |
|
51 | } |
||
52 | |||
53 | 15 | if (!empty($version)) { |
|
54 | 1 | $this->context['version'] = $version; |
|
55 | } |
||
56 | |||
57 | 15 | if (null !== $validator && null === $validationErrorsArgument) { |
|
58 | throw new \InvalidArgumentException('"$validationErrorsArgument" cannot be null when using the validator'); |
||
59 | } |
||
60 | |||
61 | 15 | $this->validator = $validator; |
|
62 | 15 | $this->validationErrorsArgument = $validationErrorsArgument; |
|
63 | 15 | } |
|
64 | |||
65 | /** |
||
66 | * {@inheritdoc} |
||
67 | */ |
||
68 | 9 | public function apply(Request $request, ParamConverter $configuration): bool |
|
69 | { |
||
70 | 9 | $options = (array) $configuration->getOptions(); |
|
71 | |||
72 | 9 | if (isset($options['deserializationContext']) && is_array($options['deserializationContext'])) { |
|
73 | 1 | $arrayContext = array_merge($this->context, $options['deserializationContext']); |
|
74 | } else { |
||
75 | 8 | $arrayContext = $this->context; |
|
76 | } |
||
77 | 9 | $this->configureContext($context = new Context(), $arrayContext); |
|
78 | |||
79 | 9 | $format = $request->getContentType(); |
|
80 | 9 | if ($format === null) { |
|
81 | return $this->throwException(new UnsupportedMediaTypeHttpException(), $configuration); |
||
82 | } |
||
83 | |||
84 | try { |
||
85 | 9 | $object = $this->serializer->deserialize( |
|
86 | 9 | $request->getContent(), |
|
|
|||
87 | 9 | $configuration->getClass(), |
|
88 | $format, |
||
89 | $context |
||
90 | ); |
||
91 | 3 | } catch (UnsupportedFormatException $e) { |
|
92 | return $this->throwException(new UnsupportedMediaTypeHttpException($e->getMessage(), $e), $configuration); |
||
93 | 3 | } catch (JMSSerializerException $e) { |
|
94 | 1 | return $this->throwException(new BadRequestHttpException($e->getMessage(), $e), $configuration); |
|
95 | 2 | } catch (SymfonySerializerException $e) { |
|
96 | 1 | return $this->throwException(new BadRequestHttpException($e->getMessage(), $e), $configuration); |
|
97 | } |
||
98 | |||
99 | 6 | $request->attributes->set($configuration->getName(), $object); |
|
100 | |||
101 | 6 | if (null !== $this->validator && (!isset($options['validate']) || $options['validate'])) { |
|
102 | 1 | $validatorOptions = $this->getValidatorOptions($options); |
|
103 | |||
104 | 1 | $errors = $this->validator->validate($object, null, $validatorOptions['groups']); |
|
105 | |||
106 | 1 | $request->attributes->set( |
|
107 | 1 | $this->validationErrorsArgument, |
|
108 | $errors |
||
109 | ); |
||
110 | } |
||
111 | |||
112 | 6 | return true; |
|
113 | } |
||
114 | |||
115 | /** |
||
116 | * {@inheritdoc} |
||
117 | */ |
||
118 | 4 | public function supports(ParamConverter $configuration): bool |
|
122 | |||
123 | 10 | private function configureContext(Context $context, array $options): void |
|
124 | { |
||
139 | |||
140 | 2 | private function throwException(\Exception $exception, ParamConverter $configuration): bool |
|
148 | |||
149 | 2 | private function getValidatorOptions(array $options): array |
|
160 | } |
||
161 |
This check looks at variables that are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.