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