vendor/easycorp/easyadmin-bundle/src/Collection/ActionCollection.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\ActionDto;
  5. /**
  6.  * @author Javier Eguiluz <javier.eguiluz@gmail.com>
  7.  */
  8. final class ActionCollection implements CollectionInterface
  9. {
  10.     /** @var ActionDto[] */
  11.     private $actions;
  12.     /**
  13.      * @param ActionDto[] $actions
  14.      */
  15.     private function __construct(array $actions)
  16.     {
  17.         $this->actions $actions;
  18.     }
  19.     public function __clone()
  20.     {
  21.         foreach ($this->actions as $actionName => $actionDto) {
  22.             $this->actions[$actionName] = clone $actionDto;
  23.         }
  24.     }
  25.     /**
  26.      * @param ActionDto[] $actions
  27.      */
  28.     public static function new(array $actions): self
  29.     {
  30.         return new self($actions);
  31.     }
  32.     /**
  33.      * @return ActionDto[]
  34.      */
  35.     public function all(): array
  36.     {
  37.         return $this->actions;
  38.     }
  39.     public function get(string $actionName): ?ActionDto
  40.     {
  41.         return $this->actions[$actionName] ?? null;
  42.     }
  43.     public function offsetExists($offset): bool
  44.     {
  45.         return \array_key_exists($offset$this->actions);
  46.     }
  47.     public function offsetGet($offset)
  48.     {
  49.         return $this->actions[$offset];
  50.     }
  51.     public function offsetSet($offset$value): void
  52.     {
  53.         $this->actions[$offset] = $value;
  54.     }
  55.     public function offsetUnset($offset): void
  56.     {
  57.         unset($this->actions[$offset]);
  58.     }
  59.     public function count(): int
  60.     {
  61.         return \count($this->actions);
  62.     }
  63.     /**
  64.      * @return \ArrayIterator|\Traversable|ActionDto[]
  65.      */
  66.     public function getIterator(): \ArrayIterator
  67.     {
  68.         return new \ArrayIterator($this->actions);
  69.     }
  70.     public function getEntityActions(): self
  71.     {
  72.         return self::new(array_filter($this->actions, static function (ActionDto $action) {
  73.             return $action->isEntityAction();
  74.         }));
  75.     }
  76.     public function getGlobalActions(): self
  77.     {
  78.         return self::new(array_filter($this->actions, static function (ActionDto $action) {
  79.             return $action->isGlobalAction();
  80.         }));
  81.     }
  82.     public function getBatchActions(): self
  83.     {
  84.         return self::new(array_filter($this->actions, static function (ActionDto $action) {
  85.             return $action->isBatchAction();
  86.         }));
  87.     }
  88. }