UnitList

class UnitList extends ArrayCollection

UnitList is a list of Unit objects.

Right now this is because of blindly following object calisthenics, but it might just be reverted to a simple array if it doesn't get any love.

Methods

public __construct(array $elements = array()) Initializes a new ArrayCollection.
public arraytoArray() Gets a native PHP array representation of the collection. from ArrayCollection
public mixedfirst() Sets the internal iterator to the first element in the collection and returns this element. from ArrayCollection
public mixedlast() Sets the internal iterator to the last element in the collection and returns this element. from ArrayCollection
public int|stringkey() Gets the key/index of the element at the current iterator position. from ArrayCollection
public mixednext() Moves the internal iterator position to the next element and returns this element. from ArrayCollection
public mixedcurrent() Gets the element of the collection at the current iterator position. from ArrayCollection
public mixedremove(string|integer $key) Removes the element at the specified index from the collection. from ArrayCollection
public booleanremoveElement(mixed $element) Removes the specified element from the collection, if it is found. from ArrayCollection
public offsetExists($offset) Required by interface ArrayAccess. from ArrayCollection
Unit|nulloffsetGet($offset) No description
public offsetSet($offset, $value) Required by interface ArrayAccess. from ArrayCollection
public offsetUnset($offset) Required by interface ArrayAccess. from ArrayCollection
public booleancontainsKey(string|integer $key) Checks whether the collection contains an element with the specified key/index. from ArrayCollection
public booleancontains(mixed $element) Checks whether an element is contained in the collection. from ArrayCollection
public booleanexists(Closure $p) Tests for the existence of an element that satisfies the given predicate. from ArrayCollection
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. from ArrayCollection
Unit|nullget($key) No description
public arraygetKeys() Gets all keys/indices of the collection. from ArrayCollection
public arraygetValues() Gets all values of the collection. from ArrayCollection
public count() {@inheritDoc} from ArrayCollection
public voidset(string|integer $key, mixed $value) Key will be ignored and the unit's name will be used instead.
public booleanadd($value) No description
public booleanisEmpty() Checks whether the collection is empty (contains no elements). from ArrayCollection
public getIterator() Required by interface IteratorAggregate. from ArrayCollection
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. from ArrayCollection
public Collectionfilter(Closure $p) Returns all the elements of this collection that satisfy the predicate p. from ArrayCollection
public booleanforAll(Closure $p) Tests whether the given predicate p holds for all elements of this collection. from ArrayCollection
public arraypartition(Closure $p) Partitions this collection in two collections according to a predicate. from ArrayCollection
public string__toString() Returns a string representation of this object. from ArrayCollection
public voidclear() Clears the collection, removing all elements. from ArrayCollection
public arrayslice(int $offset, int|null $length = null) Extracts a slice of $length elements starting at position $offset from the Collection. from ArrayCollection
public Collectionmatching(Criteria $criteria) Selects all elements from a selectable that match the expression and returns a new collection containing these elements. from ArrayCollection

Details

at line 34

__construct()

public __construct(array $elements = array())

Initializes a new ArrayCollection.

Parameters

array$elements
in ArrayCollection at line 56

toArray()

public array toArray()

Gets a native PHP array representation of the collection.

Return Value

array
in ArrayCollection at line 64

first()

public mixed first()

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

Return Value

mixed
in ArrayCollection at line 72

last()

public mixed last()

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

Return Value

mixed
in ArrayCollection at line 80

key()

public int|string key()

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

Return Value

int|string
in ArrayCollection at line 88

next()

public mixed next()

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

Return Value

mixed
in ArrayCollection at line 96

current()

public mixed current()

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

Return Value

mixed
in ArrayCollection at line 104

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.
in ArrayCollection at line 119

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.
in ArrayCollection at line 137

offsetExists()

public offsetExists($offset)

Required by interface ArrayAccess.

{@inheritDoc}

Parameters

$offset
at line 27

offsetGet()

Unit|null offsetGet($offset)

Parameters

$offset

Return Value

Unit|null
in ArrayCollection at line 157

offsetSet()

public offsetSet($offset, $value)

Required by interface ArrayAccess.

{@inheritDoc}

Parameters

$offset
$value
in ArrayCollection at line 171

offsetUnset()

public offsetUnset($offset)

Required by interface ArrayAccess.

{@inheritDoc}

Parameters

$offset
in ArrayCollection at line 179

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.
in ArrayCollection at line 187

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.
in ArrayCollection at line 195

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.
in ArrayCollection at line 209

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 27

get()

Unit|null get($key)

Parameters

$key

Return Value

Unit|null
in ArrayCollection at line 225

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.
in ArrayCollection at line 233

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.
in ArrayCollection at line 241

count()

public count()

{@inheritDoc}

at line 62

set()

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

Key will be ignored and the unit's name will be used instead.

Parameters

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

Return Value

void
at line 47

add()

public boolean add($value)

Parameters

$value

Return Value

booleanAlways TRUE.
in ArrayCollection at line 267

isEmpty()

public boolean isEmpty()

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

Return Value

booleanTRUE if the collection is empty, FALSE otherwise.
in ArrayCollection at line 277

getIterator()

public getIterator()

Required by interface IteratorAggregate.

{@inheritDoc}

in ArrayCollection at line 285

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
in ArrayCollection at line 293

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.
in ArrayCollection at line 301

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.
in ArrayCollection at line 315

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.
in ArrayCollection at line 335

__toString()

public string __toString()

Returns a string representation of this object.

Return Value

string
in ArrayCollection at line 343

clear()

public void clear()

Clears the collection, removing all elements.

Return Value

void
in ArrayCollection at line 351

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
in ArrayCollection at line 359

matching()

public Collection matching(Criteria $criteria)

Selects all elements from a selectable that match the expression and returns a new collection containing these elements.

Parameters

Criteria$criteria

Return Value

Collection

Source code

<?php

/*
 * This file is part of nochso/benchmark.
 *
 * @copyright Copyright (c) 2015 Marcel Voigt <mv@noch.so>
 * @license   https://github.com/nochso/benchmark/blob/master/LICENSE ISC
 * @link      https://github.com/nochso/benchmark
 */

namespace nochso\Benchmark;

use Doctrine\Common\Collections\ArrayCollection;

/**
 * UnitList is a list of Unit objects.
 *
 * Right now this is because of blindly following object calisthenics, but it might just be
 * reverted to a simple array if it doesn't get any love.
 *
 * @method Unit|null offsetGet($offset)
 * @method Unit|null get($key)
 *
 * @author Marcel Voigt <mv@noch.so>
 * @copyright Copyright (c) 2015 Marcel Voigt <mv@noch.so>
 */
class UnitList extends ArrayCollection
{
    /**
     * Initializes a new ArrayCollection.
     *
     * @param array $elements
     */
    public function __construct(array $elements = array())
    {
        parent::__construct(array());
        foreach ($elements as $unit) {
            $this->add($unit);
        }
    }

    /**
     * @param Unit $value
     *
     * @return bool
     */
    public function add($value)
    {
        if (!$value instanceof Unit) {
            throw new \InvalidArgumentException('Expected object of type ' . get_class(new Unit('')));
        }
        parent::set($value->getName(), $value);
        return true;
    }

    /**
     * Key will be ignored and the unit's name will be used instead.
     *
     * @param int|string $key
     * @param Unit       $value
     */
    public function set($key, $value)
    {
        $this->add($value);
    }
}