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

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

gamez/typed-collection
======================

Type-safe collections based on Laravel Collections

8.0.0(1y ago)45317.8k↓14.5%8[1 PRs](https://github.com/jeromegamez/typed-collection/pulls)MITPHPCI passing

Since Aug 31Pushed 1mo ago2 watchersCompare

[ Source](https://github.com/jeromegamez/typed-collection)[ Packagist](https://packagist.org/packages/gamez/typed-collection)[ Docs](https://github.com/jeromegamez/typed-collection)[ GitHub Sponsors](https://github.com/sponsors/jeromegamez)[ RSS](/packages/gamez-typed-collection/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (10)Dependencies (3)Versions (20)Used By (0)

Type-safe PHP collections based on [Laravel Collections](https://laravel.com/docs/collections)
==============================================================================================

[](#type-safe-php-collections-based-on-laravel-collections)

[![Latest Stable Version](https://camo.githubusercontent.com/4441bb57e09318a5f91cf1c4651168dc776190f9f9bf02ebc0f2773a75436425/68747470733a2f2f706f7365722e707567782e6f72672f67616d657a2f74797065642d636f6c6c656374696f6e2f762f737461626c65)](https://packagist.org/packages/gamez/typed-collection)[![Total Downloads](https://camo.githubusercontent.com/f9f202772c3743da86bd41ed320b654d0652aa183c8768c50f4e932ba6462dd7/68747470733a2f2f706f7365722e707567782e6f72672f67616d657a2f74797065642d636f6c6c656374696f6e2f646f776e6c6f616473)](https://packagist.org/packages/gamez/typed-collection)[![Tests](https://github.com/jeromegamez/typed-collection/actions/workflows/tests.yml/badge.svg)](https://github.com/jeromegamez/typed-collection/actions/workflows/tests.yml)[![Sponsor](https://camo.githubusercontent.com/1004a94551d1edaf2a6da4d45ba217b79a46eb18dd7dd2d7825add0a2a8ddc4f/68747470733a2f2f696d672e736869656c64732e696f2f7374617469632f76313f6c6f676f3d476974487562266c6162656c3d53706f6e736f72266d6573736167653d25453225394425413426636f6c6f723d666636396234)](https://github.com/sponsors/jeromegamez)

Note

Laravel 11 added the `ensure()` collection method that verifies that all elements of a collection are of a given type or list of types. However, this verification does not prevent items of different types to be added at a later time.

Note

If you use Laravel collections combined with Larastan/PHPStan, you won't need this library and can just™ annotate your collection classes directly.

Installation
------------

[](#installation)

The package can be installed with [Composer](https://getcomposer.org):

```
$ composer require gamez/typed-collection
```

Usage
-----

[](#usage)

```
class Person
{
    public $name;

    public function __construct($name)
    {
        $this->name = $name;
    }
}

$taylor = new Person('Taylor');
$jeffrey = new Person('Jeffrey');
```

### Typed Collections

[](#typed-collections)

```
use Gamez\Illuminate\Support\TypedCollection;

/**
 * @extends TypedCollection
 */
class People extends TypedCollection
{
    protected static array $allowedTypes = [Person::class];
}

$people = People::make([$taylor, $jeffrey])
    ->each(function (Person $person) {
        printf("This is %s.\n", $person->name);
    });
/* Output:
This is Taylor.
This is Jeffrey.
*/

try {
    People::make('Not a person');
} catch (InvalidArgumentException $e) {
    echo $e->getMessage().PHP_EOL;
}
/* Output:
Output: A People collection only accepts items of the following type(s): Person.
*/
```

### Lazy Typed Collections

[](#lazy-typed-collections)

```
use Gamez\Illuminate\Support\LazyTypedCollection;

/**
 * @extends LazyTypedCollection
 */
class LazyPeople extends LazyTypedCollection
{
    protected static array $allowedTypes = [Person::class];
}

$lazyPeople = LazyPeople::make([$taylor, $jeffrey])
    ->each(function (Person $person) {
        printf("This is %s.\n", $person->name);
    });
/* Output:
This is Lazy Taylor.
This is Lazy Jeffrey.
*/

try {
    LazyPeople::make('Nope!');
} catch (InvalidArgumentException $e) {
    echo $e->getMessage().PHP_EOL;
}
/* Output:
Output: A People collection only accepts objects of the following type(s): Person.
*/
```

### Mixed collections

[](#mixed-collections)

```
/**
 * @extends LazyTypedCollection
 */
class MixedTypeCollection extends TypedCollection
{
    protected static array $allowedTypes = ['int', 'string', Person::class];
}
```

### Supported types

[](#supported-types)

Supported types are class strings, like `Person::class`, or types recognized by the [`get_debug_type()` function](https://www.php.net/get-debug-type), `int`, `float`, `string`, `bool`, and `array`.

### Helper functions

[](#helper-functions)

The `typedCollect()` helper function enables you to dynamically create typed collections on the fly:

```
$dateTimes = typedCollect([new DateTime(), new DateTime()], DateTimeInterface::class);
```

For further information on how to use Laravel Collections, have a look at the [official documentation](https://laravel.com/docs/collections).

###  Health Score

56

—

FairBetter than 98% of packages

Maintenance70

Regular maintenance activity

Popularity48

Moderate usage in the ecosystem

Community17

Small or concentrated contributor base

Maturity72

Established project with proven stability

 Bus Factor1

Top contributor holds 66.7% 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 ~172 days

Recently: every ~201 days

Total

17

Last Release

427d ago

Major Versions

3.0.1 → 4.0.02020-12-10

4.0.0 → 5.0.02022-02-09

5.2.0 → 6.0.02023-01-02

6.1.0 → 7.0.02024-03-17

7.1.0 → 8.0.02025-03-17

### Community

Maintainers

![](https://www.gravatar.com/avatar/8685cf532053a084f1eade7b7da00a512c02676e65f1f1bdec73d4978030a47d?d=identicon)[jeromegamez](/maintainers/jeromegamez)

---

Top Contributors

[![jeromegamez](https://avatars.githubusercontent.com/u/67554?v=4)](https://github.com/jeromegamez "jeromegamez (64 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (14 commits)")[![github-actions[bot]](https://avatars.githubusercontent.com/in/15368?v=4)](https://github.com/github-actions[bot] "github-actions[bot] (14 commits)")[![mad-briller](https://avatars.githubusercontent.com/u/28307684?v=4)](https://github.com/mad-briller "mad-briller (2 commits)")[![StevePorter92](https://avatars.githubusercontent.com/u/12199424?v=4)](https://github.com/StevePorter92 "StevePorter92 (2 commits)")

---

Tags

collectioncollectionslaravelphptype-safetytypedlaravelcollectiontype-safetypedtypesafe

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Type Coverage Yes

### Embed Badge

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

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

###  Alternatives

[cerbero/lazy-json

Framework-agnostic package to load JSONs of any dimension and from any source into Laravel lazy collections.

254309.8k1](/packages/cerbero-lazy-json)[pragmarx/ia-collection

Laravel Illuminate Agnostic Collection

473.4M2](/packages/pragmarx-ia-collection)[werxe/laravel-collection-macros

Custom Laravel Collection macros.

2625.8k](/packages/werxe-laravel-collection-macros)[iteks/laravel-enum

A comprehensive Laravel package providing enhanced enum functionalities, including attribute handling, select array conversions, and fluent facade interactions for robust enum management in Laravel applications.

2516.7k](/packages/iteks-laravel-enum)[dutchcodingcompany/csv-collection

Read and write large csv files using Laravel's Illuminate Collections

117.5k](/packages/dutchcodingcompany-csv-collection)[prologue/support

Prologue Support is an extension for Illuminate Support

1616.8k](/packages/prologue-support)

PHPackages © 2026

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