Get support for jeromegamez/typed-collection
If you're new to LTH, please see our FAQ for more information on what it is we do.
Support Options
Unfortunately, there are currently no active helpers for this repository on the platform. Until they become available, we reccomend the following actions:
View Open IssuesTake a look to see if anyone else has experienced the same issue as you and if they managed to solve it.
Open an IssueMake sure to read any relevant guidelines for opening issues on this repo before posting a new issue.
Sponsor directlyCheck out the page and see if there are any options to sponsor this project or it's developers directly.
jeromegamez/typed-collection
Type-safe PHP collections based on Laravel Collections
[!NOTE]
Laravel 11 added theensure()
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.
Installation
The package can be installed with Composer:
$ composer require gamez/typed-collection
Usage
class Person
{
public $name;
public function __construct($name)
{
$this->name = $name;
}
}
$taylor = new Person('Taylor');
$jeffrey = new Person('Jeffrey');
Typed Collections
use Gamez\Illuminate\Support\TypedCollection;
class People extends TypedCollection
{
protected static $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
use Gamez\Illuminate\Support\LazyTypedCollection;
class LazyPeople extends LazyTypedCollection
{
protected static $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.
*/
Supported types
Supported types are class strings, like Person::class
, or types recognized by the
get_debug_type()
function, int
, float
,
string
, bool
, and array
.
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.
Our Mission
We want to make open source more sustainable. The entire platform was born from this and everything we do is in aid of this.
From the Blog
Interesting Articles
-
Generating income from open source
Jun 23 • 8 min read
-
2023 State of OSS
Apr 23 • 45 min read ★
-
A funding experiment...
Aug 19 • 10 min read
-
But You Said I could
Aug 19 • 2 min read
Thank you for checking out LiveTechHelper |
2025 © lth-dev incorporated
p-e622a1a2