|
| 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 Symfony\Component\HttpFoundation\Request; |
| 15 | +use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; |
| 16 | +use Symfony\Component\HttpKernel\Exception\UnsupportedMediaTypeHttpException; |
| 17 | +use Symfony\Component\OptionsResolver\OptionsResolver; |
| 18 | +use Symfony\Component\Serializer\Exception\Exception as SymfonySerializerException; |
| 19 | +use Symfony\Component\Validator\ValidatorInterface; |
| 20 | +use Sensio\Bundle\FrameworkExtraBundle\Request\ParamConverter\ParamConverterInterface; |
| 21 | +use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter; |
| 22 | +use JMS\Serializer\Exception\UnsupportedFormatException; |
| 23 | +use JMS\Serializer\Exception\Exception as JMSSerializerException; |
| 24 | +use JMS\Serializer\DeserializationContext; |
| 25 | +use JMS\Serializer\SerializerInterface; |
| 26 | + |
| 27 | +/** |
| 28 | + * @author Tyler Stroud <[email protected]> |
| 29 | + */ |
| 30 | +abstract class AbstractRequestBodyParamConverter implements ParamConverterInterface |
| 31 | +{ |
| 32 | + /** |
| 33 | + * @var object |
| 34 | + */ |
| 35 | + protected $serializer; |
| 36 | + |
| 37 | + /** |
| 38 | + * @var array |
| 39 | + */ |
| 40 | + protected $context = array(); |
| 41 | + |
| 42 | + /** |
| 43 | + * @var null|ValidatorInterface |
| 44 | + */ |
| 45 | + protected $validator; |
| 46 | + |
| 47 | + /** |
| 48 | + * @var null|string The name of the argument on which the ConstraintViolationList will be set |
| 49 | + */ |
| 50 | + protected $validationErrorsArgument; |
| 51 | + |
| 52 | + /** |
| 53 | + * @param object $serializer |
| 54 | + * @param array|null $groups An array of groups to be used in the serialization context |
| 55 | + * @param string|null $version A version string to be used in the serialization context |
| 56 | + * @param object $serializer |
| 57 | + * @param ValidatorInterface $validator |
| 58 | + * @param string|null $validationErrorsArgument |
| 59 | + * |
| 60 | + * @throws \InvalidArgumentException |
| 61 | + */ |
| 62 | + public function __construct( |
| 63 | + $serializer, |
| 64 | + $groups = null, |
| 65 | + $version = null, |
| 66 | + ValidatorInterface $validator = null, |
| 67 | + $validationErrorsArgument = null |
| 68 | + ) { |
| 69 | + $this->serializer = $serializer; |
| 70 | + |
| 71 | + if (!empty($groups)) { |
| 72 | + $this->context['groups'] = (array) $groups; |
| 73 | + } |
| 74 | + if (!empty($version)) { |
| 75 | + $this->context['version'] = $version; |
| 76 | + } |
| 77 | + |
| 78 | + if (null !== $validator && null === $validationErrorsArgument) { |
| 79 | + throw new \InvalidArgumentException('"$validationErrorsArgument" cannot be null when using the validator'); |
| 80 | + } |
| 81 | + $this->validator = $validator; |
| 82 | + $this->validationErrorsArgument = $validationErrorsArgument; |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Stores the object in the request. |
| 87 | + * |
| 88 | + * @param Request $request The request |
| 89 | + * @param ParamConverter $configuration Contains the name, class and options of the object |
| 90 | + * |
| 91 | + * @return boolean True if the object has been successfully set, else false |
| 92 | + * |
| 93 | + * @throws UnsupportedMediaTypeHttpException |
| 94 | + * @throws BadRequestHttpException |
| 95 | + */ |
| 96 | + protected function execute(Request $request, ParamConverter $configuration) |
| 97 | + { |
| 98 | + $options = (array) $configuration->getOptions(); |
| 99 | + |
| 100 | + if (isset($options['deserializationContext']) && is_array($options['deserializationContext'])) { |
| 101 | + $context = array_merge($this->context, $options['deserializationContext']); |
| 102 | + } else { |
| 103 | + $context = $this->context; |
| 104 | + } |
| 105 | + |
| 106 | + if ($this->serializer instanceof SerializerInterface) { |
| 107 | + $context = $this->configureDeserializationContext($this->getDeserializationContext(), $context); |
| 108 | + } |
| 109 | + |
| 110 | + try { |
| 111 | + $object = $this->serializer->deserialize( |
| 112 | + $request->getContent(), |
| 113 | + $configuration->getClass(), |
| 114 | + $request->getContentType(), |
| 115 | + $context |
| 116 | + ); |
| 117 | + } catch (UnsupportedFormatException $e) { |
| 118 | + throw new UnsupportedMediaTypeHttpException($e->getMessage()); |
| 119 | + } catch (JMSSerializerException $e) { |
| 120 | + throw new BadRequestHttpException($e->getMessage()); |
| 121 | + } catch (SymfonySerializerException $e) { |
| 122 | + throw new BadRequestHttpException($e->getMessage()); |
| 123 | + } |
| 124 | + |
| 125 | + $request->attributes->set($configuration->getName(), $object); |
| 126 | + |
| 127 | + if (null !== $this->validator) { |
| 128 | + $validatorOptions = $this->getValidatorOptions($options); |
| 129 | + $request->attributes->set( |
| 130 | + $this->validationErrorsArgument, |
| 131 | + $this->validator->validate( |
| 132 | + $object, |
| 133 | + $validatorOptions['groups'], |
| 134 | + $validatorOptions['traverse'], |
| 135 | + $validatorOptions['deep'] |
| 136 | + ) |
| 137 | + ); |
| 138 | + } |
| 139 | + |
| 140 | + return true; |
| 141 | + } |
| 142 | + |
| 143 | + /** |
| 144 | + * @return DeserializationContext |
| 145 | + */ |
| 146 | + protected function getDeserializationContext() |
| 147 | + { |
| 148 | + return DeserializationContext::create(); |
| 149 | + } |
| 150 | + |
| 151 | + /** |
| 152 | + * @param DeserializationContext $context |
| 153 | + * @param array $options |
| 154 | + * |
| 155 | + * @return DeserializationContext |
| 156 | + */ |
| 157 | + protected function configureDeserializationContext(DeserializationContext $context, array $options) |
| 158 | + { |
| 159 | + if (isset($options['groups'])) { |
| 160 | + $context->setGroups($options['groups']); |
| 161 | + } |
| 162 | + if (isset($options['version'])) { |
| 163 | + $context->setVersion($options['version']); |
| 164 | + } |
| 165 | + |
| 166 | + return $context; |
| 167 | + } |
| 168 | + |
| 169 | + /** |
| 170 | + * @param array $options |
| 171 | + * |
| 172 | + * @return array |
| 173 | + */ |
| 174 | + protected function getValidatorOptions(array $options) |
| 175 | + { |
| 176 | + $resolver = new OptionsResolver(); |
| 177 | + $resolver->setDefaults(array( |
| 178 | + 'groups' => null, |
| 179 | + 'traverse' => false, |
| 180 | + 'deep' => false, |
| 181 | + )); |
| 182 | + |
| 183 | + return $resolver->resolve(isset($options['validator']) ? $options['validator'] : array()); |
| 184 | + } |
| 185 | +} |
0 commit comments