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

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

purrphp/pure-collection
=======================

Immutable type-safe collections for PHP

1.0.0(2mo ago)18↓90%1MITPHPPHP ^8.1CI passing

Since Mar 30Pushed 2mo agoCompare

[ Source](https://github.com/PurrPHP/pure-collection)[ Packagist](https://packagist.org/packages/purrphp/pure-collection)[ Docs](https://github.com/PurrPHP/pure-collection)[ RSS](/packages/purrphp-pure-collection/feed)WikiDiscussions master Synced 3w ago

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

PurrPHP/collection
==================

[](#purrphpcollection)

Type-safe collections for PHP 8. Type safety based on native PHP features, not static analyzers. Inspired by [Kotlin collections](https://kotlinlang.org/docs/collections-overview.html).

Documentation
-------------

[](#documentation)

- [Common Methods](docs/common-methods.md) — methods available on every collection
- [Int Collections](docs/int-collections.md) — integer-typed classes and their specific API
- [String Collections](docs/string-collections.md) — string-typed classes and their specific API
- [Custom Collections](docs/custom-collections.md) — step-by-step guide for implementing your own typed collection
- [Architecture Decisions](docs/adr.md) — key design decisions and their rationale

### Problem scope

[](#problem-scope)

PHP has no native typed collections — only plain arrays. This makes it easy to accidentally mix types or corrupt key structure:

```
$uuids = [];
$uuids[] = Uuid::fromInteger('1');
$uuids[] = '00000000-0000-0000-0000-000000000001'; // wrong type, no error

$uuids[] = Uuid::fromInteger('2');
$uuids['validUuid'] = Uuid::fromInteger('3');       // mixed int/string keys
```

This library solves these problems by providing type-safe, semantically clear collections that reduce boilerplate and prevent mistakes at runtime.

Code structure
--------------

[](#code-structure)

Inspired by [Kotlin collections](https://kotlinlang.org/docs/collections-overview.html), the library provides three collection types:

- **List** — ordered sequence with integer keys, duplicates allowed
- **Set** — ordered sequence of unique values
- **Map** — associative array (key → value)

Collections are immutable and return new object after each manipulation.

Hierarchy:

```
CollectionInterface
└── AbstractCollection
    ├── AbstractList               — ordered list (sequential integer keys)
    │   ├── AbstractSet            — deduplicates values on construction
    └── AbstractMap                — associative map (string/int keys)

```

Available Classes
-----------------

[](#available-classes)

ClassDescription`IntList`Ordered list of integers`IntSet`Ordered list of unique integers`IntNotEmptyList`Same as `IntList`, throws on empty construction`IntNotEmptySet`Same as `IntSet`, throws on empty construction`IntMap`Associative (key → int) map`StringList`Ordered list of strings`StringSet`Ordered list of unique strings`StringNotEmptySet`Same as `StringSet`, throws on empty construction`StringMap`Associative (key → string) mapInstallation
------------

[](#installation)

```
composer require purrphp/pure-collection
```

Usage
-----

[](#usage)

### Lists

[](#lists)

```
use Purr\Collection\IntList;

$numbers = new IntList(1, 2, 3, 4, 5);

$even    = $numbers->filter(fn(int $n): bool => $n % 2 === 0);
$doubled = $numbers->map(fn(int $n): int => $n * 2); // returns array
$sum     = $numbers->reduce(fn(int $carry, int $n): int => $carry + $n, 0);
$sorted  = $numbers->sorted(fn(int $a, int $b): int => $b  $a);

$numbers->count();      // 5
$numbers->isEmpty();    // false
$numbers->isNotEmpty(); // true
$numbers->max();        // 5
$numbers->min();        // 1
```

### Sets (unique values)

[](#sets-unique-values)

```
use Purr\Collection\IntSet;
use Purr\Collection\StringSet;

$set = new IntSet(1, 2, 3, 2, 1); // duplicates are removed
$set->toArray();                   // [1, 2, 3]

$ids = IntSet::fromString('1,2,3', separator: ',');
$ids->join(',');                   // "1,2,3"

$tags = new StringSet('php', 'oop', 'php');
$tags->toArray();                  // ['php', 'oop']
$tags->sortedAlphabetically()->toArray(); // ['oop', 'php']

$a = new StringSet('a', 'b', 'c');
$b = new StringSet('b', 'c', 'd');
$a->diff($b)->toArray();           // ['a']
```

### Maps

[](#maps)

```
use Purr\Collection\IntMap;

$map = new IntMap(...['a' => 1, 'b' => 2, 'c' => 3]);

$map->filter(fn(int $v): bool => $v > 1)->toArray(); // ['b' => 2, 'c' => 3]
$map->groupBy(fn(int $v): string => $v % 2 === 0 ? 'even' : 'odd');
```

### Common Operations

[](#common-operations)

All collections implement `CollectionInterface`. See the full reference:

- [Common Methods](docs/common-methods.md) — searching, filtering, transforming, grouping, sizing, iteration
- [Int Collections](docs/int-collections.md) — aggregation, sorting, set operations variants
- [String Collections](docs/string-collections.md) — alphabetical sorting, set operations variants

Extending
---------

[](#extending)

To create a custom typed collection, extend `AbstractList`, `AbstractSet`, or `AbstractMap`. See [Architecture Decisions](docs/adr.md) for decisions motivation and [detailed guide](docs/custom-collections.md) for custom implementation.

```
use Purr\Collection\AbstractSet;

/** @template-extends AbstractSet */
class DateTimeSet extends AbstractSet
{
    public function __construct(\DateTimeImmutable ...$dates)
    {
        parent::__construct($dates);
    }
     protected function isSupportedType(mixed $value): bool
    {
        return $value instanceof \DateTimeImmutable;
    }

    protected function getId(mixed $value): int|string
    {
        return $value->getTimeStamp();
    }
}
```

Development
-----------

[](#development)

```
composer install

composer test        # Run tests
composer analyse     # Static analysis
composer cs-check    # Code style check
composer cs-fix      # Fix code style
composer check       # All checks
```

### With Docker / Make

[](#with-docker--make)

```
make test       # Run tests
make analyse    # Static analysis
make cs-check   # Code style check
make check      # All checks
make shell      # Open shell in dev container
```

License
-------

[](#license)

MIT — see [LICENSE](LICENSE).

###  Health Score

36

—

LowBetter than 79% of packages

Maintenance83

Actively maintained with recent releases

Popularity6

Limited adoption so far

Community5

Small or concentrated contributor base

Maturity42

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

Unknown

Total

1

Last Release

88d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/5460215?v=4)[Aleksey M](/maintainers/don-tre)[@don-tre](https://github.com/don-tre)

---

Tags

phpmapsetcollectionlistimmutabledata structurestype-safe

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

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

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

###  Alternatives

[phpcollection/phpcollection

General-Purpose Collection Library for PHP

96864.5M34](/packages/phpcollection-phpcollection)[chdemko/sorted-collections

Sorted Collections for PHP &gt;= 8.4

222.6M3](/packages/chdemko-sorted-collections)[equip/structure

Simple, immutable data structures

40202.3k5](/packages/equip-structure)

PHPackages © 2026

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