Version 1
[yaffs-website] / vendor / symfony / 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\Factory\ClassMetadataFactoryInterface;
19 use Symfony\Component\Serializer\Mapping\AttributeMetadataInterface;
20 use Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter;
21 use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
22
23 /**
24  * Normalizer implementation.
25  *
26  * @author Kévin Dunglas <dunglas@gmail.com>
27  */
28 abstract class AbstractNormalizer extends SerializerAwareNormalizer implements NormalizerInterface, DenormalizerInterface
29 {
30     const CIRCULAR_REFERENCE_LIMIT = 'circular_reference_limit';
31     const OBJECT_TO_POPULATE = 'object_to_populate';
32     const GROUPS = 'groups';
33
34     /**
35      * @var int
36      */
37     protected $circularReferenceLimit = 1;
38
39     /**
40      * @var callable
41      */
42     protected $circularReferenceHandler;
43
44     /**
45      * @var ClassMetadataFactoryInterface|null
46      */
47     protected $classMetadataFactory;
48
49     /**
50      * @var NameConverterInterface|null
51      */
52     protected $nameConverter;
53
54     /**
55      * @var array
56      */
57     protected $callbacks = array();
58
59     /**
60      * @var array
61      */
62     protected $ignoredAttributes = array();
63
64     /**
65      * @var array
66      */
67     protected $camelizedAttributes = array();
68
69     /**
70      * Sets the {@link ClassMetadataFactoryInterface} to use.
71      *
72      * @param ClassMetadataFactoryInterface|null $classMetadataFactory
73      * @param NameConverterInterface|null        $nameConverter
74      */
75     public function __construct(ClassMetadataFactoryInterface $classMetadataFactory = null, NameConverterInterface $nameConverter = null)
76     {
77         $this->classMetadataFactory = $classMetadataFactory;
78         $this->nameConverter = $nameConverter;
79     }
80
81     /**
82      * Set circular reference limit.
83      *
84      * @param int $circularReferenceLimit limit of iterations for the same object
85      *
86      * @return self
87      */
88     public function setCircularReferenceLimit($circularReferenceLimit)
89     {
90         $this->circularReferenceLimit = $circularReferenceLimit;
91
92         return $this;
93     }
94
95     /**
96      * Set circular reference handler.
97      *
98      * @param callable $circularReferenceHandler
99      *
100      * @return self
101      *
102      * @throws InvalidArgumentException
103      */
104     public function setCircularReferenceHandler($circularReferenceHandler)
105     {
106         if (!is_callable($circularReferenceHandler)) {
107             throw new InvalidArgumentException('The given circular reference handler is not callable.');
108         }
109
110         $this->circularReferenceHandler = $circularReferenceHandler;
111
112         return $this;
113     }
114
115     /**
116      * Set normalization callbacks.
117      *
118      * @param callable[] $callbacks help normalize the result
119      *
120      * @return self
121      *
122      * @throws InvalidArgumentException if a non-callable callback is set
123      */
124     public function setCallbacks(array $callbacks)
125     {
126         foreach ($callbacks as $attribute => $callback) {
127             if (!is_callable($callback)) {
128                 throw new InvalidArgumentException(sprintf(
129                     'The given callback for attribute "%s" is not callable.',
130                     $attribute
131                 ));
132             }
133         }
134         $this->callbacks = $callbacks;
135
136         return $this;
137     }
138
139     /**
140      * Set ignored attributes for normalization and denormalization.
141      *
142      * @param array $ignoredAttributes
143      *
144      * @return self
145      */
146     public function setIgnoredAttributes(array $ignoredAttributes)
147     {
148         $this->ignoredAttributes = $ignoredAttributes;
149
150         return $this;
151     }
152
153     /**
154      * Set attributes to be camelized on denormalize.
155      *
156      * @deprecated Deprecated since version 2.7, to be removed in 3.0. Use Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter instead.
157      *
158      * @param array $camelizedAttributes
159      *
160      * @return self
161      *
162      * @throws LogicException
163      */
164     public function setCamelizedAttributes(array $camelizedAttributes)
165     {
166         @trigger_error(sprintf('%s is deprecated since version 2.7 and will be removed in 3.0. Use Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter instead.', __METHOD__), E_USER_DEPRECATED);
167
168         if ($this->nameConverter && !$this->nameConverter instanceof CamelCaseToSnakeCaseNameConverter) {
169             throw new LogicException(sprintf('%s cannot be called if a custom Name Converter is defined.', __METHOD__));
170         }
171
172         $attributes = array();
173         foreach ($camelizedAttributes as $camelizedAttribute) {
174             $attributes[] = lcfirst(preg_replace_callback('/(^|_|\.)+(.)/', function ($match) {
175                 return ('.' === $match[1] ? '_' : '').strtoupper($match[2]);
176             }, $camelizedAttribute));
177         }
178
179         $this->nameConverter = new CamelCaseToSnakeCaseNameConverter($attributes);
180
181         return $this;
182     }
183
184     /**
185      * Detects if the configured circular reference limit is reached.
186      *
187      * @param object $object
188      * @param array  $context
189      *
190      * @return bool
191      *
192      * @throws CircularReferenceException
193      */
194     protected function isCircularReference($object, &$context)
195     {
196         $objectHash = spl_object_hash($object);
197
198         if (isset($context[static::CIRCULAR_REFERENCE_LIMIT][$objectHash])) {
199             if ($context[static::CIRCULAR_REFERENCE_LIMIT][$objectHash] >= $this->circularReferenceLimit) {
200                 unset($context[static::CIRCULAR_REFERENCE_LIMIT][$objectHash]);
201
202                 return true;
203             }
204
205             ++$context[static::CIRCULAR_REFERENCE_LIMIT][$objectHash];
206         } else {
207             $context[static::CIRCULAR_REFERENCE_LIMIT][$objectHash] = 1;
208         }
209
210         return false;
211     }
212
213     /**
214      * Handles a circular reference.
215      *
216      * If a circular reference handler is set, it will be called. Otherwise, a
217      * {@class CircularReferenceException} will be thrown.
218      *
219      * @param object $object
220      *
221      * @return mixed
222      *
223      * @throws CircularReferenceException
224      */
225     protected function handleCircularReference($object)
226     {
227         if ($this->circularReferenceHandler) {
228             return call_user_func($this->circularReferenceHandler, $object);
229         }
230
231         throw new CircularReferenceException(sprintf('A circular reference has been detected (configured limit: %d).', $this->circularReferenceLimit));
232     }
233
234     /**
235      * Format an attribute name, for example to convert a snake_case name to camelCase.
236      *
237      * @deprecated Deprecated since version 2.7, to be removed in 3.0. Use Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter instead.
238      *
239      * @param string $attributeName
240      *
241      * @return string
242      */
243     protected function formatAttribute($attributeName)
244     {
245         @trigger_error(sprintf('%s is deprecated since version 2.7 and will be removed in 3.0. Use Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter instead.', __METHOD__), E_USER_DEPRECATED);
246
247         return $this->nameConverter ? $this->nameConverter->normalize($attributeName) : $attributeName;
248     }
249
250     /**
251      * Gets attributes to normalize using groups.
252      *
253      * @param string|object $classOrObject
254      * @param array         $context
255      * @param bool          $attributesAsString If false, return an array of {@link AttributeMetadataInterface}
256      *
257      * @return string[]|AttributeMetadataInterface[]|bool
258      */
259     protected function getAllowedAttributes($classOrObject, array $context, $attributesAsString = false)
260     {
261         if (!$this->classMetadataFactory || !isset($context[static::GROUPS]) || !is_array($context[static::GROUPS])) {
262             return false;
263         }
264
265         $allowedAttributes = array();
266         foreach ($this->classMetadataFactory->getMetadataFor($classOrObject)->getAttributesMetadata() as $attributeMetadata) {
267             if (count(array_intersect($attributeMetadata->getGroups(), $context[static::GROUPS]))) {
268                 $allowedAttributes[] = $attributesAsString ? $attributeMetadata->getName() : $attributeMetadata;
269             }
270         }
271
272         return $allowedAttributes;
273     }
274
275     /**
276      * Normalizes the given data to an array. It's particularly useful during
277      * the denormalization process.
278      *
279      * @param object|array $data
280      *
281      * @return array
282      */
283     protected function prepareForDenormalization($data)
284     {
285         return (array) $data;
286     }
287
288     /**
289      * Instantiates an object using constructor parameters when needed.
290      *
291      * This method also allows to denormalize data into an existing object if
292      * it is present in the context with the object_to_populate. This object
293      * is removed from the context before being returned to avoid side effects
294      * when recursively normalizing an object graph.
295      *
296      * @param array            $data
297      * @param string           $class
298      * @param array            $context
299      * @param \ReflectionClass $reflectionClass
300      * @param array|bool       $allowedAttributes
301      *
302      * @return object
303      *
304      * @throws RuntimeException
305      */
306     protected function instantiateObject(array &$data, $class, array &$context, \ReflectionClass $reflectionClass, $allowedAttributes)
307     {
308         if (
309             isset($context[static::OBJECT_TO_POPULATE]) &&
310             is_object($context[static::OBJECT_TO_POPULATE]) &&
311             $context[static::OBJECT_TO_POPULATE] instanceof $class
312         ) {
313             $object = $context[static::OBJECT_TO_POPULATE];
314             unset($context[static::OBJECT_TO_POPULATE]);
315
316             return $object;
317         }
318
319         $constructor = $reflectionClass->getConstructor();
320         if ($constructor) {
321             $constructorParameters = $constructor->getParameters();
322
323             $params = array();
324             foreach ($constructorParameters as $constructorParameter) {
325                 $paramName = $constructorParameter->name;
326                 $key = $this->nameConverter ? $this->nameConverter->normalize($paramName) : $paramName;
327
328                 $allowed = $allowedAttributes === false || in_array($paramName, $allowedAttributes);
329                 $ignored = in_array($paramName, $this->ignoredAttributes);
330                 if (method_exists($constructorParameter, 'isVariadic') && $constructorParameter->isVariadic()) {
331                     if ($allowed && !$ignored && (isset($data[$key]) || array_key_exists($key, $data))) {
332                         if (!is_array($data[$paramName])) {
333                             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));
334                         }
335
336                         $params = array_merge($params, $data[$paramName]);
337                     }
338                 } elseif ($allowed && !$ignored && (isset($data[$key]) || array_key_exists($key, $data))) {
339                     $params[] = $data[$key];
340                     // don't run set for a parameter passed to the constructor
341                     unset($data[$key]);
342                 } elseif ($constructorParameter->isDefaultValueAvailable()) {
343                     $params[] = $constructorParameter->getDefaultValue();
344                 } else {
345                     throw new RuntimeException(
346                         sprintf(
347                             'Cannot create an instance of %s from serialized data because its constructor requires parameter "%s" to be present.',
348                             $class,
349                             $constructorParameter->name
350                         )
351                     );
352                 }
353             }
354
355             return $reflectionClass->newInstanceArgs($params);
356         }
357
358         return new $class();
359     }
360 }