PHPackages                             cocur/collection - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. cocur/collection

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

cocur/collection
================

v0.1.1(11y ago)41011MITPHP

Since Apr 28Pushed 10y ago2 watchersCompare

[ Source](https://github.com/cocur/collection)[ Packagist](https://packagist.org/packages/cocur/collection)[ RSS](/packages/cocur-collection/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (2)Dependencies (2)Versions (4)Used By (1)

Cocur\\Collection
=================

[](#cocurcollection)

> Arrays are great, but sometimes the items need to know the thing they are a part of. Then you need a `Collection`.

[![Build Status](https://camo.githubusercontent.com/974af315374365b55f4400b4428596f38026d80c0d74f314686ce90f9bd5c9b1/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f636f6375722f636f6c6c656374696f6e2f6d61737465722e7376673f7374796c653d666c6174)](https://travis-ci.org/cocur/collection)[![Windows Build status](https://camo.githubusercontent.com/5f1e9064ad81d744fa7c3d0e2ef37376358eeafc54d8e85536ed5e4e7839b2fd/68747470733a2f2f63692e6170707665796f722e636f6d2f6170692f70726f6a656374732f7374617475732f38766e61617876336b61626b6234326a3f7376673d74727565)](https://ci.appveyor.com/project/florianeckerstorfer/collection)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/264880326ae9d213d9bb310e229565574d20ebb7bca96d0e8acb160cdc8b1f9c/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f672f636f6375722f636f6c6c656374696f6e2e7376673f7374796c653d666c6174)](https://scrutinizer-ci.com/g/cocur/collection/?branch=master)[![Code Coverage](https://camo.githubusercontent.com/dedf0904456e8b886eaf242bbca8d4e050776892faee9b24d6fa0258c8d7602f/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f636f7665726167652f672f636f6375722f636f6c6c656374696f6e2e7376673f7374796c653d666c6174)](https://scrutinizer-ci.com/g/cocur/collection/?branch=master)[![StyleCI](https://camo.githubusercontent.com/d73df22674dd7b35e78a35c6ed5e01d8ac7e7843c70630540babbf65d0304a35/68747470733a2f2f7374796c6563692e696f2f7265706f732f33323838333937382f736869656c64)](https://styleci.io/repos/32883978)

Developed by [Florian Eckerstorfer](https://florian.ec) ([Twitter](https://twitter.com/Florian_)) in Vienna, Europe.

Installation
------------

[](#installation)

```
$ composer require cocur/collection
```

Usage
-----

[](#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

[](#interfaces)

- [`CollectionInterface`](#collectioninterface)
- [`ItemInterface`](#iteminterface)

### Implementations

[](#implementations)

- [`Collection`](#collection)
- [`AbstractItem`](#abstractitem)
- [`Item`](#item)
- [`ArrayItem`](#arrayitem)

### CollectionInterface

[](#collectioninterface)

`Cocur\Collection\CollectionInterface` defines the basic methods of a collection and extends `IteratorAggregate` and `Countable`.

```
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.

```
Iterator getIterator()
```

Should return an iterator to iterate through the items of the collection.

```
int count()
```

Should return the number of items in the collection.

### ItemInterface

[](#iteminterface)

`Cocur\Collection\ItemInterface` defines two methods: `setCollection()` and `getCollection()`.

```
ItemInterface setCollection(CollectionInterface $collection)
```

Should store a reference to the collection in the item.

```
CollectionInterface|null getCollection()
```

Should return a reference to the collection or `null` if the item does not have a collection.

### Collection

[](#collection)

`Cocur\Collection\Collection` implements `Cocur\Collection\CollectionInterface` and uses a simple array to keep track of its items.

### AbstractItem

[](#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

[](#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`.

```
Item setValue(mixed $value)
mixed getValue()
```

In addition `Cocur\Collection\Item` has a factory method called `::create()`:

```
Item create(mixed $value = null)
```

### ArrayItem

[](#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.

```
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.

You can create new instances using the static `::createFromArray()` method:

```
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.

```
$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, "
```

Change Log
----------

[](#change-log)

### Version 0.1.1 (18 May 2015)

[](#version-011-18-may-2015)

- Add option default value to `ArrayItem::get()`

### Version 0.1 (28 April 2015)

[](#version-01-28-april-2015)

- Initial release

License
-------

[](#license)

The MIT license applies to cocur/collection. For the full copyright and license information, please view the [LICENSE](https://github.com/cocur/collection/blob/master/LICENSE) file distributed with this source code.

###  Health Score

26

—

LowBetter than 43% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity13

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity55

Maturing project, gaining track record

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~10 days

Total

3

Last Release

4018d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/a80f9fc61cd3a7d7779e8f120b458ca4d18fdd885d719bb77d3379b96bf714d9?d=identicon)[florianeckerstorfer](/maintainers/florianeckerstorfer)

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/cocur-collection/health.svg)

```
[![Health](https://phpackages.com/badges/cocur-collection/health.svg)](https://phpackages.com/packages/cocur-collection)
```

###  Alternatives

[jackiedo/dotenv-editor

The .env file editor tool for Laravel 5.8+

2041.2M31](/packages/jackiedo-dotenv-editor)[johnbillion/wp-crontrol

Take control of the cron events on your WordPress website or WooCommerce store

22381.4k1](/packages/johnbillion-wp-crontrol)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
