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

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