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