vendor/api-platform/core/src/Core/Metadata/Property/Factory/AnnotationPropertyMetadataFactory.php line 47

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\Metadata\Property\Factory;
  12. use ApiPlatform\Core\Annotation\ApiProperty;
  13. use ApiPlatform\Core\Exception\PropertyNotFoundException;
  14. use ApiPlatform\Core\Metadata\Property\PropertyMetadata;
  15. use ApiPlatform\Util\Reflection;
  16. use Doctrine\Common\Annotations\Reader;
  17. /**
  18.  * Creates a property metadata from {@see ApiProperty} annotations.
  19.  *
  20.  * @author Kévin Dunglas <dunglas@gmail.com>
  21.  */
  22. final class AnnotationPropertyMetadataFactory implements PropertyMetadataFactoryInterface
  23. {
  24.     private $reader;
  25.     private $decorated;
  26.     public function __construct(Reader $reader nullPropertyMetadataFactoryInterface $decorated null)
  27.     {
  28.         $this->reader $reader;
  29.         $this->decorated $decorated;
  30.     }
  31.     public function create(string $resourceClassstring $property, array $options = []): PropertyMetadata
  32.     {
  33.         if (null === ($options['deprecate'] ?? null)) {
  34.             trigger_deprecation('api-platform/core''2.7'sprintf('Decorating the legacy %s is deprecated, use %s instead.'PropertyMetadataFactoryInterface::class, \ApiPlatform\Metadata\Property\Factory\PropertyMetadataFactoryInterface::class));
  35.         }
  36.         $parentPropertyMetadata null;
  37.         if ($this->decorated) {
  38.             try {
  39.                 $parentPropertyMetadata $this->decorated->create($resourceClass$property$options);
  40.             } catch (PropertyNotFoundException $propertyNotFoundException) {
  41.                 // Ignore not found exception from decorated factories
  42.             }
  43.         }
  44.         try {
  45.             $reflectionClass = new \ReflectionClass($resourceClass);
  46.         } catch (\ReflectionException $reflectionException) {
  47.             return $this->handleNotFound($parentPropertyMetadata$resourceClass$property);
  48.         }
  49.         if ($reflectionClass->hasProperty($property)) {
  50.             $annotation null;
  51.             $reflectionProperty $reflectionClass->getProperty($property);
  52.             if (\PHP_VERSION_ID >= 80000 && $attributes $reflectionProperty->getAttributes(ApiProperty::class)) {
  53.                 $annotation $attributes[0]->newInstance();
  54.             } elseif (null !== $this->reader) {
  55.                 $annotation $this->reader->getPropertyAnnotation($reflectionPropertyApiProperty::class);
  56.             }
  57.             if ($annotation instanceof ApiProperty) {
  58.                 return $this->createMetadata($annotation$parentPropertyMetadata);
  59.             }
  60.         }
  61.         foreach (array_merge(Reflection::ACCESSOR_PREFIXESReflection::MUTATOR_PREFIXES) as $prefix) {
  62.             $methodName $prefix.ucfirst($property);
  63.             if (!$reflectionClass->hasMethod($methodName)) {
  64.                 continue;
  65.             }
  66.             $reflectionMethod $reflectionClass->getMethod($methodName);
  67.             if (!$reflectionMethod->isPublic()) {
  68.                 continue;
  69.             }
  70.             $annotation null;
  71.             if (\PHP_VERSION_ID >= 80000 && $attributes $reflectionMethod->getAttributes(ApiProperty::class)) {
  72.                 $annotation $attributes[0]->newInstance();
  73.             } elseif (null !== $this->reader) {
  74.                 $annotation $this->reader->getMethodAnnotation($reflectionMethodApiProperty::class);
  75.             }
  76.             if ($annotation instanceof ApiProperty) {
  77.                 return $this->createMetadata($annotation$parentPropertyMetadata);
  78.             }
  79.         }
  80.         return $this->handleNotFound($parentPropertyMetadata$resourceClass$property);
  81.     }
  82.     /**
  83.      * Returns the metadata from the decorated factory if available or throws an exception.
  84.      *
  85.      * @throws PropertyNotFoundException
  86.      */
  87.     private function handleNotFound(?PropertyMetadata $parentPropertyMetadatastring $resourceClassstring $property): PropertyMetadata
  88.     {
  89.         if (null !== $parentPropertyMetadata) {
  90.             return $parentPropertyMetadata;
  91.         }
  92.         throw new PropertyNotFoundException(sprintf('Property "%s" of class "%s" not found.'$property$resourceClass));
  93.     }
  94.     private function createMetadata(ApiProperty $annotationPropertyMetadata $parentPropertyMetadata null): PropertyMetadata
  95.     {
  96.         if (null === $parentPropertyMetadata) {
  97.             return new PropertyMetadata(
  98.                 null,
  99.                 $annotation->description,
  100.                 $annotation->readable,
  101.                 $annotation->writable,
  102.                 $annotation->readableLink,
  103.                 $annotation->writableLink,
  104.                 $annotation->required,
  105.                 $annotation->identifier,
  106.                 $annotation->iri,
  107.                 null,
  108.                 $annotation->attributes,
  109.                 null,
  110.                 null,
  111.                 $annotation->default,
  112.                 $annotation->example
  113.             );
  114.         }
  115.         $propertyMetadata $parentPropertyMetadata;
  116.         foreach ([['get''description'], ['is''readable'], ['is''writable'], ['is''readableLink'], ['is''writableLink'], ['is''required'], ['get''iri'], ['is''identifier'], ['get''attributes'], ['get''default'], ['get''example']] as $property) {
  117.             if (null !== $value $annotation->{$property[1]}) {
  118.                 $propertyMetadata $this->createWith($propertyMetadata$property$value);
  119.             }
  120.         }
  121.         return $propertyMetadata;
  122.     }
  123.     private function createWith(PropertyMetadata $propertyMetadata, array $property$value): PropertyMetadata
  124.     {
  125.         $wither 'with'.ucfirst($property[1]);
  126.         return $propertyMetadata->{$wither}($value);
  127.     }
  128. }