pulli/collection-macros

Laravel/Illuminate 集合类的宏

0.2.4 2024-04-08 05:18 UTC

This package is auto-updated.

Last update: 2024-09-08 06:12:16 UTC


README

release packagist license downloads tests

安装

您可以通过 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()]);