vendor/doctrine/collections/lib/Doctrine/Common/Collections/AbstractLazyCollection.php line 250

Open in your IDE?
  1. <?php
  2. namespace Doctrine\Common\Collections;
  3. use Closure;
  4. use LogicException;
  5. use ReturnTypeWillChange;
  6. use Traversable;
  7. /**
  8.  * Lazy collection that is backed by a concrete collection
  9.  *
  10.  * @psalm-template TKey of array-key
  11.  * @psalm-template T
  12.  * @template-implements Collection<TKey,T>
  13.  */
  14. abstract class AbstractLazyCollection implements Collection
  15. {
  16.     /**
  17.      * The backed collection to use
  18.      *
  19.      * @psalm-var Collection<TKey,T>|null
  20.      * @var Collection<mixed>|null
  21.      */
  22.     protected $collection;
  23.     /** @var bool */
  24.     protected $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($element)
  40.     {
  41.         $this->initialize();
  42.         return $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($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($key)
  74.     {
  75.         $this->initialize();
  76.         return $this->collection->remove($key);
  77.     }
  78.     /**
  79.      * {@inheritDoc}
  80.      */
  81.     public function removeElement($element)
  82.     {
  83.         $this->initialize();
  84.         return $this->collection->removeElement($element);
  85.     }
  86.     /**
  87.      * {@inheritDoc}
  88.      */
  89.     public function containsKey($key)
  90.     {
  91.         $this->initialize();
  92.         return $this->collection->containsKey($key);
  93.     }
  94.     /**
  95.      * {@inheritDoc}
  96.      */
  97.     public function get($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($key$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 filter(Closure $p)
  186.     {
  187.         $this->initialize();
  188.         return $this->collection->filter($p);
  189.     }
  190.     /**
  191.      * {@inheritDoc}
  192.      */
  193.     public function forAll(Closure $p)
  194.     {
  195.         $this->initialize();
  196.         return $this->collection->forAll($p);
  197.     }
  198.     /**
  199.      * {@inheritDoc}
  200.      */
  201.     public function map(Closure $func)
  202.     {
  203.         $this->initialize();
  204.         return $this->collection->map($func);
  205.     }
  206.     /**
  207.      * {@inheritDoc}
  208.      */
  209.     public function partition(Closure $p)
  210.     {
  211.         $this->initialize();
  212.         return $this->collection->partition($p);
  213.     }
  214.     /**
  215.      * {@inheritDoc}
  216.      *
  217.      * @template TMaybeContained
  218.      */
  219.     public function indexOf($element)
  220.     {
  221.         $this->initialize();
  222.         return $this->collection->indexOf($element);
  223.     }
  224.     /**
  225.      * {@inheritDoc}
  226.      */
  227.     public function slice($offset$length null)
  228.     {
  229.         $this->initialize();
  230.         return $this->collection->slice($offset$length);
  231.     }
  232.     /**
  233.      * {@inheritDoc}
  234.      *
  235.      * @return Traversable<int|string, mixed>
  236.      * @psalm-return Traversable<TKey,T>
  237.      */
  238.     #[ReturnTypeWillChange]
  239.     public function getIterator()
  240.     {
  241.         $this->initialize();
  242.         return $this->collection->getIterator();
  243.     }
  244.     /**
  245.      * @param TKey $offset
  246.      *
  247.      * @return bool
  248.      */
  249.     #[ReturnTypeWillChange]
  250.     public function offsetExists($offset)
  251.     {
  252.         $this->initialize();
  253.         return $this->collection->offsetExists($offset);
  254.     }
  255.     /**
  256.      * @param TKey $offset
  257.      *
  258.      * @return mixed
  259.      */
  260.     #[ReturnTypeWillChange]
  261.     public function offsetGet($offset)
  262.     {
  263.         $this->initialize();
  264.         return $this->collection->offsetGet($offset);
  265.     }
  266.     /**
  267.      * @param TKey|null $offset
  268.      * @param T         $value
  269.      *
  270.      * @return void
  271.      */
  272.     #[ReturnTypeWillChange]
  273.     public function offsetSet($offset$value)
  274.     {
  275.         $this->initialize();
  276.         $this->collection->offsetSet($offset$value);
  277.     }
  278.     /**
  279.      * @param TKey $offset
  280.      *
  281.      * @return void
  282.      */
  283.     #[ReturnTypeWillChange]
  284.     public function offsetUnset($offset)
  285.     {
  286.         $this->initialize();
  287.         $this->collection->offsetUnset($offset);
  288.     }
  289.     /**
  290.      * Is the lazy collection already initialized?
  291.      *
  292.      * @return bool
  293.      *
  294.      * @psalm-assert-if-true Collection<TKey,T> $this->collection
  295.      */
  296.     public function isInitialized()
  297.     {
  298.         return $this->initialized;
  299.     }
  300.     /**
  301.      * Initialize the collection
  302.      *
  303.      * @return void
  304.      *
  305.      * @psalm-assert Collection<TKey,T> $this->collection
  306.      */
  307.     protected function initialize()
  308.     {
  309.         if ($this->initialized) {
  310.             return;
  311.         }
  312.         $this->doInitialize();
  313.         $this->initialized true;
  314.         if ($this->collection === null) {
  315.             throw new LogicException('You must initialize the collection property in the doInitialize() method.');
  316.         }
  317.     }
  318.     /**
  319.      * Do the initialization logic
  320.      *
  321.      * @return void
  322.      */
  323.     abstract protected function doInitialize();
  324. }