2bdf120ae299caaeb981a2d5a516e1b1b4f166bb
[yaffs-website] / serializer / Normalizer / AbstractNormalizer.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\Serializer\Exception\CircularReferenceException;
15 use Symfony\Component\Serializer\Exception\InvalidArgumentException;
16 use Symfony\Component\Serializer\Exception\LogicException;
17 use Symfony\Component\Serializer\Exception\RuntimeException;
18 use Symfony\Component\Serializer\Mapping\AttributeMetadataInterface;
19 use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface;
20 use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
21 use Symfony\Component\Serializer\SerializerAwareInterface;
22
23 /**
24  * Normalizer implementation.
25  *
26  * @author Kévin Dunglas <dunglas@gmail.com>
27  */
28 abstract class AbstractNormalizer extends SerializerAwareNormalizer implements NormalizerInterface, DenormalizerInterface, SerializerAwareInterface
29 {
30     use ObjectToPopulateTrait;
31
32     const CIRCULAR_REFERENCE_LIMIT = 'circular_reference_limit';
33     const OBJECT_TO_POPULATE = 'object_to_populate';
34     const GROUPS = 'groups';
35     const ATTRIBUTES = 'attributes';
36     const ALLOW_EXTRA_ATTRIBUTES = 'allow_extra_attributes';
37
38     /**
39      * @var int
40      */
41     protected $circularReferenceLimit = 1;
42
43     /**
44      * @var callable
45      */
46     protected $circularReferenceHandler;
47
48     /**
49      * @var ClassMetadataFactoryInterface|null
50      */
51     protected $classMetadataFactory;
52
53     /**
54      * @var NameConverterInterface|null
55      */
56     protected $nameConverter;
57
58     /**
59      * @var array
60      */
61     protected $callbacks = array();
62
63     /**
64      * @var array
65      */
66     protected $ignoredAttributes = array();
67
68     /**
69      * @var array
70      */
71     protected $camelizedAttributes = array();
72
73     /**
74      * Sets the {@link ClassMetadataFactoryInterface} to use.
75      */
76     public function __construct(ClassMetadataFactoryInterface $classMetadataFactory = null, NameConverterInterface $nameConverter = null)
77     {
78         $this->classMetadataFactory = $classMetadataFactory;
79         $this->nameConverter = $nameConverter;
80     }
81
82     /**
83      * Set circular reference limit.
84      *
85      * @param int $circularReferenceLimit Limit of iterations for the same object
86      *
87      * @return self
88      */
89     public function setCircularReferenceLimit($circularReferenceLimit)
90     {
91         $this->circularReferenceLimit = $circularReferenceLimit;
92
93         return $this;
94     }
95
96     /**
97      * Set circular reference handler.
98      *
99      * @param callable $circularReferenceHandler
100      *
101      * @return self
102      */
103     public function setCircularReferenceHandler(callable $circularReferenceHandler)
104     {
105         $this->circularReferenceHandler = $circularReferenceHandler;
106
107         return $this;
108     }
109
110     /**
111      * Set normalization callbacks.
112      *
113      * @param callable[] $callbacks Help normalize the result
114      *
115      * @return self
116      *
117      * @throws InvalidArgumentException if a non-callable callback is set
118      */
119     public function setCallbacks(array $callbacks)
120     {
121         foreach ($callbacks as $attribute => $callback) {
122             if (!\is_callable($callback)) {
123                 throw new InvalidArgumentException(sprintf('The given callback for attribute "%s" is not callable.', $attribute));
124             }
125         }
126         $this->callbacks = $callbacks;
127
128         return $this;
129     }
130
131     /**
132      * Set ignored attributes for normalization and denormalization.
133      *
134      * @return self
135      */
136     public function setIgnoredAttributes(array $ignoredAttributes)
137     {
138         $this->ignoredAttributes = $ignoredAttributes;
139
140         return $this;
141     }
142
143     /**
144      * Detects if the configured circular reference limit is reached.
145      *
146      * @param object $object
147      * @param array  $context
148      *
149      * @return bool
150      *
151      * @throws CircularReferenceException
152      */
153     protected function isCircularReference($object, &$context)
154     {
155         $objectHash = spl_object_hash($object);
156
157         if (isset($context[static::CIRCULAR_REFERENCE_LIMIT][$objectHash])) {
158             if ($context[static::CIRCULAR_REFERENCE_LIMIT][$objectHash] >= $this->circularReferenceLimit) {
159                 unset($context[static::CIRCULAR_REFERENCE_LIMIT][$objectHash]);
160
161                 return true;
162             }
163
164             ++$context[static::CIRCULAR_REFERENCE_LIMIT][$objectHash];
165         } else {
166             $context[static::CIRCULAR_REFERENCE_LIMIT][$objectHash] = 1;
167         }
168
169         return false;
170     }
171
172     /**
173      * Handles a circular reference.
174      *
175      * If a circular reference handler is set, it will be called. Otherwise, a
176      * {@class CircularReferenceException} will be thrown.
177      *
178      * @param object $object
179      *
180      * @return mixed
181      *
182      * @throws CircularReferenceException
183      */
184     protected function handleCircularReference($object)
185     {
186         if ($this->circularReferenceHandler) {
187             return \call_user_func($this->circularReferenceHandler, $object);
188         }
189
190         throw new CircularReferenceException(sprintf('A circular reference has been detected when serializing the object of class "%s" (configured limit: %d)', \get_class($object), $this->circularReferenceLimit));
191     }
192
193     /**
194      * Gets attributes to normalize using groups.
195      *
196      * @param string|object $classOrObject
197      * @param array         $context
198      * @param bool          $attributesAsString If false, return an array of {@link AttributeMetadataInterface}
199      *
200      * @throws LogicException if the 'allow_extra_attributes' context variable is false and no class metadata factory is provided
201      *
202      * @return string[]|AttributeMetadataInterface[]|bool
203      */
204     protected function getAllowedAttributes($classOrObject, array $context, $attributesAsString = false)
205     {
206         if (!$this->classMetadataFactory) {
207             if (isset($context[static::ALLOW_EXTRA_ATTRIBUTES]) && !$context[static::ALLOW_EXTRA_ATTRIBUTES]) {
208                 throw new LogicException(sprintf('A class metadata factory must be provided in the constructor when setting "%s" to false.', static::ALLOW_EXTRA_ATTRIBUTES));
209             }
210
211             return false;
212         }
213
214         $groups = false;
215         if (isset($context[static::GROUPS]) && \is_array($context[static::GROUPS])) {
216             $groups = $context[static::GROUPS];
217         } elseif (!isset($context[static::ALLOW_EXTRA_ATTRIBUTES]) || $context[static::ALLOW_EXTRA_ATTRIBUTES]) {
218             return false;
219         }
220
221         $allowedAttributes = array();
222         foreach ($this->classMetadataFactory->getMetadataFor($classOrObject)->getAttributesMetadata() as $attributeMetadata) {
223             $name = $attributeMetadata->getName();
224
225             if (
226                 (false === $groups || array_intersect($attributeMetadata->getGroups(), $groups)) &&
227                 $this->isAllowedAttribute($classOrObject, $name, null, $context)
228             ) {
229                 $allowedAttributes[] = $attributesAsString ? $name : $attributeMetadata;
230             }
231         }
232
233         return $allowedAttributes;
234     }
235
236     /**
237      * Is this attribute allowed?
238      *
239      * @param object|string $classOrObject
240      * @param string        $attribute
241      * @param string|null   $format
242      * @param array         $context
243      *
244      * @return bool
245      */
246     protected function isAllowedAttribute($classOrObject, $attribute, $format = null, array $context = array())
247     {
248         if (\in_array($attribute, $this->ignoredAttributes)) {
249             return false;
250         }
251
252         if (isset($context[self::ATTRIBUTES][$attribute])) {
253             // Nested attributes
254             return true;
255         }
256
257         if (isset($context[self::ATTRIBUTES]) && \is_array($context[self::ATTRIBUTES])) {
258             return \in_array($attribute, $context[self::ATTRIBUTES], true);
259         }
260
261         return true;
262     }
263
264     /**
265      * Normalizes the given data to an array. It's particularly useful during
266      * the denormalization process.
267      *
268      * @param object|array $data
269      *
270      * @return array
271      */
272     protected function prepareForDenormalization($data)
273     {
274         return (array) $data;
275     }
276
277     /**
278      * Returns the method to use to construct an object. This method must be either
279      * the object constructor or static.
280      *
281      * @param array            $data
282      * @param string           $class
283      * @param array            $context
284      * @param \ReflectionClass $reflectionClass
285      * @param array|bool       $allowedAttributes
286      *
287      * @return \ReflectionMethod|null
288      */
289     protected function getConstructor(array &$data, $class, array &$context, \ReflectionClass $reflectionClass, $allowedAttributes)
290     {
291         return $reflectionClass->getConstructor();
292     }
293
294     /**
295      * Instantiates an object using constructor parameters when needed.
296      *
297      * This method also allows to denormalize data into an existing object if
298      * it is present in the context with the object_to_populate. This object
299      * is removed from the context before being returned to avoid side effects
300      * when recursively normalizing an object graph.
301      *
302      * @param array            $data
303      * @param string           $class
304      * @param array            $context
305      * @param \ReflectionClass $reflectionClass
306      * @param array|bool       $allowedAttributes
307      * @param string|null      $format
308      *
309      * @return object
310      *
311      * @throws RuntimeException
312      */
313     protected function instantiateObject(array &$data, $class, array &$context, \ReflectionClass $reflectionClass, $allowedAttributes/*, string $format = null*/)
314     {
315         if (\func_num_args() >= 6) {
316             $format = \func_get_arg(5);
317         } else {
318             if (__CLASS__ !== \get_class($this)) {
319                 $r = new \ReflectionMethod($this, __FUNCTION__);
320                 if (__CLASS__ !== $r->getDeclaringClass()->getName()) {
321                     @trigger_error(sprintf('Method %s::%s() will have a 6th `string $format = null` argument in version 4.0. Not defining it is deprecated since Symfony 3.2.', \get_class($this), __FUNCTION__), E_USER_DEPRECATED);
322                 }
323             }
324
325             $format = null;
326         }
327
328         if (null !== $object = $this->extractObjectToPopulate($class, $context, static::OBJECT_TO_POPULATE)) {
329             unset($context[static::OBJECT_TO_POPULATE]);
330
331             return $object;
332         }
333
334         $constructor = $this->getConstructor($data, $class, $context, $reflectionClass, $allowedAttributes);
335         if ($constructor) {
336             $constructorParameters = $constructor->getParameters();
337
338             $params = array();
339             foreach ($constructorParameters as $constructorParameter) {
340                 $paramName = $constructorParameter->name;
341                 $key = $this->nameConverter ? $this->nameConverter->normalize($paramName) : $paramName;
342
343                 $allowed = false === $allowedAttributes || \in_array($paramName, $allowedAttributes);
344                 $ignored = !$this->isAllowedAttribute($class, $paramName, $format, $context);
345                 if (method_exists($constructorParameter, 'isVariadic') && $constructorParameter->isVariadic()) {
346                     if ($allowed && !$ignored && (isset($data[$key]) || array_key_exists($key, $data))) {
347                         if (!\is_array($data[$paramName])) {
348                             throw new RuntimeException(sprintf('Cannot create an instance of %s from serialized data because the variadic parameter %s can only accept an array.', $class, $constructorParameter->name));
349                         }
350
351                         $params = array_merge($params, $data[$paramName]);
352                     }
353                 } elseif ($allowed && !$ignored && (isset($data[$key]) || array_key_exists($key, $data))) {
354                     $parameterData = $data[$key];
355                     if (null === $parameterData && $constructorParameter->allowsNull()) {
356                         $params[] = null;
357                         // Don't run set for a parameter passed to the constructor
358                         unset($data[$key]);
359                         continue;
360                     }
361                     try {
362                         if (null !== $constructorParameter->getClass()) {
363                             if (!$this->serializer instanceof DenormalizerInterface) {
364                                 throw new LogicException(sprintf('Cannot create an instance of %s from serialized data because the serializer inject in "%s" is not a denormalizer', $constructorParameter->getClass(), static::class));
365                             }
366                             $parameterClass = $constructorParameter->getClass()->getName();
367                             $parameterData = $this->serializer->denormalize($parameterData, $parameterClass, $format, $this->createChildContext($context, $paramName));
368                         }
369                     } catch (\ReflectionException $e) {
370                         throw new RuntimeException(sprintf('Could not determine the class of the parameter "%s".', $key), 0, $e);
371                     }
372
373                     // Don't run set for a parameter passed to the constructor
374                     $params[] = $parameterData;
375                     unset($data[$key]);
376                 } elseif ($constructorParameter->isDefaultValueAvailable()) {
377                     $params[] = $constructorParameter->getDefaultValue();
378                 } else {
379                     throw new RuntimeException(sprintf('Cannot create an instance of %s from serialized data because its constructor requires parameter "%s" to be present.', $class, $constructorParameter->name));
380                 }
381             }
382
383             if ($constructor->isConstructor()) {
384                 return $reflectionClass->newInstanceArgs($params);
385             } else {
386                 return $constructor->invokeArgs(null, $params);
387             }
388         }
389
390         return new $class();
391     }
392
393     /**
394      * @param array  $parentContext
395      * @param string $attribute
396      *
397      * @return array
398      *
399      * @internal
400      */
401     protected function createChildContext(array $parentContext, $attribute)
402     {
403         if (isset($parentContext[self::ATTRIBUTES][$attribute])) {
404             $parentContext[self::ATTRIBUTES] = $parentContext[self::ATTRIBUTES][$attribute];
405         } else {
406             unset($parentContext[self::ATTRIBUTES]);
407         }
408
409         return $parentContext;
410     }
411 }