vendor/easycorp/easyadmin-bundle/src/Collection/EntityCollection.php line 11

Open in your IDE?
  1. <?php
  2. namespace EasyCorp\Bundle\EasyAdminBundle\Collection;
  3. use EasyCorp\Bundle\EasyAdminBundle\Contracts\Collection\CollectionInterface;
  4. use EasyCorp\Bundle\EasyAdminBundle\Dto\EntityDto;
  5. /**
  6.  * @author Javier Eguiluz <javier.eguiluz@gmail.com>
  7.  */
  8. final class EntityCollection implements CollectionInterface
  9. {
  10.     /** @var EntityDto[] */
  11.     private $entities;
  12.     /**
  13.      * @param EntityDto[] $entities
  14.      */
  15.     private function __construct(array $entities)
  16.     {
  17.         $this->entities $entities;
  18.     }
  19.     /**
  20.      * @param EntityDto[] $entities
  21.      */
  22.     public static function new(array $entities): self
  23.     {
  24.         return new self($entities);
  25.     }
  26.     public function get(string $entityId): ?EntityDto
  27.     {
  28.         return $this->entities[$entityId] ?? null;
  29.     }
  30.     public function set(EntityDto $newOrUpdatedEntity): void
  31.     {
  32.         $this->entities[$newOrUpdatedEntity->getPrimaryKeyValueAsString()] = $newOrUpdatedEntity;
  33.     }
  34.     public function offsetExists($offset): bool
  35.     {
  36.         return \array_key_exists($offset$this->entities);
  37.     }
  38.     /**
  39.      * @return ?EntityDto
  40.      */
  41.     public function offsetGet($offset)
  42.     {
  43.         return $this->entities[$offset];
  44.     }
  45.     public function offsetSet($offset$value): void
  46.     {
  47.         $this->entities[$offset] = $value;
  48.     }
  49.     public function offsetUnset($offset): void
  50.     {
  51.         unset($this->entities[$offset]);
  52.     }
  53.     public function count(): int
  54.     {
  55.         return \count($this->entities);
  56.     }
  57.     /**
  58.      * @return \ArrayIterator|\Traversable|EntityDto[]
  59.      */
  60.     public function getIterator(): \ArrayIterator
  61.     {
  62.         return new \ArrayIterator($this->entities);
  63.     }
  64. }