Deprecated: Constant E_STRICT is deprecated in /home/normanv/www/annuairepro/vendor/symfony/error-handler/ErrorHandler.php on line 58

Deprecated: Constant E_STRICT is deprecated in /home/normanv/www/annuairepro/vendor/symfony/error-handler/ErrorHandler.php on line 76
Symfony Profiler

vendor/symfony/serializer/Normalizer/PropertyNormalizer.php line 157

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\UninitializedPropertyException;
  12. /**
  13.  * Converts between objects and arrays by mapping properties.
  14.  *
  15.  * The normalization process looks for all the object's properties (public and private).
  16.  * The result is a map from property names to property values. Property values
  17.  * are normalized through the serializer.
  18.  *
  19.  * The denormalization first looks at the constructor of the given class to see
  20.  * if any of the parameters have the same name as one of the properties. The
  21.  * constructor is then called with all parameters or an exception is thrown if
  22.  * any required parameters were not present as properties. Then the denormalizer
  23.  * walks through the given map of property names to property values to see if a
  24.  * property with the corresponding name exists. If found, the property gets the value.
  25.  *
  26.  * @author Matthieu Napoli <matthieu@mnapoli.fr>
  27.  * @author Kévin Dunglas <dunglas@gmail.com>
  28.  */
  29. class PropertyNormalizer extends AbstractObjectNormalizer
  30. {
  31.     /**
  32.      * {@inheritdoc}
  33.      */
  34.     public function supportsNormalization($datastring $format null)
  35.     {
  36.         return parent::supportsNormalization($data$format) && $this->supports(\get_class($data));
  37.     }
  38.     /**
  39.      * {@inheritdoc}
  40.      */
  41.     public function supportsDenormalization($datastring $typestring $format null)
  42.     {
  43.         return parent::supportsDenormalization($data$type$format) && $this->supports($type);
  44.     }
  45.     /**
  46.      * {@inheritdoc}
  47.      */
  48.     public function hasCacheableSupportsMethod(): bool
  49.     {
  50.         return __CLASS__ === static::class;
  51.     }
  52.     /**
  53.      * Checks if the given class has any non-static property.
  54.      */
  55.     private function supports(string $class): bool
  56.     {
  57.         $class = new \ReflectionClass($class);
  58.         // We look for at least one non-static property
  59.         do {
  60.             foreach ($class->getProperties() as $property) {
  61.                 if (!$property->isStatic()) {
  62.                     return true;
  63.                 }
  64.             }
  65.         } while ($class $class->getParentClass());
  66.         return false;
  67.     }
  68.     /**
  69.      * {@inheritdoc}
  70.      */
  71.     protected function isAllowedAttribute($classOrObjectstring $attributestring $format null, array $context = [])
  72.     {
  73.         if (!parent::isAllowedAttribute($classOrObject$attribute$format$context)) {
  74.             return false;
  75.         }
  76.         try {
  77.             $reflectionProperty $this->getReflectionProperty($classOrObject$attribute);
  78.             if ($reflectionProperty->isStatic()) {
  79.                 return false;
  80.             }
  81.         } catch (\ReflectionException $reflectionException) {
  82.             return false;
  83.         }
  84.         return true;
  85.     }
  86.     /**
  87.      * {@inheritdoc}
  88.      */
  89.     protected function extractAttributes(object $objectstring $format null, array $context = [])
  90.     {
  91.         $reflectionObject = new \ReflectionObject($object);
  92.         $attributes = [];
  93.         do {
  94.             foreach ($reflectionObject->getProperties() as $property) {
  95.                 if (!$this->isAllowedAttribute($reflectionObject->getName(), $property->name$format$context)) {
  96.                     continue;
  97.                 }
  98.                 $attributes[] = $property->name;
  99.             }
  100.         } while ($reflectionObject $reflectionObject->getParentClass());
  101.         return array_unique($attributes);
  102.     }
  103.     /**
  104.      * {@inheritdoc}
  105.      */
  106.     protected function getAttributeValue(object $objectstring $attributestring $format null, array $context = [])
  107.     {
  108.         try {
  109.             $reflectionProperty $this->getReflectionProperty($object$attribute);
  110.         } catch (\ReflectionException $reflectionException) {
  111.             return null;
  112.         }
  113.         // Override visibility
  114.         if (!$reflectionProperty->isPublic()) {
  115.             $reflectionProperty->setAccessible(true);
  116.         }
  117.         if (\PHP_VERSION_ID >= 70400 && $reflectionProperty->hasType()) {
  118.             return $reflectionProperty->getValue($object);
  119.         }
  120.         if (!method_exists($object'__get') && !isset($object->$attribute)) {
  121.             $propertyValues = (array) $object;
  122.             if (($reflectionProperty->isPublic() && !\array_key_exists($reflectionProperty->name$propertyValues))
  123.                 || ($reflectionProperty->isProtected() && !\array_key_exists("\0*\0{$reflectionProperty->name}"$propertyValues))
  124.                 || ($reflectionProperty->isPrivate() && !\array_key_exists("\0{$reflectionProperty->class}\0{$reflectionProperty->name}"$propertyValues))
  125.             ) {
  126.                 throw new UninitializedPropertyException(sprintf('The property "%s::$%s" is not initialized.'\get_class($object), $reflectionProperty->name));
  127.             }
  128.         }
  129.         return $reflectionProperty->getValue($object);
  130.     }
  131.     /**
  132.      * {@inheritdoc}
  133.      */
  134.     protected function setAttributeValue(object $objectstring $attribute$valuestring $format null, array $context = [])
  135.     {
  136.         try {
  137.             $reflectionProperty $this->getReflectionProperty($object$attribute);
  138.         } catch (\ReflectionException $reflectionException) {
  139.             return;
  140.         }
  141.         if ($reflectionProperty->isStatic()) {
  142.             return;
  143.         }
  144.         // Override visibility
  145.         if (!$reflectionProperty->isPublic()) {
  146.             $reflectionProperty->setAccessible(true);
  147.         }
  148.         $reflectionProperty->setValue($object$value);
  149.     }
  150.     /**
  151.      * @param string|object $classOrObject
  152.      *
  153.      * @throws \ReflectionException
  154.      */
  155.     private function getReflectionProperty($classOrObjectstring $attribute): \ReflectionProperty
  156.     {
  157.         $reflectionClass = new \ReflectionClass($classOrObject);
  158.         while (true) {
  159.             try {
  160.                 return $reflectionClass->getProperty($attribute);
  161.             } catch (\ReflectionException $e) {
  162.                 if (!$reflectionClass $reflectionClass->getParentClass()) {
  163.                     throw $e;
  164.                 }
  165.             }
  166.         }
  167.     }
  168. }