Chris@0: Chris@0: * Chris@0: * For the full copyright and license information, please view the LICENSE Chris@0: * file that was distributed with this source code. Chris@0: */ Chris@0: Chris@0: namespace Symfony\Component\Serializer\Normalizer; Chris@0: Chris@0: use Symfony\Component\PropertyAccess\Exception\NoSuchPropertyException; Chris@0: use Symfony\Component\PropertyAccess\PropertyAccess; Chris@0: use Symfony\Component\PropertyAccess\PropertyAccessorInterface; Chris@0: use Symfony\Component\PropertyInfo\PropertyTypeExtractorInterface; Chris@14: use Symfony\Component\Serializer\Exception\RuntimeException; Chris@0: use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface; Chris@0: use Symfony\Component\Serializer\NameConverter\NameConverterInterface; Chris@0: Chris@0: /** Chris@0: * Converts between objects and arrays using the PropertyAccess component. Chris@0: * Chris@0: * @author Kévin Dunglas Chris@0: */ Chris@0: class ObjectNormalizer extends AbstractObjectNormalizer Chris@0: { Chris@0: protected $propertyAccessor; Chris@0: Chris@0: public function __construct(ClassMetadataFactoryInterface $classMetadataFactory = null, NameConverterInterface $nameConverter = null, PropertyAccessorInterface $propertyAccessor = null, PropertyTypeExtractorInterface $propertyTypeExtractor = null) Chris@0: { Chris@14: if (!\class_exists(PropertyAccess::class)) { Chris@14: throw new RuntimeException('The ObjectNormalizer class requires the "PropertyAccess" component. Install "symfony/property-access" to use it.'); Chris@14: } Chris@14: Chris@0: parent::__construct($classMetadataFactory, $nameConverter, $propertyTypeExtractor); Chris@0: Chris@0: $this->propertyAccessor = $propertyAccessor ?: PropertyAccess::createPropertyAccessor(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@17: protected function extractAttributes($object, $format = null, array $context = []) Chris@0: { Chris@0: // If not using groups, detect manually Chris@17: $attributes = []; Chris@0: Chris@0: // methods Chris@0: $reflClass = new \ReflectionClass($object); Chris@0: foreach ($reflClass->getMethods(\ReflectionMethod::IS_PUBLIC) as $reflMethod) { Chris@0: if ( Chris@14: 0 !== $reflMethod->getNumberOfRequiredParameters() || Chris@0: $reflMethod->isStatic() || Chris@0: $reflMethod->isConstructor() || Chris@0: $reflMethod->isDestructor() Chris@0: ) { Chris@0: continue; Chris@0: } Chris@0: Chris@0: $name = $reflMethod->name; Chris@0: $attributeName = null; Chris@0: Chris@0: if (0 === strpos($name, 'get') || 0 === strpos($name, 'has')) { Chris@0: // getters and hassers Chris@0: $attributeName = substr($name, 3); Chris@0: Chris@0: if (!$reflClass->hasProperty($attributeName)) { Chris@0: $attributeName = lcfirst($attributeName); Chris@0: } Chris@14: } elseif (0 === strpos($name, 'is')) { Chris@0: // issers Chris@0: $attributeName = substr($name, 2); Chris@0: Chris@0: if (!$reflClass->hasProperty($attributeName)) { Chris@0: $attributeName = lcfirst($attributeName); Chris@0: } Chris@0: } Chris@0: Chris@0: if (null !== $attributeName && $this->isAllowedAttribute($object, $attributeName, $format, $context)) { Chris@0: $attributes[$attributeName] = true; Chris@0: } Chris@0: } Chris@0: Chris@0: // properties Chris@0: foreach ($reflClass->getProperties(\ReflectionProperty::IS_PUBLIC) as $reflProperty) { Chris@0: if ($reflProperty->isStatic() || !$this->isAllowedAttribute($object, $reflProperty->name, $format, $context)) { Chris@0: continue; Chris@0: } Chris@0: Chris@0: $attributes[$reflProperty->name] = true; Chris@0: } Chris@0: Chris@0: return array_keys($attributes); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@17: protected function getAttributeValue($object, $attribute, $format = null, array $context = []) Chris@0: { Chris@0: return $this->propertyAccessor->getValue($object, $attribute); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@17: protected function setAttributeValue($object, $attribute, $value, $format = null, array $context = []) Chris@0: { Chris@0: try { Chris@0: $this->propertyAccessor->setValue($object, $attribute, $value); Chris@0: } catch (NoSuchPropertyException $exception) { Chris@0: // Properties not found are ignored Chris@0: } Chris@0: } Chris@0: }