typed collections
v2.0.2
This is a very simple way to make arrays type hinted!
$list = new UserCollection(new User());
OK
$list = new UserCollection('User');
Will throw:
VistikExceptionInvalidTypeException: Item (string) 'User' is not a VistikExampleUser object!
Run composer require vistik/typed-collections
Out-of-the-box collections for:
They can be created with out-of-the-box laraval style helpers:
bCollect([true, false]) - To create a BoolCollectioniCollect([1, 2, 3, 4]) - To create a IntegerCollectionfCollect([3.14, 22/7, 6.66]) - To create a FloatCollectionsCollect(['hello', 'world'']) - To create a StringCollectioneCollect(['[email protected]', '[email protected]']) - To create a EmailCollectionnCollect([1, 2, 3, 4, 3.14, INF]) - To create a NumberCollectionLook how easy it is:
use PhpsafariExampleUser;
class UserCollection extends TypedCollection{
protected $type = User::class;
}
2 simple steps
AccountCollection extend TypedCollectionprotected $type = 'VistikExampleUser'; with your classor
If the check is more then just a is_a check, you can overwrite the function: isValidItem($item) in your new Collection class