annotate vendor/symfony/serializer/Normalizer/PropertyNormalizer.php @ 8:50b0d041100e

Further files for download
author Chris Cannam
date Mon, 05 Feb 2018 10:56:40 +0000
parents 4c8ae668cc8c
children 1fec387a4317
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@0 33 /**
Chris@0 34 * {@inheritdoc}
Chris@0 35 */
Chris@0 36 public function supportsNormalization($data, $format = null)
Chris@0 37 {
Chris@0 38 return parent::supportsNormalization($data, $format) && $this->supports(get_class($data));
Chris@0 39 }
Chris@0 40
Chris@0 41 /**
Chris@0 42 * {@inheritdoc}
Chris@0 43 */
Chris@0 44 public function supportsDenormalization($data, $type, $format = null)
Chris@0 45 {
Chris@0 46 return parent::supportsDenormalization($data, $type, $format) && $this->supports($type);
Chris@0 47 }
Chris@0 48
Chris@0 49 /**
Chris@0 50 * Checks if the given class has any non-static property.
Chris@0 51 *
Chris@0 52 * @param string $class
Chris@0 53 *
Chris@0 54 * @return bool
Chris@0 55 */
Chris@0 56 private function supports($class)
Chris@0 57 {
Chris@0 58 $class = new \ReflectionClass($class);
Chris@0 59
Chris@0 60 // We look for at least one non-static property
Chris@0 61 foreach ($class->getProperties() as $property) {
Chris@0 62 if (!$property->isStatic()) {
Chris@0 63 return true;
Chris@0 64 }
Chris@0 65 }
Chris@0 66
Chris@0 67 return false;
Chris@0 68 }
Chris@0 69
Chris@0 70 /**
Chris@0 71 * {@inheritdoc}
Chris@0 72 */
Chris@0 73 protected function isAllowedAttribute($classOrObject, $attribute, $format = null, array $context = array())
Chris@0 74 {
Chris@0 75 if (!parent::isAllowedAttribute($classOrObject, $attribute, $format, $context)) {
Chris@0 76 return false;
Chris@0 77 }
Chris@0 78
Chris@0 79 try {
Chris@0 80 $reflectionProperty = new \ReflectionProperty(is_string($classOrObject) ? $classOrObject : get_class($classOrObject), $attribute);
Chris@0 81 if ($reflectionProperty->isStatic()) {
Chris@0 82 return false;
Chris@0 83 }
Chris@0 84 } catch (\ReflectionException $reflectionException) {
Chris@0 85 return false;
Chris@0 86 }
Chris@0 87
Chris@0 88 return true;
Chris@0 89 }
Chris@0 90
Chris@0 91 /**
Chris@0 92 * {@inheritdoc}
Chris@0 93 */
Chris@0 94 protected function extractAttributes($object, $format = null, array $context = array())
Chris@0 95 {
Chris@0 96 $reflectionObject = new \ReflectionObject($object);
Chris@0 97 $attributes = array();
Chris@0 98
Chris@0 99 foreach ($reflectionObject->getProperties() as $property) {
Chris@0 100 if (!$this->isAllowedAttribute($object, $property->name)) {
Chris@0 101 continue;
Chris@0 102 }
Chris@0 103
Chris@0 104 $attributes[] = $property->name;
Chris@0 105 }
Chris@0 106
Chris@0 107 return $attributes;
Chris@0 108 }
Chris@0 109
Chris@0 110 /**
Chris@0 111 * {@inheritdoc}
Chris@0 112 */
Chris@0 113 protected function getAttributeValue($object, $attribute, $format = null, array $context = array())
Chris@0 114 {
Chris@0 115 try {
Chris@0 116 $reflectionProperty = new \ReflectionProperty(get_class($object), $attribute);
Chris@0 117 } catch (\ReflectionException $reflectionException) {
Chris@0 118 return;
Chris@0 119 }
Chris@0 120
Chris@0 121 // Override visibility
Chris@0 122 if (!$reflectionProperty->isPublic()) {
Chris@0 123 $reflectionProperty->setAccessible(true);
Chris@0 124 }
Chris@0 125
Chris@0 126 return $reflectionProperty->getValue($object);
Chris@0 127 }
Chris@0 128
Chris@0 129 /**
Chris@0 130 * {@inheritdoc}
Chris@0 131 */
Chris@0 132 protected function setAttributeValue($object, $attribute, $value, $format = null, array $context = array())
Chris@0 133 {
Chris@0 134 try {
Chris@0 135 $reflectionProperty = new \ReflectionProperty(get_class($object), $attribute);
Chris@0 136 } catch (\ReflectionException $reflectionException) {
Chris@0 137 return;
Chris@0 138 }
Chris@0 139
Chris@0 140 if ($reflectionProperty->isStatic()) {
Chris@0 141 return;
Chris@0 142 }
Chris@0 143
Chris@0 144 // Override visibility
Chris@0 145 if (!$reflectionProperty->isPublic()) {
Chris@0 146 $reflectionProperty->setAccessible(true);
Chris@0 147 }
Chris@0 148
Chris@0 149 $reflectionProperty->setValue($object, $value);
Chris@0 150 }
Chris@0 151 }