PHPackages                             pinkcrab/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. pinkcrab/collection

ActiveLibrary

pinkcrab/collection
===================

A flexible and extendable Collection

0.2.0(4y ago)22.7k↑14.3%[1 issues](https://github.com/Pink-Crab/Collection/issues)1MITPHPPHP &gt;=7.1.0

Since May 8Pushed 4y agoCompare

[ Source](https://github.com/Pink-Crab/Collection)[ Packagist](https://packagist.org/packages/pinkcrab/collection)[ Docs](https://pinkcrab.co.uk)[ RSS](/packages/pinkcrab-collection/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (4)Dependencies (7)Versions (6)Used By (1)

PinkCrab Collection
===================

[](#pinkcrab-collection)

[![alt text](https://camo.githubusercontent.com/e3d0a7ef7e3188ba15b49a317babe08bed4ca8f0080cd6d4de1c8f7c84d754c2/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f43757272656e745f56657273696f6e2d302e322e302d79656c6c6f772e7376673f7374796c653d666c6174 " ")](https://camo.githubusercontent.com/e3d0a7ef7e3188ba15b49a317babe08bed4ca8f0080cd6d4de1c8f7c84d754c2/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f43757272656e745f56657273696f6e2d302e322e302d79656c6c6f772e7376673f7374796c653d666c6174)![Open Source Love](https://camo.githubusercontent.com/2d4eac62d5f5830a5309100b88e0ecccb171aee1fbcd794149f4a580b0010c56/68747470733a2f2f6261646765732e66726170736f66742e636f6d2f6f732f6d69742f6d69742e7376673f763d313032)[![](https://github.com/Pink-Crab/Collection/workflows/GitHub_CI/badge.svg " ")](https://github.com/Pink-Crab/Collection/workflows/GitHub_CI/badge.svg)[![codecov](https://camo.githubusercontent.com/96f11b3eda392f4a593524018377062f78113d73ddd8fe83ec291233e31b9328/68747470733a2f2f636f6465636f762e696f2f67682f50696e6b2d437261622f636f6c6c656374696f6e2f6272616e63682f6d61737465722f67726170682f62616467652e7376673f746f6b656e3d36745565696132763253)](https://codecov.io/gh/Pink-Crab/collection)

Version 0.2.0
-------------

[](#version-020)

> This library was extracted from the PinkCrab Plugin Framework (Perique)

Why?
----

[](#why)

Give access to a basic collection with all expected functionlaity, filtering, mapping, folding, sorting and comparing. But is also extendable for creating custom collections, which can be expanded and typed. A fairly simple, but extendable Collection.

Install
-------

[](#install)

> `composer install pink-crab/collection`

Basic Useage
------------

[](#basic-useage)

> See [`./docs`](./docs) for more details and examples.

```
$collection = new Collection(['1',2,'3']);
$collection->push(4);
$collection->apply(fn($e) => (string) $e);
var_dump($collection); // ['1','2','3','4'];
```

---

```
$collection = new Collection([1,2,3,4,5,6,7,8,9,10]);
$collection->filter(fn($e) => $e % 2 == 0);
var_dump($collection); // [2,4,6,8,10];
```

Extendable Traits
-----------------

[](#extendable-traits)

The Collection package comes with a few Traits which can be used when creating custom collections. These can either be created on the fly using anonymous classes or through defining them as full classes.

```
$indexed_collection = new class() extends \PinkCrab\Collection\Collection {
	use \PinkCrab\Collection\Traits\Indexed;
};

$indexed_collection->set( 'key1', 'value1' );
$indexed_collection->has( 'key1' ); //true
var_dump( $indexed_collection );

// As a full class.
class Indexed_Collection extends \PinkCrab\Collection\Collection {
	use \PinkCrab\Collection\Traits\Indexed;
};

$indexed_collection = new Indexed_Collection();
$indexed_collection->set( 'key1', 'value1' );
$indexed_collection->has( 'key1' ); //true
var_dump( $indexed_collection );
```

Typed &amp; Mapped Collections
------------------------------

[](#typed--mapped-collections)

```
