jcrowe/type-safe-collection
A thin wrapper around Illuminate/Collection that allows for enforcing type constraints on the collection elements
9,230
4
| Install | |
|---|---|
composer require jcrowe/type-safe-collection |
|
| Latest Version: | v7.1 |
| PHP: | >=7.0 |
| Laravel: | 7 |
| Older releases: | 4 · 5 · 6 |
| License: | MIT |
| Last Updated: | May 5, 2020 |
| Links: | GitHub · Packagist |
Maintainer: jcrowe206
TypeSafe Collections
TypeSafeCollection is a thin wrapper around Illuminate's Collection object 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
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
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
composer require jcrowe/type-safe-collection
{
"require": {
"jcrowe/type-safe-collection": "~1.0"
}
}
Related Packages
aimeos/map
Easy and elegant handling of PHP arrays as array-like collection objects similar...
521,124
4,326
rgasch/illuminate-collection-extended
Extension class for Laravel's Collection class with some extra functionality.
4,480
1
square1/laravel-collection-rolling-average
Extend collections to support rolling average functionality.
19,343
23
vistik/type-hinted-arrays
A simple way to create typed collections in PHP - build on Illuminate\Support\Co...
8,892
1
collector/collector
A collection library inspired by Laravel Collections, built using generators to...
208
15
Version History
| Version | Released | PHP | Laravel | License |
|---|---|---|---|---|
| v7.1 | >=7.0 | ^7.0 | MIT | |
| v7.0 | >=5.5.0 | ^4.0 | | MIT | |
| v5.0 | >=5.5.0 | 5.* | MIT | |
| 1.0.3 | >=5.5.0 | 4.0.0 - 5 | MIT | |
| v1.0.2 | >=5.5.0 | ^5.2@dev | MIT | |
| v1.0.1 | >=5.5.0 | ^5.2@dev | MIT | |
| 1.0 | >=5.3.0 | ^5.2@dev | MIT |