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

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

benconda/collection
===================

Collection library, powered by generators, with generics, easily extendable and immutable. Lazy by design and memory friendly. Accept anything as iterable 🔥 (Generator, array, Iterator, ...)

1.0.0(1y ago)56MITPHPPHP ^8.2

Since Apr 27Pushed 1y ago3 watchersCompare

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

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

Collection
==========

[](#collection)

Collection library, powered by generators, with generics, easily extendable and immutable. **Lazy** by design and memory friendly. Accept anything as iterable 🔥 (Generator, array, Iterator, ...)

Requirement
===========

[](#requirement)

To use this library you need at least php 8.2

Usage
=====

[](#usage)

You first need to create the collection from an iterable, it can be an array, or anything that is iterable (generators, objects iterators, etc)

```
$collection = Collection::from($myIterable);
```

Now you can apply one or multiple Modifier to it :

```
use BenConda\Collection\Collection;
// [...]

$collection = Collection::from(range(1, 10))
    ->filter(callback: static fn(int $item): bool => $item > 5)
    ->map(on: fn(int $item): string => "The number is $item");
```

Each time you call a modifier method it returns a new instance of the collection, nothing is override.

The collection is invokable, so you can add modifiers like this too :

```
$collection = Collection::from(range(1, 10))
(
  new Filter(
    callback: fn(int $item): bool => $item >= 5
  )
)
(
  new Map(
    callback: fn(int $item): string => "The number is $item"
  )
);
```

Because each modifier rely on generators, nothing is done until you start to iterate on the collection.

You can get the first value like this :

```
$first = $collection->first(); // $first = The number is 5
```

Note that it won't go through the whole array to return the first value, behind the scene it only iterate through 1,2,3,4,5 =&gt; `The number is 5` and stop.

This is possible thanks to generators, most other Collection libraries will need to loop through the whole array to filter it, then Map every filtered value before you can get the first value.

We can iterate on all value using a foreach directly on the collection $object :

```
$collection = Collection::from(range(1, 10))
foreach ($collection as $key => $item)
{
  // do what you want
}
```

And you can translate the whole Collection into an array like this :

```
$collection->toArray();
```

Be careful, by design we accept anything as a key, array restrict keys to `int|string` type, if the key type mismatch, it will fallback to int incremented key.

Modifiers
=========

[](#modifiers)

Collection iteration can be altered using modifiers, which allow you to shape and transform data in a memory friendly way.

Some modifiers require some memory buffering, and are split in another namespace called `BufferedModifier`. This is the case for example with `BenConda\Collection\BufferedModifier\Reverse`, in order to reverse, the whole collection need to be loaded in memory.

So keep in mind, `BufferedModifier` namespace = will consume memory depending on the size of the iterable.

You will find the modifiers list [in this documentation](./docs/modifiers.md)

Note : documentation is in progress

Extend
======

[](#extend)

Add custom modifier
-------------------

[](#add-custom-modifier)

Simply create a class that implement `BenConda\Collection\Modifier\ModifierInterface`

For example, for the reindex modifier :

```
use Generator;
use BenConda\Collection\Modifier\ModifierInterface;

/**
 * @template TKey
 * @template TValue
 *
 * @implements ModifierInterface
 */
final class Reindex implements ModifierInterface
{
    /**
     *
     * @param iterable $iterable
     *
     * @return Generator
     */
    public function __invoke(iterable $iterable): Generator
    {
        foreach ($iterable as $value) {
            yield $value;
        }
    }
}
```

If you need some configuration, simply add a constructor to the class.

Using your own collection class
-------------------------------

[](#using-your-own-collection-class)

Sometimes you need to create your own, strict typed Collection class.

To do so, you can extend the CoreCollection class, and implement only the needed modifier method.

Sometimes you may need to have mutable custom Collection, for this use case you can extend MutableCoreCollection. You will find an example in [this test](tests/unit/CustomCollectionTest.php)

Note that even with mutable Collection, you still rely on Generators behind so each time you modify your collection, nothing is really done until you loop through it.

###  Health Score

30

—

LowBetter than 64% of packages

Maintenance34

Infrequent updates — may be unmaintained

Popularity9

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity59

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

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 ~449 days

Total

2

Last Release

663d ago

Major Versions

0.1.0-alpha → 1.0.02024-07-19

PHP version history (2 changes)0.1.0-alphaPHP ^8.1

1.0.0PHP ^8.2

### Community

Maintainers

![](https://www.gravatar.com/avatar/679127c41d00251c3d92ccc5a7020765a4848650c46515400e13e5ae11f257fb?d=identicon)[benconda](/maintainers/benconda)

---

Top Contributors

[![benconda](https://avatars.githubusercontent.com/u/20043165?v=4)](https://github.com/benconda "benconda (8 commits)")

---

Tags

collectioncollectionsphp-library

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

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

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

PHPackages © 2026

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