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

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