pulli / collection-macros
Laravel/Illuminate 集合类的宏
0.2.4
2024-04-08 05:18 UTC
Requires
- php: >=8.2
- illuminate/collections: ^10.0|^11.0
Requires (Dev)
- laravel/pint: ^1.15
- orchestra/testbench: ^8.17
- pestphp/pest: ^2.16
README
安装
您可以通过 composer 引入此包
composer require pulli/collection-macros
宏
- mapToCollectionFrom
- mapToCollection
- positive
- recursiveToArrayFrom
- recursiveToArray
mapToCollectionFrom
静态方法:将所有数组/对象递归地映射到集合对象的集合中,允许嵌套函数调用。
$collection = Collection::mapToCollectionFrom([['test' => 1], 2, 3]); $collection->get(0)->get('test'); // returns 1 // Item has a toArray() public method, then it can be wrapped into a collection like this: $collection = Collection::mapToCollectionFrom([Item(), Item()], true);
positive
返回一个布尔值,表示集合是否包含元素。
Collection::make([1, 2, 3])->positive() // returns true Collection::make()->positive() // returns false
recursiveToArrayFrom
静态方法:类似于 mapToCollectionFrom
,它将所有数组/对象递归地映射到一个数组中。
// Item has a toArray() public method, then it can be wrapped into the collection like this: $array = Collection::recursiveToArrayFrom(['item1' => Item(), 'item2' => Item()]);