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
擴展TypedCollection
protected $type = 'VistikExampleUser';
和你的班級或者
如果檢查不只是is_a
檢查,您可以在新的 Collection 類別中覆寫該函數: isValidItem($item)