vistik / type-hinted-arrays
A simple way to create typed collections in PHP - build on Illuminate\Support\Collection
Installs: 8 891
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 2
Forks: 0
Open Issues: 0
Requires
- php: >=7.0.0
- illuminate/support: ~5.4
- phpmd/phpmd: ^2.6
Requires (Dev)
- phpunit/phpunit: ~6.0
This package is not auto-updated.
Last update: 2024-11-23 16:50:41 UTC
README
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
- Create a Class eg.
AccountCollection
extendTypedCollection
- 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
- Build upon Illuminate\Support\Collection
- Very simple to implement custom Collections