Download the PHP package yoshi2889/collections without Composer
On this page you can find all versions of the php package yoshi2889/collections. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download yoshi2889/collections
More information about yoshi2889/collections
Files in yoshi2889/collections
Package collections
Short Description Simple Collection class allowing storage of specific data.
License MIT
Informations about the package collections
Collections
Simple Collection class allowing storage of specific data, based on PHP's ArrayObject.
Installation
You can install this class via composer
:
Usage
To use a Collection, create a new instance:
Note that the closure passed as the first parameter to the Collection
constructur MUST return a boolean value.
This function is used to validate any added data types. A returned value of true means the given value may be added to the collection.
The second parameter can contain any initial values which should be in the collection. These items will also be validated.
Validating
Data is validated using a Closure
instance, or a callback. The given closure must support exactly 1 untyped(!) parameter and
return a boolean value. Values for which the closure returns false
will be denied with an \InvalidArgumentException
.
Please note that only the values are validated, while keys are not.
Validation can be manually performed on values using the ->validateType($value)
and ->validateArray(array $array)
methods, which
check a single value and an array of values respectively.
->validateType($value)
will return boolean true
if the value passes validation, and false
if it does not.
->validateArray(array $array)
will return boolean true
if all the values pass validation, and false
if one or more do not.
Manipulation
A Collection
instance may be treated like a generic array. This means you can manipulate it like the following example:
Please note that any data entered this way will still be validated and may still throw an \InvalidArgumentException
.
Furthermore, to remove data from the Collection you may use the ->offsetUnset($index)
and ->removeAll($value)
methods.
->offsetUnset($index)
will remove the value located at key $index
, which works similarly to using unset($collection['key'])
.
->removeAll($value)
will remove every value which is equal to $value
from the collection. Because values may exist multiple times
in the same Collection, it is not possible to reliably remove only a single value from the collection using a value; consider removing by key instead.
If you wish to preserve the original values while creating a new instance without certain values, please refer to Filtering below.
Filtering
A Collection can be filtered using the ->filter(\Closure $condition)
method. The given closure MUST return a boolean value.
The condition closure is called for every element in the collection. Any elements for which the closure returns true
will be kept
and put in a new Collection
instance, which will be returned. For example, to filter out all elements which are NOT foo
:
Events
A Collection will emit a changed
event if data gets added to or removed from it. No arguments are passed.
This functionality is borrowed from Événement by igorw.
You can subscribe to the event using the ->on($event, callable $listener)
method, like so:
Validation Closures
You can find a lot of closures and utilities which can be used by a Collection in the separate validation-closures project.
License
This code is released under the MIT License. Please see LICENSE
to read it.