Security update for Core, with self-updated composer
[yaffs-website] / vendor / symfony / serializer / Normalizer / AbstractObjectNormalizer.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.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 Symfony\Component\Serializer\Normalizer;
13
14 use Symfony\Component\PropertyAccess\Exception\InvalidArgumentException;
15 use Symfony\Component\Serializer\Encoder\JsonEncoder;
16 use Symfony\Component\Serializer\Exception\LogicException;
17 use Symfony\Component\Serializer\Exception\UnexpectedValueException;
18 use Symfony\Component\PropertyInfo\PropertyTypeExtractorInterface;
19 use Symfony\Component\PropertyInfo\Type;
20 use Symfony\Component\Serializer\Mapping\AttributeMetadataInterface;
21 use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface;
22 use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
23
24 /**
25  * Base class for a normalizer dealing with objects.
26  *
27  * @author Kévin Dunglas <dunglas@gmail.com>
28  */
29 abstract class AbstractObjectNormalizer extends AbstractNormalizer
30 {
31     const ENABLE_MAX_DEPTH = 'enable_max_depth';
32     const DEPTH_KEY_PATTERN = 'depth_%s::%s';
33
34     private $propertyTypeExtractor;
35     private $attributesCache = array();
36
37     public function __construct(ClassMetadataFactoryInterface $classMetadataFactory = null, NameConverterInterface $nameConverter = null, PropertyTypeExtractorInterface $propertyTypeExtractor = null)
38     {
39         parent::__construct($classMetadataFactory, $nameConverter);
40
41         $this->propertyTypeExtractor = $propertyTypeExtractor;
42     }
43
44     /**
45      * {@inheritdoc}
46      */
47     public function supportsNormalization($data, $format = null)
48     {
49         return is_object($data) && !$data instanceof \Traversable;
50     }
51
52     /**
53      * {@inheritdoc}
54      */
55     public function normalize($object, $format = null, array $context = array())
56     {
57         if (!isset($context['cache_key'])) {
58             $context['cache_key'] = $this->getCacheKey($format, $context);
59         }
60
61         if ($this->isCircularReference($object, $context)) {
62             return $this->handleCircularReference($object);
63         }
64
65         $data = array();
66         $stack = array();
67         $attributes = $this->getAttributes($object, $format, $context);
68         $class = get_class($object);
69         $attributesMetadata = $this->classMetadataFactory ? $this->classMetadataFactory->getMetadataFor($class)->getAttributesMetadata() : null;
70
71         foreach ($attributes as $attribute) {
72             if (null !== $attributesMetadata && $this->isMaxDepthReached($attributesMetadata, $class, $attribute, $context)) {
73                 continue;
74             }
75
76             $attributeValue = $this->getAttributeValue($object, $attribute, $format, $context);
77
78             if (isset($this->callbacks[$attribute])) {
79                 $attributeValue = call_user_func($this->callbacks[$attribute], $attributeValue);
80             }
81
82             if (null !== $attributeValue && !is_scalar($attributeValue)) {
83                 $stack[$attribute] = $attributeValue;
84             }
85
86             $data = $this->updateData($data, $attribute, $attributeValue);
87         }
88
89         foreach ($stack as $attribute => $attributeValue) {
90             if (!$this->serializer instanceof NormalizerInterface) {
91                 throw new LogicException(sprintf('Cannot normalize attribute "%s" because the injected serializer is not a normalizer', $attribute));
92             }
93
94             $data = $this->updateData($data, $attribute, $this->serializer->normalize($attributeValue, $format, $context));
95         }
96
97         return $data;
98     }
99
100     /**
101      * Gets and caches attributes for the given object, format and context.
102      *
103      * @param object      $object
104      * @param string|null $format
105      * @param array       $context
106      *
107      * @return string[]
108      */
109     protected function getAttributes($object, $format = null, array $context)
110     {
111         $class = get_class($object);
112         $key = $class.'-'.$context['cache_key'];
113
114         if (isset($this->attributesCache[$key])) {
115             return $this->attributesCache[$key];
116         }
117
118         $allowedAttributes = $this->getAllowedAttributes($object, $context, true);
119
120         if (false !== $allowedAttributes) {
121             if ($context['cache_key']) {
122                 $this->attributesCache[$key] = $allowedAttributes;
123             }
124
125             return $allowedAttributes;
126         }
127
128         if (isset($this->attributesCache[$class])) {
129             return $this->attributesCache[$class];
130         }
131
132         return $this->attributesCache[$class] = $this->extractAttributes($object, $format, $context);
133     }
134
135     /**
136      * Extracts attributes to normalize from the class of the given object, format and context.
137      *
138      * @param object      $object
139      * @param string|null $format
140      * @param array       $context
141      *
142      * @return string[]
143      */
144     abstract protected function extractAttributes($object, $format = null, array $context = array());
145
146     /**
147      * Gets the attribute value.
148      *
149      * @param object      $object
150      * @param string      $attribute
151      * @param string|null $format
152      * @param array       $context
153      *
154      * @return mixed
155      */
156     abstract protected function getAttributeValue($object, $attribute, $format = null, array $context = array());
157
158     /**
159      * {@inheritdoc}
160      */
161     public function supportsDenormalization($data, $type, $format = null)
162     {
163         return class_exists($type);
164     }
165
166     /**
167      * {@inheritdoc}
168      */
169     public function denormalize($data, $class, $format = null, array $context = array())
170     {
171         if (!isset($context['cache_key'])) {
172             $context['cache_key'] = $this->getCacheKey($format, $context);
173         }
174         $allowedAttributes = $this->getAllowedAttributes($class, $context, true);
175         $normalizedData = $this->prepareForDenormalization($data);
176
177         $reflectionClass = new \ReflectionClass($class);
178         $object = $this->instantiateObject($normalizedData, $class, $context, $reflectionClass, $allowedAttributes, $format);
179
180         foreach ($normalizedData as $attribute => $value) {
181             if ($this->nameConverter) {
182                 $attribute = $this->nameConverter->denormalize($attribute);
183             }
184
185             if (($allowedAttributes !== false && !in_array($attribute, $allowedAttributes)) || !$this->isAllowedAttribute($class, $attribute, $format, $context)) {
186                 continue;
187             }
188
189             $value = $this->validateAndDenormalize($class, $attribute, $value, $format, $context);
190             try {
191                 $this->setAttributeValue($object, $attribute, $value, $format, $context);
192             } catch (InvalidArgumentException $e) {
193                 throw new UnexpectedValueException($e->getMessage(), $e->getCode(), $e);
194             }
195         }
196
197         return $object;
198     }
199
200     /**
201      * Sets attribute value.
202      *
203      * @param object      $object
204      * @param string      $attribute
205      * @param mixed       $value
206      * @param string|null $format
207      * @param array       $context
208      */
209     abstract protected function setAttributeValue($object, $attribute, $value, $format = null, array $context = array());
210
211     /**
212      * Validates the submitted data and denormalizes it.
213      *
214      * @param string      $currentClass
215      * @param string      $attribute
216      * @param mixed       $data
217      * @param string|null $format
218      * @param array       $context
219      *
220      * @return mixed
221      *
222      * @throws UnexpectedValueException
223      * @throws LogicException
224      */
225     private function validateAndDenormalize($currentClass, $attribute, $data, $format, array $context)
226     {
227         if (null === $this->propertyTypeExtractor || null === $types = $this->propertyTypeExtractor->getTypes($currentClass, $attribute)) {
228             return $data;
229         }
230
231         $expectedTypes = array();
232         foreach ($types as $type) {
233             if (null === $data && $type->isNullable()) {
234                 return;
235             }
236
237             if ($type->isCollection() && null !== ($collectionValueType = $type->getCollectionValueType()) && Type::BUILTIN_TYPE_OBJECT === $collectionValueType->getBuiltinType()) {
238                 $builtinType = Type::BUILTIN_TYPE_OBJECT;
239                 $class = $collectionValueType->getClassName().'[]';
240
241                 if (null !== $collectionKeyType = $type->getCollectionKeyType()) {
242                     $context['key_type'] = $collectionKeyType;
243                 }
244             } else {
245                 $builtinType = $type->getBuiltinType();
246                 $class = $type->getClassName();
247             }
248
249             $expectedTypes[Type::BUILTIN_TYPE_OBJECT === $builtinType && $class ? $class : $builtinType] = true;
250
251             if (Type::BUILTIN_TYPE_OBJECT === $builtinType) {
252                 if (!$this->serializer instanceof DenormalizerInterface) {
253                     throw new LogicException(sprintf('Cannot denormalize attribute "%s" for class "%s" because injected serializer is not a denormalizer', $attribute, $class));
254                 }
255
256                 if ($this->serializer->supportsDenormalization($data, $class, $format)) {
257                     return $this->serializer->denormalize($data, $class, $format, $context);
258                 }
259             }
260
261             // JSON only has a Number type corresponding to both int and float PHP types.
262             // PHP's json_encode, JavaScript's JSON.stringify, Go's json.Marshal as well as most other JSON encoders convert
263             // floating-point numbers like 12.0 to 12 (the decimal part is dropped when possible).
264             // PHP's json_decode automatically converts Numbers without a decimal part to integers.
265             // To circumvent this behavior, integers are converted to floats when denormalizing JSON based formats and when
266             // a float is expected.
267             if (Type::BUILTIN_TYPE_FLOAT === $builtinType && is_int($data) && false !== strpos($format, JsonEncoder::FORMAT)) {
268                 return (float) $data;
269             }
270
271             if (call_user_func('is_'.$builtinType, $data)) {
272                 return $data;
273             }
274         }
275
276         throw new UnexpectedValueException(sprintf('The type of the "%s" attribute for class "%s" must be one of "%s" ("%s" given).', $attribute, $currentClass, implode('", "', array_keys($expectedTypes)), gettype($data)));
277     }
278
279     /**
280      * Sets an attribute and apply the name converter if necessary.
281      *
282      * @param array  $data
283      * @param string $attribute
284      * @param mixed  $attributeValue
285      *
286      * @return array
287      */
288     private function updateData(array $data, $attribute, $attributeValue)
289     {
290         if ($this->nameConverter) {
291             $attribute = $this->nameConverter->normalize($attribute);
292         }
293
294         $data[$attribute] = $attributeValue;
295
296         return $data;
297     }
298
299     /**
300      * Is the max depth reached for the given attribute?
301      *
302      * @param AttributeMetadataInterface[] $attributesMetadata
303      * @param string                       $class
304      * @param string                       $attribute
305      * @param array                        $context
306      *
307      * @return bool
308      */
309     private function isMaxDepthReached(array $attributesMetadata, $class, $attribute, array &$context)
310     {
311         if (
312             !isset($context[static::ENABLE_MAX_DEPTH]) ||
313             !isset($attributesMetadata[$attribute]) ||
314             null === $maxDepth = $attributesMetadata[$attribute]->getMaxDepth()
315         ) {
316             return false;
317         }
318
319         $key = sprintf(static::DEPTH_KEY_PATTERN, $class, $attribute);
320         if (!isset($context[$key])) {
321             $context[$key] = 1;
322
323             return false;
324         }
325
326         if ($context[$key] === $maxDepth) {
327             return true;
328         }
329
330         ++$context[$key];
331
332         return false;
333     }
334
335     /**
336      * Gets the cache key to use.
337      *
338      * @param string|null $format
339      * @param array       $context
340      *
341      * @return bool|string
342      */
343     private function getCacheKey($format, array $context)
344     {
345         try {
346             return md5($format.serialize($context));
347         } catch (\Exception $exception) {
348             // The context cannot be serialized, skip the cache
349             return false;
350         }
351     }
352 }