4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Serializer\Normalizer;
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;
24 * Normalizer implementation.
26 * @author Kévin Dunglas <dunglas@gmail.com>
28 abstract class AbstractNormalizer extends SerializerAwareNormalizer implements NormalizerInterface, DenormalizerInterface, SerializerAwareInterface
30 use ObjectToPopulateTrait;
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';
41 protected $circularReferenceLimit = 1;
46 protected $circularReferenceHandler;
49 * @var ClassMetadataFactoryInterface|null
51 protected $classMetadataFactory;
54 * @var NameConverterInterface|null
56 protected $nameConverter;
61 protected $callbacks = array();
66 protected $ignoredAttributes = array();
71 protected $camelizedAttributes = array();
74 * Sets the {@link ClassMetadataFactoryInterface} to use.
76 public function __construct(ClassMetadataFactoryInterface $classMetadataFactory = null, NameConverterInterface $nameConverter = null)
78 $this->classMetadataFactory = $classMetadataFactory;
79 $this->nameConverter = $nameConverter;
83 * Set circular reference limit.
85 * @param int $circularReferenceLimit Limit of iterations for the same object
89 public function setCircularReferenceLimit($circularReferenceLimit)
91 $this->circularReferenceLimit = $circularReferenceLimit;
97 * Set circular reference handler.
99 * @param callable $circularReferenceHandler
103 public function setCircularReferenceHandler(callable $circularReferenceHandler)
105 $this->circularReferenceHandler = $circularReferenceHandler;
111 * Set normalization callbacks.
113 * @param callable[] $callbacks Help normalize the result
117 * @throws InvalidArgumentException if a non-callable callback is set
119 public function setCallbacks(array $callbacks)
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));
126 $this->callbacks = $callbacks;
132 * Set ignored attributes for normalization and denormalization.
136 public function setIgnoredAttributes(array $ignoredAttributes)
138 $this->ignoredAttributes = $ignoredAttributes;
144 * Detects if the configured circular reference limit is reached.
146 * @param object $object
147 * @param array $context
151 * @throws CircularReferenceException
153 protected function isCircularReference($object, &$context)
155 $objectHash = spl_object_hash($object);
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]);
164 ++$context[static::CIRCULAR_REFERENCE_LIMIT][$objectHash];
166 $context[static::CIRCULAR_REFERENCE_LIMIT][$objectHash] = 1;
173 * Handles a circular reference.
175 * If a circular reference handler is set, it will be called. Otherwise, a
176 * {@class CircularReferenceException} will be thrown.
178 * @param object $object
182 * @throws CircularReferenceException
184 protected function handleCircularReference($object)
186 if ($this->circularReferenceHandler) {
187 return \call_user_func($this->circularReferenceHandler, $object);
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));
194 * Gets attributes to normalize using groups.
196 * @param string|object $classOrObject
197 * @param array $context
198 * @param bool $attributesAsString If false, return an array of {@link AttributeMetadataInterface}
200 * @throws LogicException if the 'allow_extra_attributes' context variable is false and no class metadata factory is provided
202 * @return string[]|AttributeMetadataInterface[]|bool
204 protected function getAllowedAttributes($classOrObject, array $context, $attributesAsString = false)
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));
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]) {
221 $allowedAttributes = array();
222 foreach ($this->classMetadataFactory->getMetadataFor($classOrObject)->getAttributesMetadata() as $attributeMetadata) {
223 $name = $attributeMetadata->getName();
226 (false === $groups || array_intersect($attributeMetadata->getGroups(), $groups)) &&
227 $this->isAllowedAttribute($classOrObject, $name, null, $context)
229 $allowedAttributes[] = $attributesAsString ? $name : $attributeMetadata;
233 return $allowedAttributes;
237 * Is this attribute allowed?
239 * @param object|string $classOrObject
240 * @param string $attribute
241 * @param string|null $format
242 * @param array $context
246 protected function isAllowedAttribute($classOrObject, $attribute, $format = null, array $context = array())
248 if (\in_array($attribute, $this->ignoredAttributes)) {
252 if (isset($context[self::ATTRIBUTES][$attribute])) {
257 if (isset($context[self::ATTRIBUTES]) && \is_array($context[self::ATTRIBUTES])) {
258 return \in_array($attribute, $context[self::ATTRIBUTES], true);
265 * Normalizes the given data to an array. It's particularly useful during
266 * the denormalization process.
268 * @param object|array $data
272 protected function prepareForDenormalization($data)
274 return (array) $data;
278 * Returns the method to use to construct an object. This method must be either
279 * the object constructor or static.
282 * @param string $class
283 * @param array $context
284 * @param \ReflectionClass $reflectionClass
285 * @param array|bool $allowedAttributes
287 * @return \ReflectionMethod|null
289 protected function getConstructor(array &$data, $class, array &$context, \ReflectionClass $reflectionClass, $allowedAttributes)
291 return $reflectionClass->getConstructor();
295 * Instantiates an object using constructor parameters when needed.
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.
303 * @param string $class
304 * @param array $context
305 * @param \ReflectionClass $reflectionClass
306 * @param array|bool $allowedAttributes
307 * @param string|null $format
311 * @throws RuntimeException
313 protected function instantiateObject(array &$data, $class, array &$context, \ReflectionClass $reflectionClass, $allowedAttributes/*, string $format = null*/)
315 if (\func_num_args() >= 6) {
316 $format = \func_get_arg(5);
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);
328 if (null !== $object = $this->extractObjectToPopulate($class, $context, static::OBJECT_TO_POPULATE)) {
329 unset($context[static::OBJECT_TO_POPULATE]);
334 $constructor = $this->getConstructor($data, $class, $context, $reflectionClass, $allowedAttributes);
336 $constructorParameters = $constructor->getParameters();
339 foreach ($constructorParameters as $constructorParameter) {
340 $paramName = $constructorParameter->name;
341 $key = $this->nameConverter ? $this->nameConverter->normalize($paramName) : $paramName;
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));
351 $params = array_merge($params, $data[$paramName]);
353 } elseif ($allowed && !$ignored && (isset($data[$key]) || array_key_exists($key, $data))) {
354 $parameterData = $data[$key];
355 if (null === $parameterData && $constructorParameter->allowsNull()) {
357 // Don't run set for a parameter passed to the constructor
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));
366 $parameterClass = $constructorParameter->getClass()->getName();
367 $parameterData = $this->serializer->denormalize($parameterData, $parameterClass, $format, $this->createChildContext($context, $paramName));
369 } catch (\ReflectionException $e) {
370 throw new RuntimeException(sprintf('Could not determine the class of the parameter "%s".', $key), 0, $e);
373 // Don't run set for a parameter passed to the constructor
374 $params[] = $parameterData;
376 } elseif ($constructorParameter->isDefaultValueAvailable()) {
377 $params[] = $constructorParameter->getDefaultValue();
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));
383 if ($constructor->isConstructor()) {
384 return $reflectionClass->newInstanceArgs($params);
386 return $constructor->invokeArgs(null, $params);
394 * @param array $parentContext
395 * @param string $attribute
401 protected function createChildContext(array $parentContext, $attribute)
403 if (isset($parentContext[self::ATTRIBUTES][$attribute])) {
404 $parentContext[self::ATTRIBUTES] = $parentContext[self::ATTRIBUTES][$attribute];
406 unset($parentContext[self::ATTRIBUTES]);
409 return $parentContext;