1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the FOSRestBundle package. |
5
|
|
|
* |
6
|
|
|
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace FOS\RestBundle\Request; |
13
|
|
|
|
14
|
|
|
use FOS\RestBundle\Context\Context; |
15
|
|
|
use FOS\RestBundle\Serializer\Serializer; |
16
|
|
|
use JMS\Serializer\Exception\Exception as JMSSerializerException; |
17
|
|
|
use JMS\Serializer\Exception\UnsupportedFormatException; |
18
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter; |
19
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Request\ParamConverter\ParamConverterInterface; |
20
|
|
|
use Symfony\Component\HttpFoundation\Request; |
21
|
|
|
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; |
22
|
|
|
use Symfony\Component\HttpKernel\Exception\UnsupportedMediaTypeHttpException; |
23
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
24
|
|
|
use Symfony\Component\Serializer\Exception\ExceptionInterface as SymfonySerializerException; |
25
|
|
|
use Symfony\Component\Validator\Validator\ValidatorInterface; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @author Tyler Stroud <[email protected]> |
29
|
|
|
*/ |
30
|
|
|
class RequestBodyParamConverter implements ParamConverterInterface |
31
|
|
|
{ |
32
|
|
|
private $serializer; |
33
|
|
|
private $context = []; |
34
|
|
|
private $validator; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* The name of the argument on which the ConstraintViolationList will be set. |
38
|
|
|
* |
39
|
|
|
* @var null|string |
40
|
|
|
*/ |
41
|
|
|
private $validationErrorsArgument; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param Serializer $serializer |
45
|
|
|
* @param array|null $groups An array of groups to be used in the serialization context |
46
|
|
|
* @param string|null $version A version string to be used in the serialization context |
47
|
|
|
* @param ValidatorInterface $validator |
48
|
|
|
* @param string|null $validationErrorsArgument |
49
|
|
|
* |
50
|
|
|
* @throws \InvalidArgumentException |
51
|
|
|
*/ |
52
|
15 |
|
public function __construct( |
53
|
|
|
Serializer $serializer, |
54
|
|
|
$groups = null, |
55
|
|
|
$version = null, |
56
|
|
|
ValidatorInterface $validator = null, |
57
|
|
|
$validationErrorsArgument = null |
58
|
|
|
) { |
59
|
15 |
|
$this->serializer = $serializer; |
60
|
|
|
|
61
|
15 |
|
if (!empty($groups)) { |
62
|
1 |
|
$this->context['groups'] = (array) $groups; |
63
|
1 |
|
} |
64
|
|
|
|
65
|
15 |
|
if (!empty($version)) { |
66
|
1 |
|
$this->context['version'] = $version; |
67
|
1 |
|
} |
68
|
|
|
|
69
|
15 |
|
if (null !== $validator && null === $validationErrorsArgument) { |
70
|
|
|
throw new \InvalidArgumentException('"$validationErrorsArgument" cannot be null when using the validator'); |
71
|
|
|
} |
72
|
|
|
|
73
|
15 |
|
$this->validator = $validator; |
74
|
15 |
|
$this->validationErrorsArgument = $validationErrorsArgument; |
75
|
15 |
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* {@inheritdoc} |
79
|
|
|
*/ |
80
|
9 |
|
public function apply(Request $request, ParamConverter $configuration) |
81
|
|
|
{ |
82
|
9 |
|
$options = (array) $configuration->getOptions(); |
83
|
|
|
|
84
|
9 |
|
if (isset($options['deserializationContext']) && is_array($options['deserializationContext'])) { |
85
|
1 |
|
$arrayContext = array_merge($this->context, $options['deserializationContext']); |
86
|
1 |
|
} else { |
87
|
8 |
|
$arrayContext = $this->context; |
88
|
|
|
} |
89
|
9 |
|
$this->configureContext($context = new Context(), $arrayContext); |
90
|
|
|
|
91
|
|
|
try { |
92
|
9 |
|
$object = $this->serializer->deserialize( |
93
|
9 |
|
$request->getContent(), |
|
|
|
|
94
|
9 |
|
$configuration->getClass(), |
95
|
9 |
|
$request->getContentType(), |
96
|
|
|
$context |
97
|
9 |
|
); |
98
|
9 |
|
} catch (UnsupportedFormatException $e) { |
99
|
|
|
return $this->throwException(new UnsupportedMediaTypeHttpException($e->getMessage(), $e), $configuration); |
100
|
4 |
|
} catch (JMSSerializerException $e) { |
101
|
1 |
|
return $this->throwException(new BadRequestHttpException($e->getMessage(), $e), $configuration); |
|
|
|
|
102
|
3 |
|
} catch (SymfonySerializerException $e) { |
103
|
2 |
|
return $this->throwException(new BadRequestHttpException($e->getMessage(), $e), $configuration); |
|
|
|
|
104
|
|
|
} |
105
|
|
|
|
106
|
5 |
|
$request->attributes->set($configuration->getName(), $object); |
107
|
|
|
|
108
|
5 |
|
if (null !== $this->validator) { |
109
|
1 |
|
$validatorOptions = $this->getValidatorOptions($options); |
110
|
|
|
|
111
|
1 |
|
$errors = $this->validator->validate($object, null, $validatorOptions['groups']); |
112
|
|
|
|
113
|
1 |
|
$request->attributes->set( |
114
|
1 |
|
$this->validationErrorsArgument, |
115
|
|
|
$errors |
116
|
1 |
|
); |
117
|
1 |
|
} |
118
|
|
|
|
119
|
5 |
|
return true; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* {@inheritdoc} |
124
|
|
|
*/ |
125
|
4 |
|
public function supports(ParamConverter $configuration) |
126
|
|
|
{ |
127
|
4 |
|
return null !== $configuration->getClass(); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @param Context $context |
132
|
|
|
* @param array $options |
133
|
|
|
*/ |
134
|
9 |
|
protected function configureContext(Context $context, array $options) |
135
|
|
|
{ |
136
|
9 |
|
foreach ($options as $key => $value) { |
137
|
1 |
|
if ($key == 'groups') { |
138
|
1 |
|
$context->addGroups($options['groups']); |
139
|
1 |
|
} elseif ($key == 'version') { |
140
|
1 |
|
$context->setVersion($options['version']); |
141
|
1 |
|
} elseif ($key == 'maxDepth') { |
142
|
1 |
|
$context->setMaxDepth($options['maxDepth']); |
143
|
1 |
|
} elseif ($key == 'serializeNull') { |
144
|
1 |
|
$context->setSerializeNull($options['serializeNull']); |
145
|
1 |
|
} else { |
146
|
1 |
|
$context->setAttribute($key, $value); |
147
|
|
|
} |
148
|
9 |
|
} |
149
|
9 |
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Throws an exception or return false if a ParamConverter is optional. |
153
|
|
|
*/ |
154
|
3 |
|
private function throwException(\Exception $exception, ParamConverter $configuration) |
155
|
|
|
{ |
156
|
3 |
|
if ($configuration->isOptional()) { |
157
|
1 |
|
return false; |
158
|
|
|
} |
159
|
|
|
|
160
|
2 |
|
throw $exception; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* @param array $options |
165
|
|
|
* |
166
|
|
|
* @return array |
167
|
|
|
*/ |
168
|
2 |
|
private function getValidatorOptions(array $options) |
169
|
|
|
{ |
170
|
2 |
|
$resolver = new OptionsResolver(); |
171
|
2 |
|
$resolver->setDefaults([ |
172
|
2 |
|
'groups' => null, |
173
|
2 |
|
'traverse' => false, |
174
|
2 |
|
'deep' => false, |
175
|
2 |
|
]); |
176
|
|
|
|
177
|
2 |
|
return $resolver->resolve(isset($options['validator']) ? $options['validator'] : []); |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
|
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.