andileong/collection

此软件包的最新版本(v0.1.1)没有提供许可证信息。

一个简单的数组收集组件

v0.1.1 2022-03-03 15:26 UTC

This package is auto-updated.

Last update: 2024-09-18 09:05:36 UTC


README

我已经使用 Laravel 集合有一段时间了,所以我决定创建一个简单的集合包装器,用于数组的函数,您可以链式调用数组方法。

这真的很像面向对象编程,以下是一些方法(大多数是从 Laravel 中窃取的方法名,但我创建了自定义实现)

  • map
  • filter
  • pluck
  • unique
  • only
  • first
  • last
  • second
  • sum
  • average
  • isEmpty
  • has
  • limit
  • remove
  • sortBy
  • ...

使用多个本地 PHP 数组函数可能不会感觉很面向对象,我认为将其包装起来是一个好主意,作为我的学习实践,主要是受到 Laravel 框架的启发。

如果您正在寻找一个强大的集合实现,请查看 https://github.com/illuminate/collections

使用方法

假设我们有一个用户列表,我们想要获取所有订阅用户,按最新日期排序,并给出最新的 3 条记录,并将用户名转换为大写。

use Andileong\Collection\Collection;

$array = [
            [
                'id' => 1,
                'name' => 'andi fake',
                'is_subscribed' => true,
                'created_at' => '2021-05-07T09:51:51.000000Z',
            ],
            [
                'id' => 2,
                'name' => 'Betty Waters',
                'is_subscribed' => false,
                'created_at' => '2021-04-07T09:51:51.000000Z',
            ],
            [
                'id' => 3,
                'name' => 'Landen Spinka',
                'is_subscribed' => true,
                'created_at' => '2021-01-07T09:51:51.000000Z',
            ],
            [
                'id' => 4,
                'name' => 'Dell Stoltenberg',
                'is_subscribed' => false,
                'created_at' => '2021-09-07T09:51:51.000000Z',
            ],
            [
                'id' => 5,
                'name' => 'Guy Moen PhD',
                'is_subscribed' => true,
                'created_at' => '2021-07-07T09:51:51.000000Z',
            ],
            [
                'id' => 6,
                'name' => 'Ida Swaniawski',
                'is_subscribed' => false,
                'created_at' => '2021-05-07T09:51:51.000000Z',
            ],
            [
                'id' => 7,
                'name' => 'Norberto Baumbach',
                'is_subscribed' => false,
                'created_at' => '2021-06-07T09:51:51.000000Z',
            ],
            [
                'id' => 8,
                'name' => 'McDermott',
                'is_subscribed' => true,
                'created_at' => '2021-08-07T09:51:51.000000Z',
            ],
            [
                'id' => 9,
                'name' => 'Lavon Nitzsche DDS',
                'is_subscribed' => true,
                'created_at' => '2021-06-07T09:51:51.000000Z',
            ],
            [
                'id' => 10,
                'name' => 'Natalie Huel',
                'is_subscribed' => true,
                'created_at' => '2021-01-07T09:51:51.000000Z',
            ],
        ];

//we can do as below chaining call

$collection = Collection::make($array);
$collection = $collection
    ->filter( fn($user) => $user['is_subscribed'] )
    ->map( function ($user) {
        $user['name'] = strtoupper($user['name']);
        return $user;
    })
    ->sortBy('created_at', SORT_DESC)
    ->limit(3);