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 preferable to subclass AbstractCollection
to create your own typed
collections. For example:
namespace My\Foo;
class FooCollection extends \Ramsey\Collection\AbstractCollection
{
public function getType()
{
return '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 instanceof \My\Foo\FooCollection) {
// the collection is a collection of My\Foo objects
}
Traits
Properties
protected array | $data | The items of this array. | from AbstractArray |
Methods
Constructs a collection object of the specified type, optionally with the specified data.
Returns true
if the given offset exists in this array.
Sets the given value to the given offset in the array.
Removes the given offset and its value from the array.
Returns a serialized string representation of this array object.
Converts a serialized string representation into an instance object.
Returns true
if value is of the specified type.
Returns a string representation of the value.
Extracts the value of the given property or method from the object.
Ensures that this collection 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.
Returns the values from given property or method.
Returns a sorted collection.
Returns a filtered collection.
Returns a collection of matching items.
Applies a callback to each item of the collection.
Create a new collection with divergent items between current and given collection.
Create a new collection with intersecting item between current and given collection.
Merge current items and items of given collections into a new one.
Returns the type associated with this collection.
Details
at line 93
__construct(string $collectionType, array $data = [])
Constructs a collection object of the specified type, optionally with the specified data.
in AbstractArray at line 51
Traversable
getIterator()
Returns an iterator for this array.
in AbstractArray at line 65
bool
offsetExists(mixed $offset)
Returns true
if the given offset exists in this array.
in AbstractArray at line 80
mixed|null
offsetGet(mixed $offset)
Returns the value at the specified offset.
in AbstractCollection at line 75
void
offsetSet(mixed|null $offset, mixed $value)
Sets the given value to the given offset in the array.
in AbstractArray at line 110
void
offsetUnset(mixed $offset)
Removes the given offset and its value from the array.
in AbstractArray at line 122
string
serialize()
Returns a serialized string representation of this array object.
in AbstractCollection at line 336
void
unserialize(string $serialized)
Converts a serialized string representation into an instance object.
in AbstractArray at line 146
int
count()
Returns the number of items in this array.
in AbstractArray at line 154
void
clear()
Removes all items from this array.
in AbstractArray at line 164
array
toArray()
Returns a native PHP array representation of this array object.
in AbstractArray at line 174
bool
isEmpty()
Returns true
if this array is empty.
protected bool
checkType(string $type, mixed $value)
Returns true
if value is of the specified type.
in ValueToStringTrait at line 38
protected string
toolValueToString(mixed $value)
Returns a string representation of the value.
- null value:
'NULL'
- boolean:
'TRUE'
,'FALSE'
- array:
'Array'
- scalar: converted-value
- resource:
'(type resource #number)'
- object with
__toString()
: result of__toString()
- object DateTime: ISO 8601 date
- object:
'(className Object)'
- anonymous function: same as object
in ValueExtractorTrait at line 35
protected mixed
extractValue(object $object, string $propertyOrMethod)
Extracts the value of the given property or method from the object.
in AbstractCollection at line 46
bool
add(mixed $element)
Ensures that this collection contains the specified element.
in AbstractCollection at line 61
bool
contains(mixed $element, bool $strict = true)
Returns true
if this collection contains the specified element.
in AbstractCollection at line 95
bool
remove(mixed $element)
Removes a single instance of the specified element from this collection, if it is present.
in AbstractCollection at line 115
array
column(string $propertyOrMethod)
Returns the values from given property or method.
in AbstractCollection at line 133
mixed
first()
Returns the first item of the collection.
in AbstractCollection at line 151
mixed
last()
Returns the last item of the collection.
in AbstractCollection at line 178
CollectionInterface
sort(string $propertyOrMethod, string $order = self::SORT_ASC)
Returns a sorted collection.
{@inheritdoc}
in AbstractCollection at line 205
CollectionInterface
filter(callable $callback)
Returns a filtered collection.
{@inheritdoc}
in AbstractCollection at line 225
CollectionInterface
where(string $propertyOrMethod, mixed $value)
Returns a collection of matching items.
{@inheritdoc}
in AbstractCollection at line 244
CollectionInterface
map(callable $callback)
Applies a callback to each item of the collection.
{@inheritdoc}
in AbstractCollection at line 264
CollectionInterface
diff(CollectionInterface $other)
Create a new collection with divergent items between current and given collection.
in AbstractCollection at line 292
CollectionInterface
intersect(CollectionInterface $other)
Create a new collection with intersecting item between current and given collection.
in AbstractCollection at line 314
CollectionInterface
merge(CollectionInterface ...$collections)
Merge current items and items of given collections into a new one.
at line 104
string
getType()
Returns the type associated with this collection.