vistik / type-hinted-arrays
本包最新版本(v2.0.0)没有提供许可信息。
在PHP中创建类型化集合的简单方法 - 基于 Illuminate\Support\Collection 构建
v2.0.0
2017-02-11 20:26 UTC
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-09-14 15:52:55 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/type-hinted-arrays
使用内置的集合对原始数据类型
现成的集合用于
- 布尔值
- 整数
- 浮点数
- 字符串
- 电子邮件
- 数字(浮点数或整数)
我必须为每个列表创建一个类型吗?是的,但是
看看这有多简单
class UserCollection extends TypedCollection{
protected $type = 'Vistik\Example\User';
}
2个简单步骤
- 创建一个类,例如
AccountCollection
继承TypedCollection
- 只需将
protected $type = 'Vistik\Example\User';
替换为你的类
或者
如果检查不仅仅是 is_a
检查,你可以在你的新集合类中重写函数:isValidItem($item)
特性
- 基于 Illuminate\Support\Collection
- 非常简单实现自定义集合