Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / vendor / symfony / serializer / Normalizer / ObjectNormalizer.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
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\PropertyAccess\Exception\NoSuchPropertyException;
15 use Symfony\Component\PropertyAccess\PropertyAccess;
16 use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
17 use Symfony\Component\PropertyInfo\PropertyTypeExtractorInterface;
18 use Symfony\Component\Serializer\Exception\RuntimeException;
19 use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface;
20 use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
21
22 /**
23  * Converts between objects and arrays using the PropertyAccess component.
24  *
25  * @author Kévin Dunglas <dunglas@gmail.com>
26  */
27 class ObjectNormalizer extends AbstractObjectNormalizer
28 {
29     protected $propertyAccessor;
30
31     public function __construct(ClassMetadataFactoryInterface $classMetadataFactory = null, NameConverterInterface $nameConverter = null, PropertyAccessorInterface $propertyAccessor = null, PropertyTypeExtractorInterface $propertyTypeExtractor = null)
32     {
33         if (!\class_exists(PropertyAccess::class)) {
34             throw new RuntimeException('The ObjectNormalizer class requires the "PropertyAccess" component. Install "symfony/property-access" to use it.');
35         }
36
37         parent::__construct($classMetadataFactory, $nameConverter, $propertyTypeExtractor);
38
39         $this->propertyAccessor = $propertyAccessor ?: PropertyAccess::createPropertyAccessor();
40     }
41
42     /**
43      * {@inheritdoc}
44      */
45     protected function extractAttributes($object, $format = null, array $context = array())
46     {
47         // If not using groups, detect manually
48         $attributes = array();
49
50         // methods
51         $reflClass = new \ReflectionClass($object);
52         foreach ($reflClass->getMethods(\ReflectionMethod::IS_PUBLIC) as $reflMethod) {
53             if (
54                 0 !== $reflMethod->getNumberOfRequiredParameters() ||
55                 $reflMethod->isStatic() ||
56                 $reflMethod->isConstructor() ||
57                 $reflMethod->isDestructor()
58             ) {
59                 continue;
60             }
61
62             $name = $reflMethod->name;
63             $attributeName = null;
64
65             if (0 === strpos($name, 'get') || 0 === strpos($name, 'has')) {
66                 // getters and hassers
67                 $attributeName = substr($name, 3);
68
69                 if (!$reflClass->hasProperty($attributeName)) {
70                     $attributeName = lcfirst($attributeName);
71                 }
72             } elseif (0 === strpos($name, 'is')) {
73                 // issers
74                 $attributeName = substr($name, 2);
75
76                 if (!$reflClass->hasProperty($attributeName)) {
77                     $attributeName = lcfirst($attributeName);
78                 }
79             }
80
81             if (null !== $attributeName && $this->isAllowedAttribute($object, $attributeName, $format, $context)) {
82                 $attributes[$attributeName] = true;
83             }
84         }
85
86         // properties
87         foreach ($reflClass->getProperties(\ReflectionProperty::IS_PUBLIC) as $reflProperty) {
88             if ($reflProperty->isStatic() || !$this->isAllowedAttribute($object, $reflProperty->name, $format, $context)) {
89                 continue;
90             }
91
92             $attributes[$reflProperty->name] = true;
93         }
94
95         return array_keys($attributes);
96     }
97
98     /**
99      * {@inheritdoc}
100      */
101     protected function getAttributeValue($object, $attribute, $format = null, array $context = array())
102     {
103         return $this->propertyAccessor->getValue($object, $attribute);
104     }
105
106     /**
107      * {@inheritdoc}
108      */
109     protected function setAttributeValue($object, $attribute, $value, $format = null, array $context = array())
110     {
111         try {
112             $this->propertyAccessor->setValue($object, $attribute, $value);
113         } catch (NoSuchPropertyException $exception) {
114             // Properties not found are ignored
115         }
116     }
117 }