vendor/shopware/core/Framework/Struct/Collection.php line 88

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Framework\Struct;
  3. abstract class Collection extends Struct implements \IteratorAggregate\Countable
  4. {
  5.     /**
  6.      * @var array
  7.      */
  8.     protected $elements = [];
  9.     public function __construct(iterable $elements = [])
  10.     {
  11.         foreach ($elements as $key => $element) {
  12.             $this->set($key$element);
  13.         }
  14.     }
  15.     public function add($element): void
  16.     {
  17.         $this->validateType($element);
  18.         $this->elements[] = $element;
  19.     }
  20.     public function set($key$element): void
  21.     {
  22.         $this->validateType($element);
  23.         if ($key === null) {
  24.             $this->elements[] = $element;
  25.         } else {
  26.             $this->elements[$key] = $element;
  27.         }
  28.     }
  29.     /**
  30.      * @param mixed|null $key
  31.      *
  32.      * @return mixed|null
  33.      */
  34.     public function get($key)
  35.     {
  36.         if ($this->has($key)) {
  37.             return $this->elements[$key];
  38.         }
  39.         return null;
  40.     }
  41.     public function clear(): void
  42.     {
  43.         $this->elements = [];
  44.     }
  45.     public function count(): int
  46.     {
  47.         return \count($this->elements);
  48.     }
  49.     public function getKeys(): array
  50.     {
  51.         return array_keys($this->elements);
  52.     }
  53.     public function has($key): bool
  54.     {
  55.         return \array_key_exists($key$this->elements);
  56.     }
  57.     public function map(\Closure $closure): array
  58.     {
  59.         return array_map($closure$this->elements);
  60.     }
  61.     public function reduce(\Closure $closure$initial null)
  62.     {
  63.         return array_reduce($this->elements$closure$initial);
  64.     }
  65.     public function fmap(\Closure $closure): array
  66.     {
  67.         return array_filter($this->map($closure));
  68.     }
  69.     public function sort(\Closure $closure): void
  70.     {
  71.         uasort($this->elements$closure);
  72.     }
  73.     /**
  74.      * @return static
  75.      */
  76.     public function filterInstance(string $class)
  77.     {
  78.         return $this->filter(static function ($item) use ($class) {
  79.             return $item instanceof $class;
  80.         });
  81.     }
  82.     /**
  83.      * @return static
  84.      */
  85.     public function filter(\Closure $closure)
  86.     {
  87.         return $this->createNew(array_filter($this->elements$closure));
  88.     }
  89.     /**
  90.      * @return static
  91.      */
  92.     public function slice(int $offset, ?int $length null)
  93.     {
  94.         return $this->createNew(\array_slice($this->elements$offset$lengthtrue));
  95.     }
  96.     public function getElements(): array
  97.     {
  98.         return $this->elements;
  99.     }
  100.     public function jsonSerialize(): array
  101.     {
  102.         return array_values($this->elements);
  103.     }
  104.     public function first()
  105.     {
  106.         return array_values($this->elements)[0] ?? null;
  107.     }
  108.     public function getAt(int $position)
  109.     {
  110.         return array_values($this->elements)[$position] ?? null;
  111.     }
  112.     public function last()
  113.     {
  114.         return array_values($this->elements)[\count($this->elements) - 1] ?? null;
  115.     }
  116.     /**
  117.      * @param int|string $key
  118.      */
  119.     public function remove($key): void
  120.     {
  121.         unset($this->elements[$key]);
  122.     }
  123.     /**
  124.      * @deprecated tag:v6.5.0 - reason:return-type-change - Return type will be changed to \Traversable
  125.      */
  126.     #[\ReturnTypeWillChange]
  127.     public function getIterator(): \Generator/* :\Traversable */
  128.     {
  129.         yield from $this->elements;
  130.     }
  131.     protected function getExpectedClass(): ?string
  132.     {
  133.         return null;
  134.     }
  135.     /**
  136.      * @return static
  137.      */
  138.     protected function createNew(iterable $elements = [])
  139.     {
  140.         return new static($elements);
  141.     }
  142.     protected function validateType($element): void
  143.     {
  144.         $expectedClass $this->getExpectedClass();
  145.         if ($expectedClass === null) {
  146.             return;
  147.         }
  148.         if (!$element instanceof $expectedClass) {
  149.             $elementClass \get_class($element);
  150.             throw new \InvalidArgumentException(
  151.                 sprintf('Expected collection element of type %s got %s'$expectedClass$elementClass)
  152.             );
  153.         }
  154.     }
  155. }