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

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

hyqo/collection
===============

1.0.0(3y ago)012MITPHPPHP ^8.2

Since Feb 22Pushed 3y ago1 watchersCompare

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

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

collection
==========

[](#collection)

[![Packagist Version](https://camo.githubusercontent.com/ad8f30124987013d550393e7ad7b163ed081a29acd39d728418cf173b118b3c4/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6879716f2f636f6c6c656374696f6e3f7374796c653d666c61742d737175617265)](https://camo.githubusercontent.com/ad8f30124987013d550393e7ad7b163ed081a29acd39d728418cf173b118b3c4/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6879716f2f636f6c6c656374696f6e3f7374796c653d666c61742d737175617265)[![Packagist PHP Version Support](https://camo.githubusercontent.com/d6331962df29bf0e082c65551bbfce0db0ad29da6364a262c9c7463120e49954/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f7068702d762f6879716f2f636f6c6c656374696f6e3f7374796c653d666c61742d737175617265)](https://camo.githubusercontent.com/d6331962df29bf0e082c65551bbfce0db0ad29da6364a262c9c7463120e49954/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f7068702d762f6879716f2f636f6c6c656374696f6e3f7374796c653d666c61742d737175617265)[![GitHub Workflow Status](https://camo.githubusercontent.com/1d1ef3d8d40b7111fd8740539ff561648990a2303d14e903ef9583b4d339f686/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f6879716f2f636f6c6c656374696f6e2f74657374732e796d6c3f6272616e63683d6d61696e267374796c653d666c61742d737175617265266c6162656c3d7465737473)](https://camo.githubusercontent.com/1d1ef3d8d40b7111fd8740539ff561648990a2303d14e903ef9583b4d339f686/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f6879716f2f636f6c6c656374696f6e2f74657374732e796d6c3f6272616e63683d6d61696e267374796c653d666c61742d737175617265266c6162656c3d7465737473)

Basic collection with [Generics support](https://blog.jetbrains.com/phpstorm/tag/generics/)

[![example](https://raw.githubusercontent.com/hyqo/assets/master/collection/example.png)](https://raw.githubusercontent.com/hyqo/assets/master/collection/example.png)

Install
-------

[](#install)

```
composer require hyqo/collection
```

Usage
-----

[](#usage)

For example, we have a `Product` class that we want to wrap in a collection:

```
class Product
{
    public $title;
    public $amount;

    public function __construct(string $title, int $amount){
        $this->title = $title;
        $this->amount = $amount;
    }
}
```

Create a collection:

```
use \Hyqo\Collection\Collection;
use function \Hyqo\Collection\collect;

$collection = new Collection([new Product('foo', 10), new Product('bar', 2)]);
$collection = collect([new Product('foo', 10), new Product('bar', 2)]);
```

### Auto-completion

[](#auto-completion)

There are three ways for code auto-completion:

**1.** Create a collection with items (not empty):

```
use Hyqo\Collection\Collection;

$collection = new Collection([new Product('foo', 10), new Product('bar', 2)]);
```

**2.** Use PHPDoc with Generics annotation:

```
use Hyqo\Collection\Collection;

/** @var Collection $collection */
$collection = new Collection();
```

**3.** Use your own class with `@extends` annotation:

```
use Hyqo\Collection\Collection;

/** @extends Collection */
class ProductCollection extends Collection
{
}

$collection = new ProductCollection();
```

Now you have auto-completion (see the picture above)

Methods
-------

[](#methods)

### add

[](#add)

```
function add($item): static
```

Add new item to a collection:

```
$collection->add($item);
```

### get

[](#get)

```
function get(int $index): T|null
```

Get item of a collection by index:

```
$collection->get(0);
```

### each

[](#each)

```
function each(callable $closure): static
```

Pass each item to a closure:

```
$collection->each(function(Product $product) {
    // do something
});
```

### map

[](#map)

```
function map(callable $closure): static
```

Pass each item to a closure and create a new collection of results.

The closure must return a value of `T` or `\Generator`:

```
$collection->map(function(Product $product) {
    // do something
    return $product;
});
```

### reduce

[](#reduce)

```
function reduce(callable $closure, $initial = null): mixed|null
```

Reduces the collection to a single value:

```
$collection = new Collection([new Product('foo', 10), new Product('bar', 2)]);

$amount = $collection->reduce(function($carry, Product $product) {
    return $carry + $product->amount;
});

// 4
```

### slice

[](#slice)

```
function slice(int $first, ?int $length = null): static
```

Create a new collection with a slice of the current one:

```
$collection->slice(3);
```

### copy

[](#copy)

```
function copy(): static
```

Create a new collection with the same elements (alias for `slice(0)`):

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

### chunk

[](#chunk)

```
function chunk(int $length): \Generator
```

Breaks the collection into multiple collections of a given length. The last one may contain fewer elements:

```
$collection->chunk(10);
```

### filter

[](#filter)

```
function filter(callable $closure): static
```

Pass each item to a closure and create a new collection of items for which its result will be `true`.

The closure must return a `bool` value:

```
$collection->filter(function(Product $product){
    return $product->amount > 1;
});
```

### toArray

[](#toarray)

```
function toArray(?callable $closure = null): array
```

Return all items of a collection. You can transform every element of array via a closure. If you need an associative array, the closure should return a generator yielding a key/value pair.

The closure must return any value or `\Generator`:

```
$collection->toArray(); // [Product, Product]

$collection->toArray(function(Product $product) {
    return $product->title;
}); // ['foo', 'bar']

$collection->toArray(function(Product $product): \Generator {
    yield $product->title => $product->amount;
}); // ['foo'=>10, 'bar'=>2]
```

###  Health Score

25

—

LowBetter than 37% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity5

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity57

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

Unknown

Total

1

Last Release

1172d ago

### Community

Maintainers

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

---

Top Contributors

[![akeylimepie](https://avatars.githubusercontent.com/u/355106?v=4)](https://github.com/akeylimepie "akeylimepie (3 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

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

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

###  Alternatives

[t3docs/examples

This extension packages a number of code examples from the Core Documentation.

3120.2k](/packages/t3docs-examples)

PHPackages © 2026

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