vendor/api-platform/core/src/Bridge/Doctrine/Orm/Metadata/Property/DoctrineOrmPropertyMetadataFactory.php line 40

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\Doctrine\Orm\Metadata\Property;
  12. use ApiPlatform\Core\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
  13. use ApiPlatform\Core\Metadata\Property\PropertyMetadata;
  14. use Doctrine\ORM\Mapping\ClassMetadataInfo;
  15. use Doctrine\Persistence\ManagerRegistry;
  16. /**
  17.  * Use Doctrine metadata to populate the identifier property.
  18.  *
  19.  * @author Kévin Dunglas <dunglas@gmail.com>
  20.  */
  21. final class DoctrineOrmPropertyMetadataFactory implements PropertyMetadataFactoryInterface
  22. {
  23.     private $decorated;
  24.     private $managerRegistry;
  25.     public function __construct(ManagerRegistry $managerRegistryPropertyMetadataFactoryInterface $decorated)
  26.     {
  27.         $this->managerRegistry $managerRegistry;
  28.         $this->decorated $decorated;
  29.     }
  30.     /**
  31.      * {@inheritdoc}
  32.      */
  33.     public function create(string $resourceClassstring $property, array $options = []): PropertyMetadata
  34.     {
  35.         $propertyMetadata $this->decorated->create($resourceClass$property$options);
  36.         if (null !== $propertyMetadata->isIdentifier()) {
  37.             return $propertyMetadata;
  38.         }
  39.         $manager $this->managerRegistry->getManagerForClass($resourceClass);
  40.         if (!$manager) {
  41.             return $propertyMetadata;
  42.         }
  43.         $doctrineClassMetadata $manager->getClassMetadata($resourceClass);
  44.         $identifiers $doctrineClassMetadata->getIdentifier();
  45.         foreach ($identifiers as $identifier) {
  46.             if ($identifier === $property) {
  47.                 $propertyMetadata $propertyMetadata->withIdentifier(true);
  48.                 if (null !== $propertyMetadata->isWritable()) {
  49.                     break;
  50.                 }
  51.                 if ($doctrineClassMetadata instanceof ClassMetadataInfo) {
  52.                     $writable $doctrineClassMetadata->isIdentifierNatural();
  53.                 } else {
  54.                     $writable false;
  55.                 }
  56.                 $propertyMetadata $propertyMetadata->withWritable($writable);
  57.                 break;
  58.             }
  59.         }
  60.         if (null === $propertyMetadata->isIdentifier()) {
  61.             $propertyMetadata $propertyMetadata->withIdentifier(false);
  62.         }
  63.         return $propertyMetadata;
  64.     }
  65. }