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. [Utility &amp; Helpers](/categories/utility)
4. /
5. pinkcrab/collection

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

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

A flexible and extendable Collection

1.0.0(3mo ago)23.5k↓84.4%2MITPHPPHP &gt;=7.1.0CI passing

Since May 8Pushed 3mo 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 1w ago

READMEChangelog (5)Dependencies (12)Versions (9)Used By (2)

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

[](#pinkcrab-collection)

[![Latest Stable Version](https://camo.githubusercontent.com/1fa1f26e83b25cf728fdc1c4a57ec67d4ade123273fb8979f1c7deecfc45528c/68747470733a2f2f706f7365722e707567782e6f72672f70696e6b637261622f636f6c6c656374696f6e2f76)](https://packagist.org/packages/pinkcrab/collection)[![Total Downloads](https://camo.githubusercontent.com/81a13e76df1782fe3dd057d0f7396ab4f8242d61d95711540a8d01210ee3d1a0/68747470733a2f2f706f7365722e707567782e6f72672f70696e6b637261622f636f6c6c656374696f6e2f646f776e6c6f616473)](https://packagist.org/packages/pinkcrab/collection)[![License](https://camo.githubusercontent.com/658682b8c7682fd4f384d680106abffb439447cb0a2771e47706a635b11a9431/68747470733a2f2f706f7365722e707567782e6f72672f70696e6b637261622f636f6c6c656374696f6e2f6c6963656e7365)](https://packagist.org/packages/pinkcrab/collection)[![PHP Version Require](https://camo.githubusercontent.com/d4392726f77255d38a95264e6d023efb3fa9123b66d4860e16332fb02cbb0827/68747470733a2f2f706f7365722e707567782e6f72672f70696e6b637261622f636f6c6c656374696f6e2f726571756972652f706870)](https://packagist.org/packages/pinkcrab/collection)[![GitHub contributors](https://camo.githubusercontent.com/860891b0e3bd7f9ecec5de18a1e8915d6effd7b73d81596c28a6e2256f74cfc8/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f636f6e7472696275746f72732f50696e6b2d437261622f436f6c6c656374696f6e3f6c6162656c3d436f6e7472696275746f7273)](https://camo.githubusercontent.com/860891b0e3bd7f9ecec5de18a1e8915d6effd7b73d81596c28a6e2256f74cfc8/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f636f6e7472696275746f72732f50696e6b2d437261622f436f6c6c656374696f6e3f6c6162656c3d436f6e7472696275746f7273)[![GitHub issues](https://camo.githubusercontent.com/9dccbd088b9597443750ed5792a05699591460375ab585391c4a53758ad1c06b/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6973737565732d7261772f50696e6b2d437261622f436f6c6c656374696f6e)](https://camo.githubusercontent.com/9dccbd088b9597443750ed5792a05699591460375ab585391c4a53758ad1c06b/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6973737565732d7261772f50696e6b2d437261622f436f6c6c656374696f6e)

[![Tests [PHP7.1-8.5]](https://github.com/Pink-Crab/Collection/actions/workflows/php.yaml/badge.svg)](https://github.com/Pink-Crab/Collection/actions/workflows/php.yaml)

[![codecov](https://camo.githubusercontent.com/96f11b3eda392f4a593524018377062f78113d73ddd8fe83ec291233e31b9328/68747470733a2f2f636f6465636f762e696f2f67682f50696e6b2d437261622f636f6c6c656374696f6e2f6272616e63682f6d61737465722f67726170682f62616467652e7376673f746f6b656e3d36745565696132763253)](https://codecov.io/gh/Pink-Crab/collection)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/28ff1bce58c432cdc8467a70249fe9add6183803b5b309945471e6b239678fee/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f50696e6b2d437261622f436f6c6c656374696f6e2f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/Pink-Crab/Collection/?branch=master)

Version 1.0.0
-------------

[](#version-100)

> 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)

Install via Composer:

```
composer require pinkcrab/collection
```

Then include the Composer autoloader in your project:

```
require_once __DIR__ . '/vendor/autoload.php';
```

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)

```
