vendor/api-platform/core/src/Bridge/Symfony/PropertyInfo/Metadata/Property/PropertyInfoPropertyMetadataFactory.php line 53

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the API Platform project.
  4.  *
  5.  * (c) Kévin Dunglas <dunglas@gmail.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. declare(strict_types=1);
  11. namespace ApiPlatform\Core\Bridge\Symfony\PropertyInfo\Metadata\Property;
  12. use ApiPlatform\Core\Exception\PropertyNotFoundException;
  13. use ApiPlatform\Core\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
  14. use ApiPlatform\Core\Metadata\Property\PropertyMetadata;
  15. use Symfony\Component\PropertyInfo\PropertyInfoExtractorInterface;
  16. /**
  17.  * PropertyInfo metadata loader decorator.
  18.  *
  19.  * @author Kévin Dunglas <dunglas@gmail.com>
  20.  */
  21. final class PropertyInfoPropertyMetadataFactory implements PropertyMetadataFactoryInterface
  22. {
  23.     private $propertyInfo;
  24.     private $decorated;
  25.     public function __construct(PropertyInfoExtractorInterface $propertyInfoPropertyMetadataFactoryInterface $decorated null)
  26.     {
  27.         $this->propertyInfo $propertyInfo;
  28.         $this->decorated $decorated;
  29.     }
  30.     /**
  31.      * {@inheritdoc}
  32.      */
  33.     public function create(string $resourceClassstring $name, array $options = []): PropertyMetadata
  34.     {
  35.         if (null === $this->decorated) {
  36.             $propertyMetadata = new PropertyMetadata();
  37.         } else {
  38.             try {
  39.                 $propertyMetadata $this->decorated->create($resourceClass$name$options);
  40.             } catch (PropertyNotFoundException $propertyNotFoundException) {
  41.                 $propertyMetadata = new PropertyMetadata();
  42.             }
  43.         }
  44.         if (null === $propertyMetadata->getType()) {
  45.             $types $this->propertyInfo->getTypes($resourceClass$name$options);
  46.             if (isset($types[0])) {
  47.                 $propertyMetadata $propertyMetadata->withType($types[0]);
  48.             }
  49.         }
  50.         if (null === $propertyMetadata->getDescription() && null !== $description $this->propertyInfo->getShortDescription($resourceClass$name$options)) {
  51.             $propertyMetadata $propertyMetadata->withDescription($description);
  52.         }
  53.         if (null === $propertyMetadata->isReadable() && null !== $readable $this->propertyInfo->isReadable($resourceClass$name$options)) {
  54.             $propertyMetadata $propertyMetadata->withReadable($readable);
  55.         }
  56.         if (null === $propertyMetadata->isWritable() && null !== $writable $this->propertyInfo->isWritable($resourceClass$name$options)) {
  57.             $propertyMetadata $propertyMetadata->withWritable($writable);
  58.         }
  59.         if (method_exists($this->propertyInfo'isInitializable')) {
  60.             if (null === $propertyMetadata->isInitializable() && null !== $initializable $this->propertyInfo->isInitializable($resourceClass$name$options)) {
  61.                 $propertyMetadata $propertyMetadata->withInitializable($initializable);
  62.             }
  63.         } else {
  64.             // BC layer for Symfony < 4.2
  65.             $ref = new \ReflectionClass($resourceClass);
  66.             if ($ref->isInstantiable() && $constructor $ref->getConstructor()) {
  67.                 foreach ($constructor->getParameters() as $constructorParameter) {
  68.                     if ($constructorParameter->name === $name && null === $propertyMetadata->isInitializable()) {
  69.                         $propertyMetadata $propertyMetadata->withInitializable(true);
  70.                     }
  71.                 }
  72.             }
  73.         }
  74.         return $propertyMetadata;
  75.     }
  76. }