2017 © Pedro Peláez
 

library type-safe-collection

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

image

jcrowe/type-safe-collection

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

  • Friday, March 25, 2016
  • by jcrowe206
  • Repository
  • 1 Watchers
  • 3 Stars
  • 2,900 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 6 Versions
  • 57 % Grown

The README.md

TypeSafe Collections

Build Status Coverage Status, (*1)

TypeSafeCollection is a thin wrapper around Illuminate's Collection object that allows for easy enforcement of type protection in your collections., (*2)


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"
    }
}

The Versions

25/03 2016

v5.0

5.0.0.0

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

  Sources   Download

MIT

The Requires

 

The Development Requires

by Jonathan Crowe

laravel collection array illuminate type safe

17/03 2016

dev-master

9999999-dev

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

  Sources   Download

MIT

The Requires

 

The Development Requires

by Jonathan Crowe

laravel collection array illuminate type safe

03/10 2015

1.0.3

1.0.3.0

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

  Sources   Download

MIT

The Requires

 

The Development Requires

by Jonathan Crowe

laravel collection array illuminate type safe

01/10 2015

v1.0.2

1.0.2.0

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

  Sources   Download

MIT

The Requires

 

The Development Requires

by Jonathan Crowe

laravel collection array illuminate type safe

29/09 2015

v1.0.1

1.0.1.0

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

  Sources   Download

MIT

The Requires

 

The Development Requires

by Jonathan Crowe

laravel collection array illuminate type safe

29/09 2015

1.0

1.0.0.0

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

  Sources   Download

MIT

The Requires

 

The Development Requires

by Jonathan Crowe

laravel collection array illuminate type safe