2017 © Pedro PelĂĄez
 

library typed-collection

Create strictly typed collections in PHP

image

lc5/typed-collection

Create strictly typed collections in PHP

  • Friday, September 29, 2017
  • by Lc5
  • Repository
  • 1 Watchers
  • 1 Stars
  • 8,864 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 3 Versions
  • 14 % Grown

The README.md

TypedCollection

Build Status Latest Stable Version Total Downloads PHP Version Require License PHPStan Enabled, (*1)

Create strictly typed collections in PHP., (*2)

Installation

$ composer require lc5/typed-collection

AbstractTypedCollection:

An abstract class used to create strictly typed collections implemented as a type-checking wrapper around ArrayObject. The type of elements in a collection is defined by extending AbstractTypedCollection and implementing abstract AbstractTypedCollection::getType method. It should return the type as a string, which can be any class name or one of the internal types in a form recognised by the internal gettype() function ("boolean", "integer", "double", "string", "array", "object", "resource", "NULL"). UnexpectedValueException will be thrown, when trying to add an element with an invalid type., (*3)

Usage:

use Lc5\TypedCollection\AbstractTypedCollection;
use Lc5\TypedCollection\Exception\UnexpectedValueException;

class stdClassCollection extends AbstractTypedCollection
{
    public function getType(): string
    {
        return \stdClass::class; //can be any class or internal type
    }
}

$collection = new stdClassCollection([new \stdClass(), new \stdClass()]);
$collection[] = new \stdClass();

try {
    $collection[] = 'invalid';
} catch (UnexpectedValueException $e) {
    echo $e->getMessage(); //Invalid value type: string. Only \stdClass is allowed.
}

try {
    $collection = new stdClassCollection(['invalid', new \stdClass()]);
} catch (UnexpectedValueException $e) {
    echo $e->getMessage(); //Invalid value type: string. Only \stdClass is allowed.
}

TypedCollection:

A strictly typed collection based on ArrayObject. The type of elements in collection is defined using constructor argument, which can be any class name or one of the internal types in a form recognised by internal gettype() function ("boolean", "integer", "double", "string", "array", "object", "resource", "NULL"). UnexpectedValueException will be thrown, when trying to add element with invalid type., (*4)

Usage:

use Lc5\TypedCollection\TypedArray;
use Lc5\TypedCollection\Exception\UnexpectedValueException;

$values = [new \stdClass(), new \stdClass()];

$typedCollection = new TypedCollection(\stdClass::class, $values);
$typedCollection[] = new \stdClass();

try {
    $typedCollection[] = 'invalid';
} catch (UnexpectedValueException $e) {
    echo $e->getMessage(); //Invalid value type: string. Only \stdClass is allowed.
}

The behavior is identical as in AbstractTypedCollection., (*5)

The Versions

29/09 2017

dev-master

9999999-dev https://github.com/Lc5/TypedCollection

Create strictly typed collections in PHP

  Sources   Download

MIT

The Requires

  • php >=5.6.0

 

The Development Requires

by Ɓukasz Krzyszczak

collection typed collection

01/08 2017

1.0.1

1.0.1.0 https://github.com/Lc5/TypedCollection

Create strictly typed collections in PHP

  Sources   Download

MIT

The Requires

  • php >=5.6.0

 

The Development Requires

by Ɓukasz Krzyszczak

collection typed collection

01/08 2017

1.0.0

1.0.0.0 https://github.com/Lc5/TypedCollection

Create strictly typed collections in PHP

  Sources   Download

MIT

The Requires

  • php >=5.6.0

 

The Development Requires

by Ɓukasz Krzyszczak

collection typed collection