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\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;
25 * Base class for a normalizer dealing with objects.
27 * @author Kévin Dunglas <dunglas@gmail.com>
29 abstract class AbstractObjectNormalizer extends AbstractNormalizer
31 const ENABLE_MAX_DEPTH = 'enable_max_depth';
32 const DEPTH_KEY_PATTERN = 'depth_%s::%s';
34 private $propertyTypeExtractor;
35 private $attributesCache = array();
37 public function __construct(ClassMetadataFactoryInterface $classMetadataFactory = null, NameConverterInterface $nameConverter = null, PropertyTypeExtractorInterface $propertyTypeExtractor = null)
39 parent::__construct($classMetadataFactory, $nameConverter);
41 $this->propertyTypeExtractor = $propertyTypeExtractor;
47 public function supportsNormalization($data, $format = null)
49 return is_object($data) && !$data instanceof \Traversable;
55 public function normalize($object, $format = null, array $context = array())
57 if (!isset($context['cache_key'])) {
58 $context['cache_key'] = $this->getCacheKey($format, $context);
61 if ($this->isCircularReference($object, $context)) {
62 return $this->handleCircularReference($object);
67 $attributes = $this->getAttributes($object, $format, $context);
68 $class = get_class($object);
69 $attributesMetadata = $this->classMetadataFactory ? $this->classMetadataFactory->getMetadataFor($class)->getAttributesMetadata() : null;
71 foreach ($attributes as $attribute) {
72 if (null !== $attributesMetadata && $this->isMaxDepthReached($attributesMetadata, $class, $attribute, $context)) {
76 $attributeValue = $this->getAttributeValue($object, $attribute, $format, $context);
78 if (isset($this->callbacks[$attribute])) {
79 $attributeValue = call_user_func($this->callbacks[$attribute], $attributeValue);
82 if (null !== $attributeValue && !is_scalar($attributeValue)) {
83 $stack[$attribute] = $attributeValue;
86 $data = $this->updateData($data, $attribute, $attributeValue);
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));
94 $data = $this->updateData($data, $attribute, $this->serializer->normalize($attributeValue, $format, $context));
101 * Gets and caches attributes for the given object, format and context.
103 * @param object $object
104 * @param string|null $format
105 * @param array $context
109 protected function getAttributes($object, $format = null, array $context)
111 $class = get_class($object);
112 $key = $class.'-'.$context['cache_key'];
114 if (isset($this->attributesCache[$key])) {
115 return $this->attributesCache[$key];
118 $allowedAttributes = $this->getAllowedAttributes($object, $context, true);
120 if (false !== $allowedAttributes) {
121 if ($context['cache_key']) {
122 $this->attributesCache[$key] = $allowedAttributes;
125 return $allowedAttributes;
128 if (isset($this->attributesCache[$class])) {
129 return $this->attributesCache[$class];
132 return $this->attributesCache[$class] = $this->extractAttributes($object, $format, $context);
136 * Extracts attributes to normalize from the class of the given object, format and context.
138 * @param object $object
139 * @param string|null $format
140 * @param array $context
144 abstract protected function extractAttributes($object, $format = null, array $context = array());
147 * Gets the attribute value.
149 * @param object $object
150 * @param string $attribute
151 * @param string|null $format
152 * @param array $context
156 abstract protected function getAttributeValue($object, $attribute, $format = null, array $context = array());
161 public function supportsDenormalization($data, $type, $format = null)
163 return class_exists($type);
169 public function denormalize($data, $class, $format = null, array $context = array())
171 if (!isset($context['cache_key'])) {
172 $context['cache_key'] = $this->getCacheKey($format, $context);
174 $allowedAttributes = $this->getAllowedAttributes($class, $context, true);
175 $normalizedData = $this->prepareForDenormalization($data);
177 $reflectionClass = new \ReflectionClass($class);
178 $object = $this->instantiateObject($normalizedData, $class, $context, $reflectionClass, $allowedAttributes, $format);
180 foreach ($normalizedData as $attribute => $value) {
181 if ($this->nameConverter) {
182 $attribute = $this->nameConverter->denormalize($attribute);
185 if (($allowedAttributes !== false && !in_array($attribute, $allowedAttributes)) || !$this->isAllowedAttribute($class, $attribute, $format, $context)) {
189 $value = $this->validateAndDenormalize($class, $attribute, $value, $format, $context);
191 $this->setAttributeValue($object, $attribute, $value, $format, $context);
192 } catch (InvalidArgumentException $e) {
193 throw new UnexpectedValueException($e->getMessage(), $e->getCode(), $e);
201 * Sets attribute value.
203 * @param object $object
204 * @param string $attribute
205 * @param mixed $value
206 * @param string|null $format
207 * @param array $context
209 abstract protected function setAttributeValue($object, $attribute, $value, $format = null, array $context = array());
212 * Validates the submitted data and denormalizes it.
214 * @param string $currentClass
215 * @param string $attribute
217 * @param string|null $format
218 * @param array $context
222 * @throws UnexpectedValueException
223 * @throws LogicException
225 private function validateAndDenormalize($currentClass, $attribute, $data, $format, array $context)
227 if (null === $this->propertyTypeExtractor || null === $types = $this->propertyTypeExtractor->getTypes($currentClass, $attribute)) {
231 $expectedTypes = array();
232 foreach ($types as $type) {
233 if (null === $data && $type->isNullable()) {
237 if ($type->isCollection() && null !== ($collectionValueType = $type->getCollectionValueType()) && Type::BUILTIN_TYPE_OBJECT === $collectionValueType->getBuiltinType()) {
238 $builtinType = Type::BUILTIN_TYPE_OBJECT;
239 $class = $collectionValueType->getClassName().'[]';
241 if (null !== $collectionKeyType = $type->getCollectionKeyType()) {
242 $context['key_type'] = $collectionKeyType;
245 $builtinType = $type->getBuiltinType();
246 $class = $type->getClassName();
249 $expectedTypes[Type::BUILTIN_TYPE_OBJECT === $builtinType && $class ? $class : $builtinType] = true;
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));
256 if ($this->serializer->supportsDenormalization($data, $class, $format)) {
257 return $this->serializer->denormalize($data, $class, $format, $context);
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;
271 if (call_user_func('is_'.$builtinType, $data)) {
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)));
280 * Sets an attribute and apply the name converter if necessary.
283 * @param string $attribute
284 * @param mixed $attributeValue
288 private function updateData(array $data, $attribute, $attributeValue)
290 if ($this->nameConverter) {
291 $attribute = $this->nameConverter->normalize($attribute);
294 $data[$attribute] = $attributeValue;
300 * Is the max depth reached for the given attribute?
302 * @param AttributeMetadataInterface[] $attributesMetadata
303 * @param string $class
304 * @param string $attribute
305 * @param array $context
309 private function isMaxDepthReached(array $attributesMetadata, $class, $attribute, array &$context)
312 !isset($context[static::ENABLE_MAX_DEPTH]) ||
313 !isset($attributesMetadata[$attribute]) ||
314 null === $maxDepth = $attributesMetadata[$attribute]->getMaxDepth()
319 $key = sprintf(static::DEPTH_KEY_PATTERN, $class, $attribute);
320 if (!isset($context[$key])) {
326 if ($context[$key] === $maxDepth) {
336 * Gets the cache key to use.
338 * @param string|null $format
339 * @param array $context
341 * @return bool|string
343 private function getCacheKey($format, array $context)
346 return md5($format.serialize($context));
347 } catch (\Exception $exception) {
348 // The context cannot be serialized, skip the cache