dev-master
9999999-dev
MIT
The Development Requires
0.1.x-dev
0.1.9999999.9999999-dev
MIT
The Development Requires
v0.1.1
0.1.1.0
MIT
The Development Requires
v0.1
0.1.0.0
MIT
The Development Requires
Wallogit.com
2017 © Pedro Peláez
Arrays are great, but sometimes the items need to know the thing they are a part of. Then you need a
Collection., (*1)
Developed by Florian Eckerstorfer (Twitter) in Vienna, Europe., (*3)
$ composer require cocur/collection
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., (*4)
Cocur\Collection\CollectionInterface defines the basic methods of a collection and extends
IteratorAggregate and Countable., (*5)
Cocur\Collection\CollectionInterface add(ItemInterface $item)
When calling add() the collection should call the setCollection() method of the item and set itself as its
collection., (*6)
Iterator getIterator()
Should return an iterator to iterate through the items of the collection., (*7)
int count()
Should return the number of items in the collection., (*8)
Cocur\Collection\ItemInterface defines two methods: setCollection() and getCollection()., (*9)
ItemInterface setCollection(CollectionInterface $collection)
Should store a reference to the collection in the item., (*10)
CollectionInterface|null getCollection()
Should return a reference to the collection or null if the item does not have a collection., (*11)
Cocur\Collection\Collection implements Cocur\Collection\CollectionInterface and uses a simple array to keep track
of its items., (*12)
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., (*13)
AbstractItem is, as its name suggests, an abstract class and you need to extend it., (*14)
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., (*15)
Item setValue(mixed $value) mixed getValue()
In addition Cocur\Collection\Item has a factory method called ::create():, (*16)
Item create(mixed $value = null)
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., (*17)
ArrayItem set(mixed $key, mixed $value) bool has(mixed $key) mixed get(mixed $key[, mixed $defaultValue]) ArrayItem remove(mixed $key() array toArray()
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., (*18)
You can create new instances using the static ::createFromArray() method:, (*19)
ArrayItem createFromArray(array $data = [])
Because ArrayItem implements ArrayAccess, IteratorAggregate and Countable you can use it in most cases just
like you would use an array., (*20)
$item = ArrayItem::createFromArray(['foo' => 'bar');
$item['qoo'] = 'qoz';
echo count($item); // -> 2
if (isset($item['qoo'])) {
echo $item['qoo']; // -> "qoz"
}
foreach ($item as $key => $value) {
echo "$key: $value, ";
}
// -> "foo: bar, qoo: qoz, "
ArrayItem::get()
The MIT license applies to cocur/collection. For the full copyright and license information, please view the LICENSE file distributed with this source code., (*21)
MIT
MIT
MIT
MIT