typed collections
v2.0.2
这是使数组类型提示的非常简单的方法!
$list = new UserCollection(new User());
好的
$list = new UserCollection('User');
会抛出:
VistikExceptionInvalidTypeException: Item (string) 'User' is not a VistikExampleUser object!
运行composer require vistik/typed-collections
开箱即用的集合:
它们可以使用开箱即用的 laraval 风格助手创建:
bCollect([true, false]) - 创建 BoolCollectioniCollect([1, 2, 3, 4]) - 创建 IntegerCollectionfCollect([3.14, 22/7, 6.66]) - 创建 FloatCollectionsCollect(['hello', 'world'']) - 创建 StringCollectioneCollect(['[email protected]', '[email protected]']) - 创建 EmailCollectionnCollect([1, 2, 3, 4, 3.14, INF]) - 创建 NumberCollection看看它是多么容易:
use PhpsafariExampleUser;
class UserCollection extends TypedCollection{
protected $type = User::class;
}
2个简单步骤
AccountCollection扩展TypedCollectionprotected $type = 'VistikExampleUser';和你的班级或者
如果检查不仅仅是is_a检查,您可以在新的 Collection 类中覆盖该函数: isValidItem($item)