vendor/easycorp/easyadmin-bundle/src/Dto/EntityDto.php line 20

  1. <?php
  2. namespace EasyCorp\Bundle\EasyAdminBundle\Dto;
  3. use Doctrine\ORM\Mapping\ClassMetadata;
  4. use Doctrine\ORM\Mapping\FieldMapping;
  5. use Doctrine\ORM\Mapping\ManyToManyAssociationMapping;
  6. use Doctrine\ORM\Mapping\ManyToOneAssociationMapping;
  7. use Doctrine\ORM\Mapping\OneToManyAssociationMapping;
  8. use Doctrine\ORM\Mapping\OneToOneAssociationMapping;
  9. use EasyCorp\Bundle\EasyAdminBundle\Collection\ActionCollection;
  10. use EasyCorp\Bundle\EasyAdminBundle\Collection\FieldCollection;
  11. use EasyCorp\Bundle\EasyAdminBundle\Config\KeyValueStore;
  12. use Symfony\Component\ExpressionLanguage\Expression;
  13. use Symfony\Component\PropertyAccess\PropertyAccess;
  14. /**
  15.  * @author Javier Eguiluz <javier.eguiluz@gmail.com>
  16.  */
  17. final class EntityDto
  18. {
  19.     private bool $isAccessible true;
  20.     private string $fqcn;
  21.     private ClassMetadata $metadata;
  22.     private $instance;
  23.     private $primaryKeyName;
  24.     private mixed $primaryKeyValue null;
  25.     private string|Expression|null $permission;
  26.     private ?FieldCollection $fields null;
  27.     private ?ActionCollection $actions null;
  28.     public function __construct(string $entityFqcnClassMetadata $entityMetadatastring|Expression|null $entityPermission null/* ?object */ $entityInstance null)
  29.     {
  30.         if (!\is_object($entityInstance)
  31.             && null !== $entityInstance) {
  32.             trigger_deprecation(
  33.                 'easycorp/easyadmin-bundle',
  34.                 '4.0.5',
  35.                 'Argument "%s" for "%s" must be one of these types: %s. Passing type "%s" will cause an error in 5.0.0.',
  36.                 '$entityInstance',
  37.                 __METHOD__,
  38.                 '"object" or "null"',
  39.                 \gettype($entityInstance)
  40.             );
  41.         }
  42.         $this->fqcn $entityFqcn;
  43.         $this->metadata $entityMetadata;
  44.         $this->instance $entityInstance;
  45.         $this->primaryKeyName $this->metadata->getIdentifierFieldNames()[0];
  46.         $this->permission $entityPermission;
  47.     }
  48.     public function __toString(): string
  49.     {
  50.         return $this->toString();
  51.     }
  52.     public function getFqcn(): string
  53.     {
  54.         return $this->fqcn;
  55.     }
  56.     public function getName(): string
  57.     {
  58.         return basename(str_replace('\\''/'$this->fqcn));
  59.     }
  60.     public function toString(): string
  61.     {
  62.         if (null === $this->instance) {
  63.             return '';
  64.         }
  65.         if (method_exists($this->instance'__toString')) {
  66.             return (string) $this->instance;
  67.         }
  68.         return sprintf('%s #%s'$this->getName(), substr($this->getPrimaryKeyValueAsString(), 016));
  69.     }
  70.     public function getInstance()/* : ?object */
  71.     {
  72.         return $this->instance;
  73.     }
  74.     public function getPrimaryKeyName(): ?string
  75.     {
  76.         return $this->primaryKeyName;
  77.     }
  78.     public function getPrimaryKeyValue(): mixed
  79.     {
  80.         if (null === $this->instance) {
  81.             return null;
  82.         }
  83.         if (null !== $this->primaryKeyValue) {
  84.             return $this->primaryKeyValue;
  85.         }
  86.         $propertyAccessor PropertyAccess::createPropertyAccessorBuilder()
  87.             ->enableExceptionOnInvalidIndex()
  88.             ->getPropertyAccessor();
  89.         $primaryKeyValue $propertyAccessor->getValue($this->instance$this->primaryKeyName);
  90.         return $this->primaryKeyValue $primaryKeyValue;
  91.     }
  92.     public function getPrimaryKeyValueAsString(): string
  93.     {
  94.         return (string) $this->getPrimaryKeyValue();
  95.     }
  96.     public function getPermission(): string|Expression|null
  97.     {
  98.         return $this->permission;
  99.     }
  100.     public function isAccessible(): bool
  101.     {
  102.         return $this->isAccessible;
  103.     }
  104.     public function markAsInaccessible(): void
  105.     {
  106.         $this->isAccessible false;
  107.         $this->instance null;
  108.         $this->fields null;
  109.     }
  110.     public function getFields(): ?FieldCollection
  111.     {
  112.         return $this->fields;
  113.     }
  114.     public function setFields(FieldCollection $fields): void
  115.     {
  116.         $this->fields $fields;
  117.     }
  118.     public function setActions(ActionCollection $actions): void
  119.     {
  120.         $this->actions $actions;
  121.     }
  122.     public function getActions(): ActionCollection
  123.     {
  124.         return $this->actions;
  125.     }
  126.     /**
  127.      * Returns the names of all properties defined in the entity, no matter
  128.      * if they are used or not in the application.
  129.      */
  130.     public function getAllPropertyNames(): array
  131.     {
  132.         return $this->metadata->getFieldNames();
  133.     }
  134.     public function getPropertyMetadata(string $propertyName): KeyValueStore
  135.     {
  136.         if (\array_key_exists($propertyName$this->metadata->fieldMappings)) {
  137.             /** @var FieldMapping|array $fieldMapping */
  138.             $fieldMapping $this->metadata->fieldMappings[$propertyName];
  139.             // Doctrine ORM 2.x returns an array and Doctrine ORM 3.x returns a FieldMapping object
  140.             if ($fieldMapping instanceof FieldMapping) {
  141.                 $fieldMapping = (array) $fieldMapping;
  142.             }
  143.             return KeyValueStore::new($fieldMapping);
  144.         }
  145.         if (\array_key_exists($propertyName$this->metadata->associationMappings)) {
  146.             /** @var OneToOneAssociationMapping|OneToManyAssociationMapping|ManyToOneAssociationMapping|ManyToManyAssociationMapping|array $associationMapping */
  147.             $associationMapping $this->metadata->associationMappings[$propertyName];
  148.             // Doctrine ORM 2.x returns an array and Doctrine ORM 3.x returns one of the many *Mapping objects
  149.             // there's not a single interface implemented by all of them, so let's only check if it's an object
  150.             if (\is_object($associationMapping)) {
  151.                 // Doctrine ORM 3.x doesn't include the 'type' key that tells the type of association
  152.                 // recreate that key to keep the code compatible with both versions
  153.                 $associationType = match (true) {
  154.                     $associationMapping instanceof OneToOneAssociationMapping => ClassMetadata::ONE_TO_ONE,
  155.                     $associationMapping instanceof OneToManyAssociationMapping => ClassMetadata::ONE_TO_MANY,
  156.                     $associationMapping instanceof ManyToOneAssociationMapping => ClassMetadata::MANY_TO_ONE,
  157.                     $associationMapping instanceof ManyToManyAssociationMapping => ClassMetadata::MANY_TO_MANY,
  158.                     default => null,
  159.                 };
  160.                 $associationMapping = (array) $associationMapping;
  161.                 $associationMapping['type'] = $associationType;
  162.             }
  163.             return KeyValueStore::new($associationMapping);
  164.         }
  165.         throw new \InvalidArgumentException(sprintf('The "%s" field does not exist in the "%s" entity.'$propertyName$this->getFqcn()));
  166.     }
  167.     public function getPropertyDataType(string $propertyName)
  168.     {
  169.         return $this->getPropertyMetadata($propertyName)->get('type');
  170.     }
  171.     public function hasProperty(string $propertyName): bool
  172.     {
  173.         return \array_key_exists($propertyName$this->metadata->fieldMappings)
  174.             || \array_key_exists($propertyName$this->metadata->associationMappings);
  175.     }
  176.     public function isAssociation(string $propertyName): bool
  177.     {
  178.         return \array_key_exists($propertyName$this->metadata->associationMappings)
  179.             || (str_contains($propertyName'.') && !$this->isEmbeddedClassProperty($propertyName));
  180.     }
  181.     public function isToOneAssociation(string $propertyName): bool
  182.     {
  183.         $associationType $this->getPropertyMetadata($propertyName)->get('type');
  184.         return \in_array($associationType, [ClassMetadata::ONE_TO_ONEClassMetadata::MANY_TO_ONE], true);
  185.     }
  186.     public function isToManyAssociation(string $propertyName): bool
  187.     {
  188.         $associationType $this->getPropertyMetadata($propertyName)->get('type');
  189.         return \in_array($associationType, [ClassMetadata::ONE_TO_MANYClassMetadata::MANY_TO_MANY], true);
  190.     }
  191.     public function isEmbeddedClassProperty(string $propertyName): bool
  192.     {
  193.         $propertyNameParts explode('.'$propertyName2);
  194.         return \array_key_exists($propertyNameParts[0], $this->metadata->embeddedClasses);
  195.     }
  196.     public function setInstance(?object $newEntityInstance): void
  197.     {
  198.         if (null !== $this->instance && null !== $newEntityInstance && !$newEntityInstance instanceof $this->fqcn) {
  199.             throw new \InvalidArgumentException(sprintf('The new entity instance must be of the same type as the previous instance (original instance: "%s", new instance: "%s").'$this->fqcn$newEntityInstance::class));
  200.         }
  201.         $this->instance $newEntityInstance;
  202.         $this->primaryKeyValue null;
  203.     }
  204.     public function newWithInstance(/* object */ $newEntityInstance): self
  205.     {
  206.         if (!\is_object($newEntityInstance)) {
  207.             trigger_deprecation(
  208.                 'easycorp/easyadmin-bundle',
  209.                 '4.0.5',
  210.                 'Argument "%s" for "%s" must be one of these types: %s. Passing type "%s" will cause an error in 5.0.0.',
  211.                 '$newEntityInstance',
  212.                 __METHOD__,
  213.                 '"object"',
  214.                 \gettype($newEntityInstance)
  215.             );
  216.         }
  217.         if (null !== $this->instance && !$newEntityInstance instanceof $this->fqcn) {
  218.             throw new \InvalidArgumentException(sprintf('The new entity instance must be of the same type as the previous instance (original instance: "%s", new instance: "%s").'$this->fqcn$newEntityInstance::class));
  219.         }
  220.         return new self($this->fqcn$this->metadata$this->permission$newEntityInstance);
  221.     }
  222. }