annotate vendor/symfony/serializer/Normalizer/PropertyNormalizer.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents af1871eacc83
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 /*
Chris@0 4 * This file is part of the Symfony package.
Chris@0 5 *
Chris@0 6 * (c) Fabien Potencier <fabien@symfony.com>
Chris@0 7 *
Chris@0 8 * For the full copyright and license information, please view the LICENSE
Chris@0 9 * file that was distributed with this source code.
Chris@0 10 */
Chris@0 11
Chris@0 12 namespace Symfony\Component\Serializer\Normalizer;
Chris@0 13
Chris@0 14 /**
Chris@0 15 * Converts between objects and arrays by mapping properties.
Chris@0 16 *
Chris@0 17 * The normalization process looks for all the object's properties (public and private).
Chris@0 18 * The result is a map from property names to property values. Property values
Chris@0 19 * are normalized through the serializer.
Chris@0 20 *
Chris@0 21 * The denormalization first looks at the constructor of the given class to see
Chris@0 22 * if any of the parameters have the same name as one of the properties. The
Chris@0 23 * constructor is then called with all parameters or an exception is thrown if
Chris@0 24 * any required parameters were not present as properties. Then the denormalizer
Chris@0 25 * walks through the given map of property names to property values to see if a
Chris@0 26 * property with the corresponding name exists. If found, the property gets the value.
Chris@0 27 *
Chris@0 28 * @author Matthieu Napoli <matthieu@mnapoli.fr>
Chris@0 29 * @author Kévin Dunglas <dunglas@gmail.com>
Chris@0 30 */
Chris@0 31 class PropertyNormalizer extends AbstractObjectNormalizer
Chris@0 32 {
Chris@17 33 private $cache = [];
Chris@14 34
Chris@0 35 /**
Chris@0 36 * {@inheritdoc}
Chris@0 37 */
Chris@0 38 public function supportsNormalization($data, $format = null)
Chris@0 39 {
Chris@14 40 return parent::supportsNormalization($data, $format) && (isset($this->cache[$type = \get_class($data)]) ? $this->cache[$type] : $this->cache[$type] = $this->supports($type));
Chris@0 41 }
Chris@0 42
Chris@0 43 /**
Chris@0 44 * {@inheritdoc}
Chris@0 45 */
Chris@0 46 public function supportsDenormalization($data, $type, $format = null)
Chris@0 47 {
Chris@14 48 return parent::supportsDenormalization($data, $type, $format) && (isset($this->cache[$type]) ? $this->cache[$type] : $this->cache[$type] = $this->supports($type));
Chris@0 49 }
Chris@0 50
Chris@0 51 /**
Chris@0 52 * Checks if the given class has any non-static property.
Chris@0 53 *
Chris@0 54 * @param string $class
Chris@0 55 *
Chris@0 56 * @return bool
Chris@0 57 */
Chris@0 58 private function supports($class)
Chris@0 59 {
Chris@0 60 $class = new \ReflectionClass($class);
Chris@0 61
Chris@0 62 // We look for at least one non-static property
Chris@14 63 do {
Chris@14 64 foreach ($class->getProperties() as $property) {
Chris@14 65 if (!$property->isStatic()) {
Chris@14 66 return true;
Chris@14 67 }
Chris@0 68 }
Chris@14 69 } while ($class = $class->getParentClass());
Chris@0 70
Chris@0 71 return false;
Chris@0 72 }
Chris@0 73
Chris@0 74 /**
Chris@0 75 * {@inheritdoc}
Chris@0 76 */
Chris@17 77 protected function isAllowedAttribute($classOrObject, $attribute, $format = null, array $context = [])
Chris@0 78 {
Chris@0 79 if (!parent::isAllowedAttribute($classOrObject, $attribute, $format, $context)) {
Chris@0 80 return false;
Chris@0 81 }
Chris@0 82
Chris@0 83 try {
Chris@14 84 $reflectionProperty = $this->getReflectionProperty($classOrObject, $attribute);
Chris@0 85 if ($reflectionProperty->isStatic()) {
Chris@0 86 return false;
Chris@0 87 }
Chris@0 88 } catch (\ReflectionException $reflectionException) {
Chris@0 89 return false;
Chris@0 90 }
Chris@0 91
Chris@0 92 return true;
Chris@0 93 }
Chris@0 94
Chris@0 95 /**
Chris@0 96 * {@inheritdoc}
Chris@0 97 */
Chris@17 98 protected function extractAttributes($object, $format = null, array $context = [])
Chris@0 99 {
Chris@0 100 $reflectionObject = new \ReflectionObject($object);
Chris@17 101 $attributes = [];
Chris@0 102
Chris@14 103 do {
Chris@14 104 foreach ($reflectionObject->getProperties() as $property) {
Chris@18 105 if (!$this->isAllowedAttribute($reflectionObject->getName(), $property->name, $format, $context)) {
Chris@14 106 continue;
Chris@14 107 }
Chris@14 108
Chris@14 109 $attributes[] = $property->name;
Chris@0 110 }
Chris@14 111 } while ($reflectionObject = $reflectionObject->getParentClass());
Chris@0 112
Chris@0 113 return $attributes;
Chris@0 114 }
Chris@0 115
Chris@0 116 /**
Chris@0 117 * {@inheritdoc}
Chris@0 118 */
Chris@17 119 protected function getAttributeValue($object, $attribute, $format = null, array $context = [])
Chris@0 120 {
Chris@0 121 try {
Chris@14 122 $reflectionProperty = $this->getReflectionProperty($object, $attribute);
Chris@0 123 } catch (\ReflectionException $reflectionException) {
Chris@0 124 return;
Chris@0 125 }
Chris@0 126
Chris@0 127 // Override visibility
Chris@0 128 if (!$reflectionProperty->isPublic()) {
Chris@0 129 $reflectionProperty->setAccessible(true);
Chris@0 130 }
Chris@0 131
Chris@0 132 return $reflectionProperty->getValue($object);
Chris@0 133 }
Chris@0 134
Chris@0 135 /**
Chris@0 136 * {@inheritdoc}
Chris@0 137 */
Chris@17 138 protected function setAttributeValue($object, $attribute, $value, $format = null, array $context = [])
Chris@0 139 {
Chris@0 140 try {
Chris@14 141 $reflectionProperty = $this->getReflectionProperty($object, $attribute);
Chris@0 142 } catch (\ReflectionException $reflectionException) {
Chris@0 143 return;
Chris@0 144 }
Chris@0 145
Chris@0 146 if ($reflectionProperty->isStatic()) {
Chris@0 147 return;
Chris@0 148 }
Chris@0 149
Chris@0 150 // Override visibility
Chris@0 151 if (!$reflectionProperty->isPublic()) {
Chris@0 152 $reflectionProperty->setAccessible(true);
Chris@0 153 }
Chris@0 154
Chris@0 155 $reflectionProperty->setValue($object, $value);
Chris@0 156 }
Chris@14 157
Chris@14 158 /**
Chris@14 159 * @param string|object $classOrObject
Chris@14 160 * @param string $attribute
Chris@14 161 *
Chris@14 162 * @return \ReflectionProperty
Chris@14 163 *
Chris@14 164 * @throws \ReflectionException
Chris@14 165 */
Chris@14 166 private function getReflectionProperty($classOrObject, $attribute)
Chris@14 167 {
Chris@14 168 $reflectionClass = new \ReflectionClass($classOrObject);
Chris@14 169 while (true) {
Chris@14 170 try {
Chris@14 171 return $reflectionClass->getProperty($attribute);
Chris@14 172 } catch (\ReflectionException $e) {
Chris@14 173 if (!$reflectionClass = $reflectionClass->getParentClass()) {
Chris@14 174 throw $e;
Chris@14 175 }
Chris@14 176 }
Chris@14 177 }
Chris@14 178 }
Chris@0 179 }