vendor/symfony/serializer/Normalizer/ObjectNormalizer.php line 27

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <fabien@symfony.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Symfony\Component\Serializer\Normalizer;
  11. use Symfony\Component\PropertyAccess\Exception\NoSuchPropertyException;
  12. use Symfony\Component\PropertyAccess\PropertyAccess;
  13. use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
  14. use Symfony\Component\PropertyInfo\PropertyTypeExtractorInterface;
  15. use Symfony\Component\Serializer\Exception\RuntimeException;
  16. use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface;
  17. use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
  18. /**
  19.  * Converts between objects and arrays using the PropertyAccess component.
  20.  *
  21.  * @author Kévin Dunglas <dunglas@gmail.com>
  22.  */
  23. class ObjectNormalizer extends AbstractObjectNormalizer
  24. {
  25.     protected $propertyAccessor;
  26.     public function __construct(ClassMetadataFactoryInterface $classMetadataFactory nullNameConverterInterface $nameConverter nullPropertyAccessorInterface $propertyAccessor nullPropertyTypeExtractorInterface $propertyTypeExtractor null)
  27.     {
  28.         if (!class_exists(PropertyAccess::class)) {
  29.             throw new RuntimeException('The ObjectNormalizer class requires the "PropertyAccess" component. Install "symfony/property-access" to use it.');
  30.         }
  31.         parent::__construct($classMetadataFactory$nameConverter$propertyTypeExtractor);
  32.         $this->propertyAccessor $propertyAccessor ?: PropertyAccess::createPropertyAccessor();
  33.     }
  34.     /**
  35.      * {@inheritdoc}
  36.      */
  37.     protected function extractAttributes($object$format null, array $context = [])
  38.     {
  39.         // If not using groups, detect manually
  40.         $attributes = [];
  41.         // methods
  42.         $reflClass = new \ReflectionClass($object);
  43.         foreach ($reflClass->getMethods(\ReflectionMethod::IS_PUBLIC) as $reflMethod) {
  44.             if (
  45.                 !== $reflMethod->getNumberOfRequiredParameters() ||
  46.                 $reflMethod->isStatic() ||
  47.                 $reflMethod->isConstructor() ||
  48.                 $reflMethod->isDestructor()
  49.             ) {
  50.                 continue;
  51.             }
  52.             $name $reflMethod->name;
  53.             $attributeName null;
  54.             if (=== strpos($name'get') || === strpos($name'has')) {
  55.                 // getters and hassers
  56.                 $attributeName substr($name3);
  57.                 if (!$reflClass->hasProperty($attributeName)) {
  58.                     $attributeName lcfirst($attributeName);
  59.                 }
  60.             } elseif (=== strpos($name'is')) {
  61.                 // issers
  62.                 $attributeName substr($name2);
  63.                 if (!$reflClass->hasProperty($attributeName)) {
  64.                     $attributeName lcfirst($attributeName);
  65.                 }
  66.             }
  67.             if (null !== $attributeName && $this->isAllowedAttribute($object$attributeName$format$context)) {
  68.                 $attributes[$attributeName] = true;
  69.             }
  70.         }
  71.         // properties
  72.         foreach ($reflClass->getProperties(\ReflectionProperty::IS_PUBLIC) as $reflProperty) {
  73.             if ($reflProperty->isStatic() || !$this->isAllowedAttribute($object$reflProperty->name$format$context)) {
  74.                 continue;
  75.             }
  76.             $attributes[$reflProperty->name] = true;
  77.         }
  78.         return array_keys($attributes);
  79.     }
  80.     /**
  81.      * {@inheritdoc}
  82.      */
  83.     protected function getAttributeValue($object$attribute$format null, array $context = [])
  84.     {
  85.         return $this->propertyAccessor->getValue($object$attribute);
  86.     }
  87.     /**
  88.      * {@inheritdoc}
  89.      */
  90.     protected function setAttributeValue($object$attribute$value$format null, array $context = [])
  91.     {
  92.         try {
  93.             $this->propertyAccessor->setValue($object$attribute$value);
  94.         } catch (NoSuchPropertyException $exception) {
  95.             // Properties not found are ignored
  96.         }
  97.     }
  98. }