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

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.     public function contains(mixed $element)
  56.     {
  57.         $this->initialize();
  58.         return $this->collection->contains($element);
  59.     }
  60.     /**
  61.      * {@inheritDoc}
  62.      */
  63.     public function isEmpty()
  64.     {
  65.         $this->initialize();
  66.         return $this->collection->isEmpty();
  67.     }
  68.     /**
  69.      * {@inheritDoc}
  70.      */
  71.     public function remove(string|int $key)
  72.     {
  73.         $this->initialize();
  74.         return $this->collection->remove($key);
  75.     }
  76.     /**
  77.      * {@inheritDoc}
  78.      */
  79.     public function removeElement(mixed $element)
  80.     {
  81.         $this->initialize();
  82.         return $this->collection->removeElement($element);
  83.     }
  84.     /**
  85.      * {@inheritDoc}
  86.      */
  87.     public function containsKey(string|int $key)
  88.     {
  89.         $this->initialize();
  90.         return $this->collection->containsKey($key);
  91.     }
  92.     /**
  93.      * {@inheritDoc}
  94.      */
  95.     public function get(string|int $key)
  96.     {
  97.         $this->initialize();
  98.         return $this->collection->get($key);
  99.     }
  100.     /**
  101.      * {@inheritDoc}
  102.      */
  103.     public function getKeys()
  104.     {
  105.         $this->initialize();
  106.         return $this->collection->getKeys();
  107.     }
  108.     /**
  109.      * {@inheritDoc}
  110.      */
  111.     public function getValues()
  112.     {
  113.         $this->initialize();
  114.         return $this->collection->getValues();
  115.     }
  116.     /**
  117.      * {@inheritDoc}
  118.      */
  119.     public function set(string|int $keymixed $value)
  120.     {
  121.         $this->initialize();
  122.         $this->collection->set($key$value);
  123.     }
  124.     /**
  125.      * {@inheritDoc}
  126.      */
  127.     public function toArray()
  128.     {
  129.         $this->initialize();
  130.         return $this->collection->toArray();
  131.     }
  132.     /**
  133.      * {@inheritDoc}
  134.      */
  135.     public function first()
  136.     {
  137.         $this->initialize();
  138.         return $this->collection->first();
  139.     }
  140.     /**
  141.      * {@inheritDoc}
  142.      */
  143.     public function last()
  144.     {
  145.         $this->initialize();
  146.         return $this->collection->last();
  147.     }
  148.     /**
  149.      * {@inheritDoc}
  150.      */
  151.     public function key()
  152.     {
  153.         $this->initialize();
  154.         return $this->collection->key();
  155.     }
  156.     /**
  157.      * {@inheritDoc}
  158.      */
  159.     public function current()
  160.     {
  161.         $this->initialize();
  162.         return $this->collection->current();
  163.     }
  164.     /**
  165.      * {@inheritDoc}
  166.      */
  167.     public function next()
  168.     {
  169.         $this->initialize();
  170.         return $this->collection->next();
  171.     }
  172.     /**
  173.      * {@inheritDoc}
  174.      */
  175.     public function exists(Closure $p)
  176.     {
  177.         $this->initialize();
  178.         return $this->collection->exists($p);
  179.     }
  180.     /**
  181.      * {@inheritDoc}
  182.      */
  183.     public function findFirst(Closure $p)
  184.     {
  185.         $this->initialize();
  186.         return $this->collection->findFirst($p);
  187.     }
  188.     /**
  189.      * {@inheritDoc}
  190.      */
  191.     public function filter(Closure $p)
  192.     {
  193.         $this->initialize();
  194.         return $this->collection->filter($p);
  195.     }
  196.     /**
  197.      * {@inheritDoc}
  198.      */
  199.     public function forAll(Closure $p)
  200.     {
  201.         $this->initialize();
  202.         return $this->collection->forAll($p);
  203.     }
  204.     /**
  205.      * {@inheritDoc}
  206.      */
  207.     public function map(Closure $func)
  208.     {
  209.         $this->initialize();
  210.         return $this->collection->map($func);
  211.     }
  212.     /**
  213.      * {@inheritDoc}
  214.      */
  215.     public function reduce(Closure $funcmixed $initial null)
  216.     {
  217.         $this->initialize();
  218.         return $this->collection->reduce($func$initial);
  219.     }
  220.     /**
  221.      * {@inheritDoc}
  222.      */
  223.     public function partition(Closure $p)
  224.     {
  225.         $this->initialize();
  226.         return $this->collection->partition($p);
  227.     }
  228.     /**
  229.      * {@inheritDoc}
  230.      *
  231.      * @template TMaybeContained
  232.      */
  233.     public function indexOf(mixed $element)
  234.     {
  235.         $this->initialize();
  236.         return $this->collection->indexOf($element);
  237.     }
  238.     /**
  239.      * {@inheritDoc}
  240.      */
  241.     public function slice(int $offsetint|null $length null)
  242.     {
  243.         $this->initialize();
  244.         return $this->collection->slice($offset$length);
  245.     }
  246.     /**
  247.      * {@inheritDoc}
  248.      *
  249.      * @return Traversable<int|string, mixed>
  250.      * @psalm-return Traversable<TKey,T>
  251.      */
  252.     #[ReturnTypeWillChange]
  253.     public function getIterator()
  254.     {
  255.         $this->initialize();
  256.         return $this->collection->getIterator();
  257.     }
  258.     /**
  259.      * {@inheritDoc}
  260.      *
  261.      * @param TKey $offset
  262.      *
  263.      * @return bool
  264.      */
  265.     #[ReturnTypeWillChange]
  266.     public function offsetExists(mixed $offset)
  267.     {
  268.         $this->initialize();
  269.         return $this->collection->offsetExists($offset);
  270.     }
  271.     /**
  272.      * {@inheritDoc}
  273.      *
  274.      * @param TKey $offset
  275.      *
  276.      * @return T|null
  277.      */
  278.     #[ReturnTypeWillChange]
  279.     public function offsetGet(mixed $offset)
  280.     {
  281.         $this->initialize();
  282.         return $this->collection->offsetGet($offset);
  283.     }
  284.     /**
  285.      * {@inheritDoc}
  286.      *
  287.      * @param TKey|null $offset
  288.      * @param T         $value
  289.      *
  290.      * @return void
  291.      */
  292.     #[ReturnTypeWillChange]
  293.     public function offsetSet(mixed $offsetmixed $value)
  294.     {
  295.         $this->initialize();
  296.         $this->collection->offsetSet($offset$value);
  297.     }
  298.     /**
  299.      * @param TKey $offset
  300.      *
  301.      * @return void
  302.      */
  303.     #[ReturnTypeWillChange]
  304.     public function offsetUnset(mixed $offset)
  305.     {
  306.         $this->initialize();
  307.         $this->collection->offsetUnset($offset);
  308.     }
  309.     /**
  310.      * Is the lazy collection already initialized?
  311.      *
  312.      * @return bool
  313.      *
  314.      * @psalm-assert-if-true Collection<TKey,T> $this->collection
  315.      */
  316.     public function isInitialized()
  317.     {
  318.         return $this->initialized;
  319.     }
  320.     /**
  321.      * Initialize the collection
  322.      *
  323.      * @return void
  324.      *
  325.      * @psalm-assert Collection<TKey,T> $this->collection
  326.      */
  327.     protected function initialize()
  328.     {
  329.         if ($this->initialized) {
  330.             return;
  331.         }
  332.         $this->doInitialize();
  333.         $this->initialized true;
  334.         if ($this->collection === null) {
  335.             throw new LogicException('You must initialize the collection property in the doInitialize() method.');
  336.         }
  337.     }
  338.     /**
  339.      * Do the initialization logic
  340.      *
  341.      * @return void
  342.      */
  343.     abstract protected function doInitialize();
  344. }