vendor/doctrine/collections/src/AbstractLazyCollection.php line 240

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Doctrine\Common\Collections;
  4. use Closure;
  5. use LogicException;
  6. use ReturnTypeWillChange;
  7. use Traversable;
  8. /**
  9.  * Lazy collection that is backed by a concrete collection
  10.  *
  11.  * @psalm-template TKey of array-key
  12.  * @psalm-template T
  13.  * @template-implements Collection<TKey,T>
  14.  */
  15. abstract class AbstractLazyCollection implements Collection
  16. {
  17.     /**
  18.      * The backed collection to use
  19.      *
  20.      * @psalm-var Collection<TKey,T>|null
  21.      * @var Collection<mixed>|null
  22.      */
  23.     protected Collection|null $collection;
  24.     protected bool $initialized false;
  25.     /**
  26.      * {@inheritDoc}
  27.      *
  28.      * @return int
  29.      */
  30.     #[ReturnTypeWillChange]
  31.     public function count()
  32.     {
  33.         $this->initialize();
  34.         return $this->collection->count();
  35.     }
  36.     /**
  37.      * {@inheritDoc}
  38.      */
  39.     public function add(mixed $element)
  40.     {
  41.         $this->initialize();
  42.         $this->collection->add($element);
  43.     }
  44.     /**
  45.      * {@inheritDoc}
  46.      */
  47.     public function clear()
  48.     {
  49.         $this->initialize();
  50.         $this->collection->clear();
  51.     }
  52.     /**
  53.      * {@inheritDoc}
  54.      *
  55.      * @template TMaybeContained
  56.      */
  57.     public function contains(mixed $element)
  58.     {
  59.         $this->initialize();
  60.         return $this->collection->contains($element);
  61.     }
  62.     /**
  63.      * {@inheritDoc}
  64.      */
  65.     public function isEmpty()
  66.     {
  67.         $this->initialize();
  68.         return $this->collection->isEmpty();
  69.     }
  70.     /**
  71.      * {@inheritDoc}
  72.      */
  73.     public function remove(string|int $key)
  74.     {
  75.         $this->initialize();
  76.         return $this->collection->remove($key);
  77.     }
  78.     /**
  79.      * {@inheritDoc}
  80.      */
  81.     public function removeElement(mixed $element)
  82.     {
  83.         $this->initialize();
  84.         return $this->collection->removeElement($element);
  85.     }
  86.     /**
  87.      * {@inheritDoc}
  88.      */
  89.     public function containsKey(string|int $key)
  90.     {
  91.         $this->initialize();
  92.         return $this->collection->containsKey($key);
  93.     }
  94.     /**
  95.      * {@inheritDoc}
  96.      */
  97.     public function get(string|int $key)
  98.     {
  99.         $this->initialize();
  100.         return $this->collection->get($key);
  101.     }
  102.     /**
  103.      * {@inheritDoc}
  104.      */
  105.     public function getKeys()
  106.     {
  107.         $this->initialize();
  108.         return $this->collection->getKeys();
  109.     }
  110.     /**
  111.      * {@inheritDoc}
  112.      */
  113.     public function getValues()
  114.     {
  115.         $this->initialize();
  116.         return $this->collection->getValues();
  117.     }
  118.     /**
  119.      * {@inheritDoc}
  120.      */
  121.     public function set(string|int $keymixed $value)
  122.     {
  123.         $this->initialize();
  124.         $this->collection->set($key$value);
  125.     }
  126.     /**
  127.      * {@inheritDoc}
  128.      */
  129.     public function toArray()
  130.     {
  131.         $this->initialize();
  132.         return $this->collection->toArray();
  133.     }
  134.     /**
  135.      * {@inheritDoc}
  136.      */
  137.     public function first()
  138.     {
  139.         $this->initialize();
  140.         return $this->collection->first();
  141.     }
  142.     /**
  143.      * {@inheritDoc}
  144.      */
  145.     public function last()
  146.     {
  147.         $this->initialize();
  148.         return $this->collection->last();
  149.     }
  150.     /**
  151.      * {@inheritDoc}
  152.      */
  153.     public function key()
  154.     {
  155.         $this->initialize();
  156.         return $this->collection->key();
  157.     }
  158.     /**
  159.      * {@inheritDoc}
  160.      */
  161.     public function current()
  162.     {
  163.         $this->initialize();
  164.         return $this->collection->current();
  165.     }
  166.     /**
  167.      * {@inheritDoc}
  168.      */
  169.     public function next()
  170.     {
  171.         $this->initialize();
  172.         return $this->collection->next();
  173.     }
  174.     /**
  175.      * {@inheritDoc}
  176.      */
  177.     public function exists(Closure $p)
  178.     {
  179.         $this->initialize();
  180.         return $this->collection->exists($p);
  181.     }
  182.     /**
  183.      * {@inheritDoc}
  184.      */
  185.     public function findFirst(Closure $p)
  186.     {
  187.         $this->initialize();
  188.         return $this->collection->findFirst($p);
  189.     }
  190.     /**
  191.      * @psalm-param Closure(T, TKey):bool $p
  192.      *
  193.      * @return ReadableCollection<mixed>
  194.      * @psalm-return ReadableCollection<TKey, T>
  195.      */
  196.     public function filter(Closure $p)
  197.     {
  198.         $this->initialize();
  199.         return $this->collection->filter($p);
  200.     }
  201.     /**
  202.      * {@inheritDoc}
  203.      */
  204.     public function forAll(Closure $p)
  205.     {
  206.         $this->initialize();
  207.         return $this->collection->forAll($p);
  208.     }
  209.     /**
  210.      * {@inheritDoc}
  211.      *
  212.      * @psalm-param Closure(T):U $func
  213.      *
  214.      * @return ReadableCollection<mixed>
  215.      * @psalm-return ReadableCollection<TKey, U>
  216.      *
  217.      * @psalm-template U
  218.      */
  219.     public function map(Closure $func)
  220.     {
  221.         $this->initialize();
  222.         return $this->collection->map($func);
  223.     }
  224.     /**
  225.      * {@inheritDoc}
  226.      */
  227.     public function reduce(Closure $funcmixed $initial null)
  228.     {
  229.         $this->initialize();
  230.         return $this->collection->reduce($func$initial);
  231.     }
  232.     /**
  233.      * {@inheritDoc}
  234.      */
  235.     public function partition(Closure $p)
  236.     {
  237.         $this->initialize();
  238.         return $this->collection->partition($p);
  239.     }
  240.     /**
  241.      * {@inheritDoc}
  242.      *
  243.      * @template TMaybeContained
  244.      */
  245.     public function indexOf(mixed $element)
  246.     {
  247.         $this->initialize();
  248.         return $this->collection->indexOf($element);
  249.     }
  250.     /**
  251.      * {@inheritDoc}
  252.      */
  253.     public function slice(int $offsetint|null $length null)
  254.     {
  255.         $this->initialize();
  256.         return $this->collection->slice($offset$length);
  257.     }
  258.     /**
  259.      * {@inheritDoc}
  260.      *
  261.      * @return Traversable<int|string, mixed>
  262.      * @psalm-return Traversable<TKey,T>
  263.      */
  264.     #[ReturnTypeWillChange]
  265.     public function getIterator()
  266.     {
  267.         $this->initialize();
  268.         return $this->collection->getIterator();
  269.     }
  270.     /**
  271.      * {@inheritDoc}
  272.      *
  273.      * @param TKey $offset
  274.      *
  275.      * @return bool
  276.      */
  277.     #[ReturnTypeWillChange]
  278.     public function offsetExists(mixed $offset)
  279.     {
  280.         $this->initialize();
  281.         return $this->collection->offsetExists($offset);
  282.     }
  283.     /**
  284.      * {@inheritDoc}
  285.      *
  286.      * @param TKey $offset
  287.      *
  288.      * @return T|null
  289.      */
  290.     #[ReturnTypeWillChange]
  291.     public function offsetGet(mixed $offset)
  292.     {
  293.         $this->initialize();
  294.         return $this->collection->offsetGet($offset);
  295.     }
  296.     /**
  297.      * {@inheritDoc}
  298.      *
  299.      * @param TKey|null $offset
  300.      * @param T         $value
  301.      *
  302.      * @return void
  303.      */
  304.     #[ReturnTypeWillChange]
  305.     public function offsetSet(mixed $offsetmixed $value)
  306.     {
  307.         $this->initialize();
  308.         $this->collection->offsetSet($offset$value);
  309.     }
  310.     /**
  311.      * @param TKey $offset
  312.      *
  313.      * @return void
  314.      */
  315.     #[ReturnTypeWillChange]
  316.     public function offsetUnset(mixed $offset)
  317.     {
  318.         $this->initialize();
  319.         $this->collection->offsetUnset($offset);
  320.     }
  321.     /**
  322.      * Is the lazy collection already initialized?
  323.      *
  324.      * @return bool
  325.      *
  326.      * @psalm-assert-if-true Collection<TKey,T> $this->collection
  327.      */
  328.     public function isInitialized()
  329.     {
  330.         return $this->initialized;
  331.     }
  332.     /**
  333.      * Initialize the collection
  334.      *
  335.      * @return void
  336.      *
  337.      * @psalm-assert Collection<TKey,T> $this->collection
  338.      */
  339.     protected function initialize()
  340.     {
  341.         if ($this->initialized) {
  342.             return;
  343.         }
  344.         $this->doInitialize();
  345.         $this->initialized true;
  346.         if ($this->collection === null) {
  347.             throw new LogicException('You must initialize the collection property in the doInitialize() method.');
  348.         }
  349.     }
  350.     /**
  351.      * Do the initialization logic
  352.      *
  353.      * @return void
  354.      */
  355.     abstract protected function doInitialize();
  356. }