vistik/type-hinted-arrays

A simple way to create typed collections in PHP - build on Illuminate\Support\Collection
8,892 1
Install
composer require vistik/type-hinted-arrays
Latest Version:v2.0.0
PHP:>=7.0.0
Last Updated:Feb 12, 2017
Links: GitHub  ·  Packagist
Maintainer: vistik

Build Status

What is this?

This is a very simple way to make arrays type hinted!

$list = new UserCollection(new User());

OK

$list = new UserCollection('User');

Will throw:

Vistik\Exception\InvalidTypeException: Item (string) 'User' is not a Vistik\Example\User object!

Install

Run composer require vistik/type-hinted-arrays

Use build in Collections for primitives

Out-of-the-box collections for:

  • Booleans
  • Integers
  • Floats
  • Strings
  • Emails
  • Numbers (float or int)

Do I have to create a type for each list? Yes, but

Look how easy it is:

class UserCollection extends TypedCollection{
    protected $type = 'Vistik\Example\User';
}

2 simple steps

  1. Create a Class eg. AccountCollection extend TypedCollection
  2. Just replace protected $type = 'Vistik\Example\User'; with your class

or

If the check is more then just a is_a check, you can overwrite the function: isValidItem($item) in your new Collection class

Features

Related Packages

collector/collector

A collection library inspired by Laravel Collections, built using generators to...

208 15
aimeos/map

Easy and elegant handling of PHP arrays as array-like collection objects similar...

501,427 4,323
jcrowe/type-safe-collection

A thin wrapper around Illuminate/Collection that allows for enforcing type const...

9,218 4
propaganistas/laravel-helper-macros

Useful helpers and macros for Laravel 5.

63 16
webparking/laravel-type-safe-collection

This package provides type-safe extension of the laravel collection, forcing a s...

8,240 37