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\Factory\ClassMetadataFactoryInterface;
19 use Symfony\Component\Serializer\Mapping\AttributeMetadataInterface;
20 use Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter;
21 use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
24 * Normalizer implementation.
26 * @author Kévin Dunglas <dunglas@gmail.com>
28 abstract class AbstractNormalizer extends SerializerAwareNormalizer implements NormalizerInterface, DenormalizerInterface
30 const CIRCULAR_REFERENCE_LIMIT = 'circular_reference_limit';
31 const OBJECT_TO_POPULATE = 'object_to_populate';
32 const GROUPS = 'groups';
37 protected $circularReferenceLimit = 1;
42 protected $circularReferenceHandler;
45 * @var ClassMetadataFactoryInterface|null
47 protected $classMetadataFactory;
50 * @var NameConverterInterface|null
52 protected $nameConverter;
57 protected $callbacks = array();
62 protected $ignoredAttributes = array();
67 protected $camelizedAttributes = array();
70 * Sets the {@link ClassMetadataFactoryInterface} to use.
72 * @param ClassMetadataFactoryInterface|null $classMetadataFactory
73 * @param NameConverterInterface|null $nameConverter
75 public function __construct(ClassMetadataFactoryInterface $classMetadataFactory = null, NameConverterInterface $nameConverter = null)
77 $this->classMetadataFactory = $classMetadataFactory;
78 $this->nameConverter = $nameConverter;
82 * Set circular reference limit.
84 * @param int $circularReferenceLimit limit of iterations for the same object
88 public function setCircularReferenceLimit($circularReferenceLimit)
90 $this->circularReferenceLimit = $circularReferenceLimit;
96 * Set circular reference handler.
98 * @param callable $circularReferenceHandler
102 * @throws InvalidArgumentException
104 public function setCircularReferenceHandler($circularReferenceHandler)
106 if (!is_callable($circularReferenceHandler)) {
107 throw new InvalidArgumentException('The given circular reference handler is not callable.');
110 $this->circularReferenceHandler = $circularReferenceHandler;
116 * Set normalization callbacks.
118 * @param callable[] $callbacks help normalize the result
122 * @throws InvalidArgumentException if a non-callable callback is set
124 public function setCallbacks(array $callbacks)
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.',
134 $this->callbacks = $callbacks;
140 * Set ignored attributes for normalization and denormalization.
142 * @param array $ignoredAttributes
146 public function setIgnoredAttributes(array $ignoredAttributes)
148 $this->ignoredAttributes = $ignoredAttributes;
154 * Set attributes to be camelized on denormalize.
156 * @deprecated Deprecated since version 2.7, to be removed in 3.0. Use Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter instead.
158 * @param array $camelizedAttributes
162 * @throws LogicException
164 public function setCamelizedAttributes(array $camelizedAttributes)
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);
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__));
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));
179 $this->nameConverter = new CamelCaseToSnakeCaseNameConverter($attributes);
185 * Detects if the configured circular reference limit is reached.
187 * @param object $object
188 * @param array $context
192 * @throws CircularReferenceException
194 protected function isCircularReference($object, &$context)
196 $objectHash = spl_object_hash($object);
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]);
205 ++$context[static::CIRCULAR_REFERENCE_LIMIT][$objectHash];
207 $context[static::CIRCULAR_REFERENCE_LIMIT][$objectHash] = 1;
214 * Handles a circular reference.
216 * If a circular reference handler is set, it will be called. Otherwise, a
217 * {@class CircularReferenceException} will be thrown.
219 * @param object $object
223 * @throws CircularReferenceException
225 protected function handleCircularReference($object)
227 if ($this->circularReferenceHandler) {
228 return call_user_func($this->circularReferenceHandler, $object);
231 throw new CircularReferenceException(sprintf('A circular reference has been detected (configured limit: %d).', $this->circularReferenceLimit));
235 * Format an attribute name, for example to convert a snake_case name to camelCase.
237 * @deprecated Deprecated since version 2.7, to be removed in 3.0. Use Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter instead.
239 * @param string $attributeName
243 protected function formatAttribute($attributeName)
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);
247 return $this->nameConverter ? $this->nameConverter->normalize($attributeName) : $attributeName;
251 * Gets attributes to normalize using groups.
253 * @param string|object $classOrObject
254 * @param array $context
255 * @param bool $attributesAsString If false, return an array of {@link AttributeMetadataInterface}
257 * @return string[]|AttributeMetadataInterface[]|bool
259 protected function getAllowedAttributes($classOrObject, array $context, $attributesAsString = false)
261 if (!$this->classMetadataFactory || !isset($context[static::GROUPS]) || !is_array($context[static::GROUPS])) {
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;
272 return $allowedAttributes;
276 * Normalizes the given data to an array. It's particularly useful during
277 * the denormalization process.
279 * @param object|array $data
283 protected function prepareForDenormalization($data)
285 return (array) $data;
289 * Instantiates an object using constructor parameters when needed.
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.
297 * @param string $class
298 * @param array $context
299 * @param \ReflectionClass $reflectionClass
300 * @param array|bool $allowedAttributes
304 * @throws RuntimeException
306 protected function instantiateObject(array &$data, $class, array &$context, \ReflectionClass $reflectionClass, $allowedAttributes)
309 isset($context[static::OBJECT_TO_POPULATE]) &&
310 is_object($context[static::OBJECT_TO_POPULATE]) &&
311 $context[static::OBJECT_TO_POPULATE] instanceof $class
313 $object = $context[static::OBJECT_TO_POPULATE];
314 unset($context[static::OBJECT_TO_POPULATE]);
319 $constructor = $reflectionClass->getConstructor();
321 $constructorParameters = $constructor->getParameters();
324 foreach ($constructorParameters as $constructorParameter) {
325 $paramName = $constructorParameter->name;
326 $key = $this->nameConverter ? $this->nameConverter->normalize($paramName) : $paramName;
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));
336 $params = array_merge($params, $data[$paramName]);
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
342 } elseif ($constructorParameter->isDefaultValueAvailable()) {
343 $params[] = $constructorParameter->getDefaultValue();
345 throw new RuntimeException(
347 'Cannot create an instance of %s from serialized data because its constructor requires parameter "%s" to be present.',
349 $constructorParameter->name
355 return $reflectionClass->newInstanceArgs($params);