Collection
class Collection extends AbstractCollection (View source)
A collection represents a group of objects. Each object in the collection is of a specific, defined type.
This is a direct implementation of CollectionInterface, provided for the sake of convenience.
Example usage:
$collection = new \Ramsey\Collection\Collection('My\\Foo');
$collection->add(new \My\Foo());
$collection->add(new \My\Foo());
foreach ($collection as $foo) {
// Do something with $foo
}
It is preferrable to subclass AbstractCollection to create your own typed collections. For example:
namespace My\Foo;
class FooCollection extends \Ramsey\Collection\AbstractCollection
{
public function __construct()
{
parent::__construct('My\\Foo');
}
}
And then use it similarly to the earlier example:
$fooCollection = new \My\Foo\FooCollection();
$fooCollection->add(new \My\Foo());
$fooCollection->add(new \My\Foo());
foreach ($fooCollection as $foo) {
// Do something with $foo
}
The benefit with this approach is that you may do type-checking on the collection object:
if ($collection instancof \My\Foo\FooCollection) {
// the collection is a collection of My\Foo objects
}
Traits
Properties
protected array | $data | from AbstractArray |
Methods
Constructs a collection object of the specified type
Checks whether the specified offset exists in the array
Returns the value stored at the specified offset in the array
Sets the specified offset in the map with the given value
Removes the specified offset and its value from the map
Converts this map object to a string when the object is serialized
with serialize()
Re-constructs the object from its serialized form
Returns a native PHP array containing all of the elements in this array object
Returns true
if value is of the specified type
Ensures that this collection contains the specified element (optional operation). Returns true if this collection changed as a result of the call. (Returns false if this collection does not permit duplicates and already contains the specified element.)
Returns true
if this collection contains the specified element
Removes a single instance of the specified element from this collection, if it is present
Details
in AbstractCollection at line 44
__construct(string $collectionType)
Constructs a collection object of the specified type
in AbstractArray at line 48
ArrayIterator
getIterator()
Returns a new iterator from this array
in AbstractArray at line 60
bool
offsetExists(mixed $offset)
Checks whether the specified offset exists in the array
in AbstractArray at line 72
mixed
offsetGet(mixed $offset)
Returns the value stored at the specified offset in the array
in AbstractCollection at line 66
offsetSet(mixed $offset, mixed $value)
Sets the specified offset in the map with the given value
in AbstractArray at line 100
offsetUnset(mixed $offset)
Removes the specified offset and its value from the map
in AbstractArray at line 112
string
serialize()
Converts this map object to a string when the object is serialized
with serialize()
in AbstractArray at line 123
unserialize(string $serialized)
Re-constructs the object from its serialized form
in AbstractArray at line 134
int
count()
Returns the number of elements contained in this array
in AbstractArray at line 139
void
clear()
Remove all the elements from this array object
in AbstractArray at line 144
toArray()
Returns a native PHP array containing all of the elements in this array object
in AbstractArray at line 149
bool
isEmpty()
Returns true
if this array object contains no elements
protected bool
checkType(string $type, mixed $value)
Returns true
if value is of the specified type
in AbstractCollection at line 49
bool
add(mixed $element)
Ensures that this collection contains the specified element (optional operation). Returns true if this collection changed as a result of the call. (Returns false if this collection does not permit duplicates and already contains the specified element.)
Collections that support this operation may place limitations on what elements may be added to this collection. In particular, some collections will refuse to add null elements, and others will impose restrictions on the type of elements that may be added. Collection classes should clearly specify in their documentation any restrictions on what elements may be added.
If a collection refuses to add a particular element for any reason other than that it already contains the element, it must throw an exception (rather than returning false). This preserves the invariant that a collection always contains the specified element after this call returns.
in AbstractCollection at line 56
bool
contains(mixed $element)
Returns true
if this collection contains the specified element
This performs a strict type check on the value.
in AbstractCollection at line 61
string
getType()
Returns the type associated with this collection
in AbstractCollection at line 78
bool
remove(mixed $element)
Removes a single instance of the specified element from this collection, if it is present