viktor / typed-collections
在PHP中创建类型化集合的简单方法 - 基于 Illuminate\Support\Collection 构建
v2.0.2
2017-07-27 22:24 UTC
Requires
- php: >=7.0.0
- illuminate/support: 5.4.*|5.3.*|5.2.*|5.1.*
- squizlabs/php_codesniffer: *
Requires (Dev)
- phpmd/phpmd: ^2.6
- phpunit/phpunit: ~6.0
This package is not auto-updated.
Last update: 2024-09-14 19:42:24 UTC
README
这是什么?
这是一个制作数组的类型提示非常简单的方法!
$list = new UserCollection(new User());
OK
$list = new UserCollection('User');
将会抛出
Vistik\Exception\InvalidTypeException: Item (string) 'User' is not a Vistik\Example\User 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(['hello@example.org', 'write@me.com'])
- 创建 EmailCollectionnCollect([1, 2, 3, 4, 3.14, INF])
- 创建 NumberCollection
我必须为每个列表创建一个类型吗?是的,但是
看看它有多简单
use Phpsafari\Example\User;
class UserCollection extends TypedCollection{
protected $type = User::class;
}
2个简单步骤
- 创建一个类,例如
AccountCollection
继承TypedCollection
- 只需将
protected $type = 'Vistik\Example\User';
替换为您自己的类
或者
如果检查不仅仅是 is_a
检查,您可以在新的集合类中重写函数: isValidItem($item)
功能
- 基于 Illuminate\Support\Collection
- 自定义集合的实现非常简单