2017 © Pedro Peláez
 

library typed-collection

Type-safe collections based on Laravel Collections

image

gamez/typed-collection

Type-safe collections based on Laravel Collections

  • Tuesday, November 7, 2017
  • by jeromegamez
  • Repository
  • 1 Watchers
  • 6 Stars
  • 1,028 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 3 Versions
  • 3 % Grown

The README.md

Type-safe PHP collections based on Laravel Collections

Latest Stable Version Total Downloads Build Status Sponsor, (*1)

Installation

The package can be installed with Composer:, (*2)

$ 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 objects 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.
*/

For further information on how to use Laravel Collections, have a look at the official documentation., (*3)

The Versions

07/11 2017

dev-master

9999999-dev https://github.com/jeromegamez/typed-collection

Type-safe collections based on Laravel Collections

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel collection typed typesafe type-safe

17/10 2017

2.0

2.0.0.0 https://github.com/jeromegamez/typed-collection

Type-safe collections based on Laravel Collections

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel collection typed typesafe type-safe

31/08 2017

1.0

1.0.0.0 https://github.com/jeromegamez/typed-collection

Type-safe collections based on Laravel Collections

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel collection typed typesafe type-safe