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