AbstractLazyCollection

class AbstractLazyCollection implements Collection

Lazy collection that is backed by a concrete collection

Properties

protected Collection$collectionThe backed collection to use
protected boolean$initialized

Methods

public count() {@inheritDoc}
public booleanadd(mixed $element) Adds an element at the end of the collection.
public voidclear() Clears the collection, removing all elements.
public booleancontains(mixed $element) Checks whether an element is contained in the collection.
public booleanisEmpty() Checks whether the collection is empty (contains no elements).
public mixedremove(string|integer $key) Removes the element at the specified index from the collection.
public booleanremoveElement(mixed $element) Removes the specified element from the collection, if it is found.
public booleancontainsKey(string|integer $key) Checks whether the collection contains an element with the specified key/index.
public mixedget(string|integer $key) Gets the element at the specified key/index.
public arraygetKeys() Gets all keys/indices of the collection.
public arraygetValues() Gets all values of the collection.
public voidset(string|integer $key, mixed $value) Sets an element in the collection at the specified key/index.
public arraytoArray() Gets a native PHP array representation of the collection.
public mixedfirst() Sets the internal iterator to the first element in the collection and returns this element.
public mixedlast() Sets the internal iterator to the last element in the collection and returns this element.
public int|stringkey() Gets the key/index of the element at the current iterator position.
public mixedcurrent() Gets the element of the collection at the current iterator position.
public mixednext() Moves the internal iterator position to the next element and returns this element.
public booleanexists(Closure $p) Tests for the existence of an element that satisfies the given predicate.
public Collectionfilter(Closure $p) Returns all the elements of this collection that satisfy the predicate p.
public booleanforAll(Closure $p) Tests whether the given predicate p holds for all elements of this collection.
public Collectionmap(Closure $func) Applies the given function to each element in the collection and returns a new collection with the elements returned by the function.
public arraypartition(Closure $p) Partitions this collection in two collections according to a predicate.
public int|string|boolindexOf(mixed $element) Gets the index/key of a given element. The comparison of two elements is strict, that means not only the value but also the type must match.
public arrayslice(int $offset, int|null $length = null) Extracts a slice of $length elements starting at position $offset from the Collection.
public getIterator() {@inheritDoc}
public offsetExists($offset) {@inheritDoc}
public offsetGet($offset) {@inheritDoc}
public offsetSet($offset, $value) {@inheritDoc}
public offsetUnset($offset) {@inheritDoc}
public boolisInitialized() Is the lazy collection already initialized?
protected voidinitialize() Initialize the collection
abstract protected voiddoInitialize() Do the initialization logic

Details

at line 47

count()

public count()

{@inheritDoc}

at line 56

add()

public boolean add(mixed $element)

Adds an element at the end of the collection.

Parameters

mixed$elementThe element to add.

Return Value

booleanAlways TRUE.
at line 65

clear()

public void clear()

Clears the collection, removing all elements.

Return Value

void
at line 74

contains()

public boolean contains(mixed $element)

Checks whether an element is contained in the collection.

This is an O(n) operation, where n is the size of the collection.

Parameters

mixed$elementThe element to search for.

Return Value

booleanTRUE if the collection contains the element, FALSE otherwise.
at line 83

isEmpty()

public boolean isEmpty()

Checks whether the collection is empty (contains no elements).

Return Value

booleanTRUE if the collection is empty, FALSE otherwise.
at line 92

remove()

public mixed remove(string|integer $key)

Removes the element at the specified index from the collection.

Parameters

string|integer$keyThe kex/index of the element to remove.

Return Value

mixedThe removed element or NULL, if the collection did not contain the element.
at line 101

removeElement()

public boolean removeElement(mixed $element)

Removes the specified element from the collection, if it is found.

Parameters

mixed$elementThe element to remove.

Return Value

booleanTRUE if this collection contained the specified element, FALSE otherwise.
at line 110

containsKey()

public boolean containsKey(string|integer $key)

Checks whether the collection contains an element with the specified key/index.

Parameters

string|integer$keyThe key/index to check for.

Return Value

booleanTRUE if the collection contains an element with the specified key/index, FALSE otherwise.
at line 119

get()

public mixed get(string|integer $key)

Gets the element at the specified key/index.

Parameters

string|integer$keyThe key/index of the element to retrieve.

Return Value

mixed
at line 128

getKeys()

public array getKeys()

Gets all keys/indices of the collection.

Return Value

arrayThe keys/indices of the collection, in the order of the corresponding elements in the collection.
at line 137

getValues()

public array getValues()

Gets all values of the collection.

Return Value

arrayThe values of all elements in the collection, in the order they appear in the collection.
at line 146

set()

public void set(string|integer $key, mixed $value)

Sets an element in the collection at the specified key/index.

Parameters

string|integer$keyThe key/index of the element to set.
mixed$valueThe element to set.

Return Value

void
at line 155

toArray()

public array toArray()

Gets a native PHP array representation of the collection.

Return Value

array
at line 164

first()

public mixed first()

Sets the internal iterator to the first element in the collection and returns this element.

Return Value

mixed
at line 173

last()

public mixed last()

Sets the internal iterator to the last element in the collection and returns this element.

Return Value

mixed
at line 182

key()

public int|string key()

Gets the key/index of the element at the current iterator position.

Return Value

int|string
at line 191

current()

public mixed current()

Gets the element of the collection at the current iterator position.

Return Value

mixed
at line 200

next()

public mixed next()

Moves the internal iterator position to the next element and returns this element.

Return Value

mixed
at line 209

exists()

public boolean exists(Closure $p)

Tests for the existence of an element that satisfies the given predicate.

Parameters

Closure$pThe predicate.

Return Value

booleanTRUE if the predicate is TRUE for at least one element, FALSE otherwise.
at line 218

filter()

public Collection filter(Closure $p)

Returns all the elements of this collection that satisfy the predicate p.

The order of the elements is preserved.

Parameters

Closure$pThe predicate used for filtering.

Return Value

CollectionA collection with the results of the filter operation.
at line 227

forAll()

public boolean forAll(Closure $p)

Tests whether the given predicate p holds for all elements of this collection.

Parameters

Closure$pThe predicate.

Return Value

booleanTRUE, if the predicate yields TRUE for all elements, FALSE otherwise.
at line 236

map()

public Collection map(Closure $func)

Applies the given function to each element in the collection and returns a new collection with the elements returned by the function.

Parameters

Closure$func

Return Value

Collection
at line 245

partition()

public array partition(Closure $p)

Partitions this collection in two collections according to a predicate.

Keys are preserved in the resulting collections.

Parameters

Closure$pThe predicate on which to partition.

Return Value

arrayAn array with two elements. The first element contains the collection of elements where the predicate returned TRUE, the second element contains the collection of elements where the predicate returned FALSE.
at line 254

indexOf()

public int|string|bool indexOf(mixed $element)

Gets the index/key of a given element. The comparison of two elements is strict, that means not only the value but also the type must match.

For objects this means reference equality.

Parameters

mixed$elementThe element to search for.

Return Value

int|string|boolThe key/index of the element or FALSE if the element was not found.
at line 263

slice()

public array slice(int $offset, int|null $length = null)

Extracts a slice of $length elements starting at position $offset from the Collection.

If $length is null it returns all elements from $offset to the end of the Collection. Keys have to be preserved by this method. Calling this method will only return the selected slice and NOT change the elements contained in the collection slice is called on.

Parameters

int$offsetThe offset to start from.
int|null$lengthThe maximum number of elements to return, or null for no limit.

Return Value

array
at line 272

getIterator()

public getIterator()

{@inheritDoc}

at line 281

offsetExists()

public offsetExists($offset)

{@inheritDoc}

Parameters

$offset
at line 290

offsetGet()

public offsetGet($offset)

{@inheritDoc}

Parameters

$offset
at line 299

offsetSet()

public offsetSet($offset, $value)

{@inheritDoc}

Parameters

$offset
$value
at line 308

offsetUnset()

public offsetUnset($offset)

{@inheritDoc}

Parameters

$offset
at line 319

isInitialized()

public bool isInitialized()

Is the lazy collection already initialized?

Return Value

bool
at line 329

initialize()

protected void initialize()

Initialize the collection

Return Value

void
at line 342

doInitialize()

abstract protected void doInitialize()

Do the initialization logic

Return Value

void

Source code

<?php
/*
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * This software consists of voluntary contributions made by many individuals
 * and is licensed under the MIT license. For more information, see
 * <http://www.doctrine-project.org>.
 */

namespace Doctrine\Common\Collections;

use Closure;

/**
 * Lazy collection that is backed by a concrete collection
 *
 * @author Michaƫl Gallego <mic.gallego@gmail.com>
 * @since  1.2
 */
abstract class AbstractLazyCollection implements Collection
{
    /**
     * The backed collection to use
     *
     * @var Collection
     */
    protected $collection;

    /**
     * @var boolean
     */
    protected $initialized = false;

    /**
     * {@inheritDoc}
     */
    public function count()
    {
        $this->initialize();
        return $this->collection->count();
    }

    /**
     * {@inheritDoc}
     */
    public function add($element)
    {
        $this->initialize();
        return $this->collection->add($element);
    }

    /**
     * {@inheritDoc}
     */
    public function clear()
    {
        $this->initialize();
        $this->collection->clear();
    }

    /**
     * {@inheritDoc}
     */
    public function contains($element)
    {
        $this->initialize();
        return $this->collection->contains($element);
    }

    /**
     * {@inheritDoc}
     */
    public function isEmpty()
    {
        $this->initialize();
        return $this->collection->isEmpty();
    }

    /**
     * {@inheritDoc}
     */
    public function remove($key)
    {
        $this->initialize();
        return $this->collection->remove($key);
    }

    /**
     * {@inheritDoc}
     */
    public function removeElement($element)
    {
        $this->initialize();
        return $this->collection->removeElement($element);
    }

    /**
     * {@inheritDoc}
     */
    public function containsKey($key)
    {
        $this->initialize();
        return $this->collection->containsKey($key);
    }

    /**
     * {@inheritDoc}
     */
    public function get($key)
    {
        $this->initialize();
        return $this->collection->get($key);
    }

    /**
     * {@inheritDoc}
     */
    public function getKeys()
    {
        $this->initialize();
        return $this->collection->getKeys();
    }

    /**
     * {@inheritDoc}
     */
    public function getValues()
    {
        $this->initialize();
        return $this->collection->getValues();
    }

    /**
     * {@inheritDoc}
     */
    public function set($key, $value)
    {
        $this->initialize();
        $this->collection->set($key, $value);
    }

    /**
     * {@inheritDoc}
     */
    public function toArray()
    {
        $this->initialize();
        return $this->collection->toArray();
    }

    /**
     * {@inheritDoc}
     */
    public function first()
    {
        $this->initialize();
        return $this->collection->first();
    }

    /**
     * {@inheritDoc}
     */
    public function last()
    {
        $this->initialize();
        return $this->collection->last();
    }

    /**
     * {@inheritDoc}
     */
    public function key()
    {
        $this->initialize();
        return $this->collection->key();
    }

    /**
     * {@inheritDoc}
     */
    public function current()
    {
        $this->initialize();
        return $this->collection->current();
    }

    /**
     * {@inheritDoc}
     */
    public function next()
    {
        $this->initialize();
        return $this->collection->next();
    }

    /**
     * {@inheritDoc}
     */
    public function exists(Closure $p)
    {
        $this->initialize();
        return $this->collection->exists($p);
    }

    /**
     * {@inheritDoc}
     */
    public function filter(Closure $p)
    {
        $this->initialize();
        return $this->collection->filter($p);
    }

    /**
     * {@inheritDoc}
     */
    public function forAll(Closure $p)
    {
        $this->initialize();
        return $this->collection->forAll($p);
    }

    /**
     * {@inheritDoc}
     */
    public function map(Closure $func)
    {
        $this->initialize();
        return $this->collection->map($func);
    }

    /**
     * {@inheritDoc}
     */
    public function partition(Closure $p)
    {
        $this->initialize();
        return $this->collection->partition($p);
    }

    /**
     * {@inheritDoc}
     */
    public function indexOf($element)
    {
        $this->initialize();
        return $this->collection->indexOf($element);
    }

    /**
     * {@inheritDoc}
     */
    public function slice($offset, $length = null)
    {
        $this->initialize();
        return $this->collection->slice($offset, $length);
    }

    /**
     * {@inheritDoc}
     */
    public function getIterator()
    {
        $this->initialize();
        return $this->collection->getIterator();
    }

    /**
     * {@inheritDoc}
     */
    public function offsetExists($offset)
    {
        $this->initialize();
        return $this->collection->offsetExists($offset);
    }

    /**
     * {@inheritDoc}
     */
    public function offsetGet($offset)
    {
        $this->initialize();
        return $this->collection->offsetGet($offset);
    }

    /**
     * {@inheritDoc}
     */
    public function offsetSet($offset, $value)
    {
        $this->initialize();
        $this->collection->offsetSet($offset, $value);
    }

    /**
     * {@inheritDoc}
     */
    public function offsetUnset($offset)
    {
        $this->initialize();
        $this->collection->offsetUnset($offset);
    }

    /**
     * Is the lazy collection already initialized?
     *
     * @return bool
     */
    public function isInitialized()
    {
        return $this->initialized;
    }

    /**
     * Initialize the collection
     *
     * @return void
     */
    protected function initialize()
    {
        if ( ! $this->initialized) {
            $this->doInitialize();
            $this->initialized = true;
        }
    }

    /**
     * Do the initialization logic
     *
     * @return void
     */
    abstract protected function doInitialize();
}