Download the PHP package cocur/collection without Composer
On this page you can find all versions of the php package cocur/collection. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package collection
Cocur\Collection
Arrays are great, but sometimes the items need to know the thing they are a part of. Then you need a
Collection
.
Developed by Florian Eckerstorfer (Twitter) in Vienna, Europe.
Installation
Usage
The most important part of Collection are it's interfaces. They make your collection interoperable with other libraries. However, this library also contains implementations that are ready to use.
Interfaces
CollectionInterface
ItemInterface
Implementations
Collection
AbstractItem
Item
ArrayItem
CollectionInterface
Cocur\Collection\CollectionInterface
defines the basic methods of a collection and extends
IteratorAggregate
and Countable
.
When calling add()
the collection should call the setCollection()
method of the item and set itself as its
collection.
Should return an iterator to iterate through the items of the collection.
Should return the number of items in the collection.
ItemInterface
Cocur\Collection\ItemInterface
defines two methods: setCollection()
and getCollection()
.
Should store a reference to the collection in the item.
Should return a reference to the collection or null
if the item does not have a collection.
Collection
Cocur\Collection\Collection
implements Cocur\Collection\CollectionInterface
and uses a simple array to keep track
of its items.
AbstractItem
Cocur\Collection\AbstractItem
implements the methods from Cocur\Collection\ItemInterface
. It simply sets and gets
the collection and is great if you don't need a specific logic on how to set and get the collection on an item.
AbstractItem
is, as its name suggests, an abstract class and you need to extend it.
Item
Cocur\Collection\Item
extends Cocur\Collection\AbstractItem
and in addition to the setCollection()
and
getCollection()
methods also implements setValue()
and getValue()
to set and get an arbitrary value. You can wrap
Item
around a scalar value or around an object that is out of your control and does not implement ItemInterface
.
In addition Cocur\Collection\Item
has a factory method called ::create()
:
ArrayItem
While Item
stores a single value, Cocur\Collection\ArrayItem
is meant to hold an array. It implements methods to
set, get and remove elements from the item as well as to check for their existence. In addition the toArray()
method
returns the underlying array.
The get()
and remove()
methods throw an OutOfBoundsException
if the element with the given key does not exist.
However, instead of throwing an exception get()
can also return a default value if one is provided as second
argument.
You can create new instances using the static ::createFromArray()
method:
Because ArrayItem
implements ArrayAccess
, IteratorAggregate
and Countable
you can use it in most cases just
like you would use an array.
Change Log
Version 0.1.1 (18 May 2015)
- Add option default value to
ArrayItem::get()
Version 0.1 (28 April 2015)
- Initial release
License
The MIT license applies to cocur/collection. For the full copyright and license information, please view the LICENSE file distributed with this source code.