PHPackages                             jcrowe/type-safe-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. jcrowe/type-safe-collection

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

jcrowe/type-safe-collection
===========================

A thin wrapper around Illuminate/Collection that allows for enforcing type constraints on the collection elements

v7.1(6y ago)59.2k4MITPHPPHP &gt;=7.0CI failing

Since Sep 29Pushed 6y ago1 watchersCompare

[ Source](https://github.com/jcrowe206/type-safe-collection)[ Packagist](https://packagist.org/packages/jcrowe/type-safe-collection)[ RSS](/packages/jcrowe-type-safe-collection/feed)WikiDiscussions master Synced yesterday

READMEChangelogDependencies (4)Versions (9)Used By (0)

TypeSafe Collections
====================

[](#typesafe-collections)

[![Build Status](https://camo.githubusercontent.com/4ded3a6f1daf4a0d6c08d1f904a3b494c85d2d0adcb1cd0c4625e2b9b1243373/68747470733a2f2f7472617669732d63692e6f72672f6a63726f77653230362f747970652d736166652d636f6c6c656374696f6e2e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/jcrowe206/type-safe-collection) [![Coverage Status](https://camo.githubusercontent.com/15853a2aa006208e0df3528c4ff8af15ba3d192976f9095fd4dfe6154b5f4027/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6a63726f77653230362f747970652d736166652d636f6c6c656374696f6e2f62616467652e7376673f6272616e63683d6d617374657226736572766963653d676974687562)](https://coveralls.io/github/jcrowe206/type-safe-collection?branch=master)

TypeSafeCollection is a thin wrapper around [Illuminate's Collection object](https://github.com/illuminate/support/blob/master/Collection.php) that allows for easy enforcement of type protection in your collections.

```
use JCrowe\TypeSafeCollection\TypeSafeCollection;

class MovieLibrary extends TypeSafeCollection {

    // list of classes that can be added to the collection
    protected $allowedClasses = [Watchable::class, Rentable::class];
}

$myLibrary = new MovieLibrary([
    new WatchableMovie(),
    new RentableDVD(),
    new ReadableBook() // throws \InvalidArgumentProvided exception
]);

$myLibrary = new MovieLibrary();

$myLibarry->push(new RentableDVD());

$myLibrary->push(new ReadableBook()); // throws \InvalidArgumentProvided exception
```

#### Custom checks

[](#custom-checks)

```
class MovieLibrary extends TypeSafeCollection {

    // list of classes that can be added to the collection
    protected $allowedClasses = [Watchable::class, Rentable::class];

    // this function will be called whenever a new
    // element is being added to the collection
    protected function onAddNewElement($element)
    {
        if (!$element->isAvailable()) {

            return false; // or throw exception
        }
    }
}
```

#### Ignore invalid types and do not throw exception

[](#ignore-invalid-types-and-do-not-throw-exception)

```
class MovieLibrary extends TypeSafeCollection {

    // list of classes that can be added to the collection
    protected $allowedClasses = [Watchable::class, Rentable::class];

    // if set to true no exception will be thrown when
    // attempting to add an invalid value.
    protected $ignoreInvalidElements = true;
}

$myLibrary = new MovieLibrary();

$myLibrary->put('my_rentable', new RentableDVD());

$myLibrary->get('my_rentable'); // RentableDVD object

$myLibrary->put('my_readable', new ReadableBook()); // no exception is thrown

$myLibrary->get('my_readable'); // null
```

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

[](#installation)

```
composer require jcrowe/type-safe-collection

```

```
{
    "require": {
        "jcrowe/type-safe-collection": "~1.0"
    }
}
```

###  Health Score

33

—

LowBetter than 72% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity26

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity63

Established project with proven stability

 Bus Factor1

Top contributor holds 78.3% 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 ~280 days

Recently: every ~419 days

Total

7

Last Release

2249d ago

Major Versions

1.0.3 → v5.02016-03-25

v5.0 → v7.02020-05-05

PHP version history (3 changes)1.0PHP &gt;=5.3.0

v1.0.1PHP &gt;=5.5.0

v7.1PHP &gt;=7.0

### Community

Maintainers

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

---

Top Contributors

[![jcrowe206](https://avatars.githubusercontent.com/u/928788?v=4)](https://github.com/jcrowe206 "jcrowe206 (18 commits)")[![bramr94](https://avatars.githubusercontent.com/u/24361182?v=4)](https://github.com/bramr94 "bramr94 (5 commits)")

---

Tags

laravelarraycollectionilluminatetype-safe

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/jcrowe-type-safe-collection/health.svg)

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

###  Alternatives

[psalm/plugin-laravel

Psalm plugin for Laravel

3355.3M346](/packages/psalm-plugin-laravel)[renatomarinho/laravel-page-speed

Laravel Page Speed

2.5k1.7M11](/packages/renatomarinho-laravel-page-speed)[vinkius-labs/laravel-page-speed

Laravel Page Speed

2.5k12.5k1](/packages/vinkius-labs-laravel-page-speed)[emargareten/inertia-modal

Inertia Modal is a Laravel package that lets you implement backend-driven modal dialogs for Inertia apps.

90142.9k](/packages/emargareten-inertia-modal)[wearepixel/laravel-cart

A cart implementation for Laravel

1374.8k](/packages/wearepixel-laravel-cart)[tomshaw/electricgrid

A feature-rich Livewire package designed for projects that require dynamic, interactive data tables.

119.4k](/packages/tomshaw-electricgrid)

PHPackages © 2026

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