|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <[email protected]> |
| 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\LogicException; |
| 16 | + |
| 17 | +/** |
| 18 | + * Base class for a normalizer dealing with objects. |
| 19 | + * |
| 20 | + * @author Kévin Dunglas <[email protected]> |
| 21 | + */ |
| 22 | +abstract class AbstractObjectNormalizer extends AbstractNormalizer |
| 23 | +{ |
| 24 | + private $attributesCache = array(); |
| 25 | + |
| 26 | + /** |
| 27 | + * {@inheritdoc} |
| 28 | + */ |
| 29 | + public function supportsNormalization($data, $format = null) |
| 30 | + { |
| 31 | + return is_object($data) && !$data instanceof \Traversable; |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + * {@inheritdoc} |
| 36 | + * |
| 37 | + * @throws CircularReferenceException |
| 38 | + */ |
| 39 | + public function normalize($object, $format = null, array $context = array()) |
| 40 | + { |
| 41 | + if ($this->isCircularReference($object, $context)) { |
| 42 | + return $this->handleCircularReference($object); |
| 43 | + } |
| 44 | + |
| 45 | + $data = array(); |
| 46 | + $attributes = $this->getAttributes($object, $format, $context); |
| 47 | + |
| 48 | + foreach ($attributes as $attribute) { |
| 49 | + $attributeValue = $this->getAttributeValue($object, $attribute, $format, $context); |
| 50 | + |
| 51 | + if (isset($this->callbacks[$attribute])) { |
| 52 | + $attributeValue = call_user_func($this->callbacks[$attribute], $attributeValue); |
| 53 | + } |
| 54 | + |
| 55 | + if (null !== $attributeValue && !is_scalar($attributeValue)) { |
| 56 | + if (!$this->serializer instanceof NormalizerInterface) { |
| 57 | + throw new LogicException(sprintf('Cannot normalize attribute "%s" because injected serializer is not a normalizer', $attribute)); |
| 58 | + } |
| 59 | + |
| 60 | + $attributeValue = $this->serializer->normalize($attributeValue, $format, $context); |
| 61 | + } |
| 62 | + |
| 63 | + if ($this->nameConverter) { |
| 64 | + $attribute = $this->nameConverter->normalize($attribute); |
| 65 | + } |
| 66 | + |
| 67 | + $data[$attribute] = $attributeValue; |
| 68 | + } |
| 69 | + |
| 70 | + return $data; |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Gets and caches attributes for the given object, format and context. |
| 75 | + * |
| 76 | + * @param object $object |
| 77 | + * @param string|null $format |
| 78 | + * @param array $context |
| 79 | + * |
| 80 | + * @return string[] |
| 81 | + */ |
| 82 | + protected function getAttributes($object, $format = null, array $context) |
| 83 | + { |
| 84 | + $key = sprintf('%s-%s', get_class($object), serialize($context)); |
| 85 | + |
| 86 | + if (isset($this->attributesCache[$key])) { |
| 87 | + return $this->attributesCache[$key]; |
| 88 | + } |
| 89 | + |
| 90 | + $allowedAttributes = $this->getAllowedAttributes($object, $context, true); |
| 91 | + |
| 92 | + if (false !== $allowedAttributes) { |
| 93 | + return $this->attributesCache[$key] = $allowedAttributes; |
| 94 | + } |
| 95 | + |
| 96 | + return $this->attributesCache[$key] = $this->extractAttributes($object, $format, $context); |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * Extracts attributes to normalize from the class of the given object, format and context. |
| 101 | + * |
| 102 | + * @param object $object |
| 103 | + * @param string|null $format |
| 104 | + * @param array $context |
| 105 | + * |
| 106 | + * @return string[] |
| 107 | + */ |
| 108 | + abstract protected function extractAttributes($object, $format = null, array $context = array()); |
| 109 | + |
| 110 | + /** |
| 111 | + * Gets the attribute value. |
| 112 | + * |
| 113 | + * @param object $object |
| 114 | + * @param string $attribute |
| 115 | + * @param string|null $format |
| 116 | + * @param array $context |
| 117 | + * |
| 118 | + * @return mixed |
| 119 | + */ |
| 120 | + abstract protected function getAttributeValue($object, $attribute, $format = null, array $context = array()); |
| 121 | + |
| 122 | + /** |
| 123 | + * {@inheritdoc} |
| 124 | + */ |
| 125 | + public function supportsDenormalization($data, $type, $format = null) |
| 126 | + { |
| 127 | + return class_exists($type); |
| 128 | + } |
| 129 | + |
| 130 | + /** |
| 131 | + * {@inheritdoc} |
| 132 | + */ |
| 133 | + public function denormalize($data, $class, $format = null, array $context = array()) |
| 134 | + { |
| 135 | + $allowedAttributes = $this->getAllowedAttributes($class, $context, true); |
| 136 | + $normalizedData = $this->prepareForDenormalization($data); |
| 137 | + |
| 138 | + $reflectionClass = new \ReflectionClass($class); |
| 139 | + $object = $this->instantiateObject($normalizedData, $class, $context, $reflectionClass, $allowedAttributes); |
| 140 | + |
| 141 | + foreach ($normalizedData as $attribute => $value) { |
| 142 | + if ($this->nameConverter) { |
| 143 | + $attribute = $this->nameConverter->denormalize($attribute); |
| 144 | + } |
| 145 | + |
| 146 | + $allowed = $allowedAttributes === false || in_array($attribute, $allowedAttributes); |
| 147 | + $ignored = in_array($attribute, $this->ignoredAttributes); |
| 148 | + |
| 149 | + if ($allowed && !$ignored) { |
| 150 | + $this->setAttributeValue($object, $attribute, $value, $format, $context); |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + return $object; |
| 155 | + } |
| 156 | + |
| 157 | + /** |
| 158 | + * Sets attribute value. |
| 159 | + * |
| 160 | + * @param object $object |
| 161 | + * @param string $attribute |
| 162 | + * @param mixed $value |
| 163 | + * @param string|null $format |
| 164 | + * @param array $context |
| 165 | + */ |
| 166 | + abstract protected function setAttributeValue($object, $attribute, $value, $format = null, array $context = array()); |
| 167 | +} |
0 commit comments