4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Serializer\Normalizer;
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;
23 * Converts between objects and arrays using the PropertyAccess component.
25 * @author Kévin Dunglas <dunglas@gmail.com>
27 class ObjectNormalizer extends AbstractObjectNormalizer
29 protected $propertyAccessor;
31 public function __construct(ClassMetadataFactoryInterface $classMetadataFactory = null, NameConverterInterface $nameConverter = null, PropertyAccessorInterface $propertyAccessor = null, PropertyTypeExtractorInterface $propertyTypeExtractor = null)
33 if (!\class_exists(PropertyAccess::class)) {
34 throw new RuntimeException('The ObjectNormalizer class requires the "PropertyAccess" component. Install "symfony/property-access" to use it.');
37 parent::__construct($classMetadataFactory, $nameConverter, $propertyTypeExtractor);
39 $this->propertyAccessor = $propertyAccessor ?: PropertyAccess::createPropertyAccessor();
45 protected function extractAttributes($object, $format = null, array $context = array())
47 // If not using groups, detect manually
48 $attributes = array();
51 $reflClass = new \ReflectionClass($object);
52 foreach ($reflClass->getMethods(\ReflectionMethod::IS_PUBLIC) as $reflMethod) {
54 0 !== $reflMethod->getNumberOfRequiredParameters() ||
55 $reflMethod->isStatic() ||
56 $reflMethod->isConstructor() ||
57 $reflMethod->isDestructor()
62 $name = $reflMethod->name;
63 $attributeName = null;
65 if (0 === strpos($name, 'get') || 0 === strpos($name, 'has')) {
66 // getters and hassers
67 $attributeName = substr($name, 3);
69 if (!$reflClass->hasProperty($attributeName)) {
70 $attributeName = lcfirst($attributeName);
72 } elseif (0 === strpos($name, 'is')) {
74 $attributeName = substr($name, 2);
76 if (!$reflClass->hasProperty($attributeName)) {
77 $attributeName = lcfirst($attributeName);
81 if (null !== $attributeName && $this->isAllowedAttribute($object, $attributeName, $format, $context)) {
82 $attributes[$attributeName] = true;
87 foreach ($reflClass->getProperties(\ReflectionProperty::IS_PUBLIC) as $reflProperty) {
88 if ($reflProperty->isStatic() || !$this->isAllowedAttribute($object, $reflProperty->name, $format, $context)) {
92 $attributes[$reflProperty->name] = true;
95 return array_keys($attributes);
101 protected function getAttributeValue($object, $attribute, $format = null, array $context = array())
103 return $this->propertyAccessor->getValue($object, $attribute);
109 protected function setAttributeValue($object, $attribute, $value, $format = null, array $context = array())
112 $this->propertyAccessor->setValue($object, $attribute, $value);
113 } catch (NoSuchPropertyException $exception) {
114 // Properties not found are ignored