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\Mapping\Factory\ClassMetadataFactoryInterface;
19 use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
22 * Converts between objects and arrays using the PropertyAccess component.
24 * @author Kévin Dunglas <dunglas@gmail.com>
26 class ObjectNormalizer extends AbstractObjectNormalizer
29 * @var PropertyAccessorInterface
31 protected $propertyAccessor;
33 public function __construct(ClassMetadataFactoryInterface $classMetadataFactory = null, NameConverterInterface $nameConverter = null, PropertyAccessorInterface $propertyAccessor = null, PropertyTypeExtractorInterface $propertyTypeExtractor = null)
35 parent::__construct($classMetadataFactory, $nameConverter, $propertyTypeExtractor);
37 $this->propertyAccessor = $propertyAccessor ?: PropertyAccess::createPropertyAccessor();
43 protected function extractAttributes($object, $format = null, array $context = array())
45 // If not using groups, detect manually
46 $attributes = array();
49 $reflClass = new \ReflectionClass($object);
50 foreach ($reflClass->getMethods(\ReflectionMethod::IS_PUBLIC) as $reflMethod) {
52 $reflMethod->getNumberOfRequiredParameters() !== 0 ||
53 $reflMethod->isStatic() ||
54 $reflMethod->isConstructor() ||
55 $reflMethod->isDestructor()
60 $name = $reflMethod->name;
61 $attributeName = null;
63 if (0 === strpos($name, 'get') || 0 === strpos($name, 'has')) {
64 // getters and hassers
65 $attributeName = substr($name, 3);
67 if (!$reflClass->hasProperty($attributeName)) {
68 $attributeName = lcfirst($attributeName);
70 } elseif (strpos($name, 'is') === 0) {
72 $attributeName = substr($name, 2);
74 if (!$reflClass->hasProperty($attributeName)) {
75 $attributeName = lcfirst($attributeName);
79 if (null !== $attributeName && $this->isAllowedAttribute($object, $attributeName, $format, $context)) {
80 $attributes[$attributeName] = true;
85 foreach ($reflClass->getProperties(\ReflectionProperty::IS_PUBLIC) as $reflProperty) {
86 if ($reflProperty->isStatic() || !$this->isAllowedAttribute($object, $reflProperty->name, $format, $context)) {
90 $attributes[$reflProperty->name] = true;
93 return array_keys($attributes);
99 protected function getAttributeValue($object, $attribute, $format = null, array $context = array())
101 return $this->propertyAccessor->getValue($object, $attribute);
107 protected function setAttributeValue($object, $attribute, $value, $format = null, array $context = array())
110 $this->propertyAccessor->setValue($object, $attribute, $value);
111 } catch (NoSuchPropertyException $exception) {
112 // Properties not found are ignored