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

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