1 | <?php |
||
32 | class RequestBodyParamConverter implements ParamConverterInterface |
||
33 | { |
||
34 | private $serializer; |
||
35 | private $context = []; |
||
36 | private $validator; |
||
37 | private $validationErrorsArgument; |
||
38 | |||
39 | /** |
||
40 | * @param string[]|null $groups An array of groups to be used in the serialization context |
||
41 | * @param string|null $version A version string to be used in the serialization context |
||
42 | * |
||
43 | * @throws \InvalidArgumentException |
||
44 | */ |
||
45 | 19 | public function __construct( |
|
69 | |||
70 | /** |
||
71 | * {@inheritdoc} |
||
72 | */ |
||
73 | 10 | public function apply(Request $request, ParamConverter $configuration) |
|
74 | { |
||
75 | 10 | $options = (array) $configuration->getOptions(); |
|
76 | |||
77 | 10 | if (isset($options['deserializationContext']) && is_array($options['deserializationContext'])) { |
|
78 | 1 | $arrayContext = array_merge($this->context, $options['deserializationContext']); |
|
79 | } else { |
||
80 | 9 | $arrayContext = $this->context; |
|
81 | } |
||
82 | 10 | $this->configureContext($context = new Context(), $arrayContext); |
|
83 | |||
84 | 10 | $format = $request->getContentType(); |
|
85 | 10 | if (null === $format) { |
|
86 | 1 | return $this->throwException(new UnsupportedMediaTypeHttpException(), $configuration); |
|
87 | } |
||
88 | |||
89 | try { |
||
90 | 9 | $object = $this->serializer->deserialize( |
|
91 | 9 | $request->getContent(), |
|
|
|||
92 | 9 | $configuration->getClass(), |
|
93 | $format, |
||
94 | $context |
||
95 | ); |
||
96 | 3 | } catch (UnsupportedFormatException $e) { |
|
97 | return $this->throwException(new UnsupportedMediaTypeHttpException($e->getMessage(), $e), $configuration); |
||
98 | 3 | } catch (JMSSerializerException $e) { |
|
99 | 1 | return $this->throwException(new BadRequestHttpException($e->getMessage(), $e), $configuration); |
|
100 | 2 | } catch (SymfonySerializerException $e) { |
|
101 | 1 | return $this->throwException(new BadRequestHttpException($e->getMessage(), $e), $configuration); |
|
102 | } |
||
103 | |||
104 | 6 | $request->attributes->set($configuration->getName(), $object); |
|
105 | |||
106 | 6 | if (null !== $this->validator && (!isset($options['validate']) || $options['validate'])) { |
|
107 | 1 | $validatorOptions = $this->getValidatorOptions($options); |
|
108 | |||
109 | 1 | $errors = $this->validator->validate($object, null, $validatorOptions['groups']); |
|
110 | |||
111 | 1 | $request->attributes->set( |
|
112 | 1 | $this->validationErrorsArgument, |
|
113 | $errors |
||
114 | ); |
||
115 | } |
||
116 | |||
117 | 6 | return true; |
|
118 | } |
||
119 | |||
120 | /** |
||
121 | * {@inheritdoc} |
||
122 | */ |
||
123 | 5 | public function supports(ParamConverter $configuration) |
|
127 | |||
128 | 12 | protected function configureContext(Context $context, array $options) |
|
147 | |||
148 | 3 | private function throwException(\Exception $exception, ParamConverter $configuration) |
|
156 | |||
157 | 2 | private function getValidatorOptions(array $options): array |
|
168 | } |
||
169 |
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.