vulcan/collections

此包已被废弃,不再维护。作者建议使用axiom/collections包代替。

简单的PHP集合实现

v2.0.1 2021-08-16 05:36 UTC

This package is auto-updated.

Last update: 2021-08-16 05:37:51 UTC


README

Collections是一个简单轻量级的PHP集合类,有助于以面向对象的方式方便地处理数据数组。

安装Axiom Collections

Axiom Collections需要PHP 7.0或更高版本。它未经HHVM或旧版(支持的)PHP版本测试,但本包(除PHPUnit外)中没有任何内容会导致失败。请注意,在这些情况下不保证支持。

建议您使用Composer安装此包。

$ composer require axiom/collections

此包符合PSR-1PSR-2PSR-4规范。如果您发现任何符合性疏忽,请通过pull request发送补丁。

使用Collections

创建集合

创建新的Collection实例有几种选择。

一种是通过直接创建类的实例

$collection = new \Axiom\Collections\Collection([1, 2, 3]);

或者,使用make静态辅助方法

$collection = Collection::make([1, 2, 3]);

方法

在本文档的其余部分,我们将介绍Collection类提供的所有可用方法。所有这些方法都可以串联起来流畅地操作底层的数据数组。最后,几乎在所有情况下,每个方法都将返回一个新的Collections实例,在必要时保留集合的原始副本。

all()

all方法检索集合中的所有项目。

Collection::make([1, 2, 3])->all();

// [1, 2, 3]

count()

count方法返回集合中的项目总数。

$collection = Collection::make([1, 2, 3, 4]);

$collection->count();

// 4

each()

each方法遍历集合中的项目,并将每个项目传递给回调。

$collection = $collection->each(function($item, $key) {
    //
});

要停止遍历项目并退出循环,只需从回调中返回false即可。

$collection = $collection->each(function($item, $key) {
    if ($someCondition === true) {
        return false;
    }
});

exists()

exists方法确定给定的键是否存在于集合中。

$collection = Collection::make(['bot_id' => 1, 'name' => 'Loki']);

$collection->exists('name');

// true

filter()

filter方法使用给定的回调过滤集合,仅保留通过给定真值测试的项目。

$collection = Collection::make([1, 2, 3, 4]);

$filtered = $collection->filter(function($item, $key) {
    return $value > 2;
});

$filtered->all();

// [3, 4]

有关filter的逆操作,请参阅reject方法。

first()

first方法返回集合中的第一个元素。

Collection::make([1, 2, 3, 4])->first();

// 1

flatten()

《flatten` 方法返回一个包含扁平化项数组的新的 Collection 实例。

$collection = Collection::make([
    'name' => 'Kai',
    'profile' => [
        'age' => 28,
        'favorite_games' => ['Mass Effect', 'Oxygen Not Included', 'event[0]']
    ]
]);

$result = $collection->flatten();

$result->all();

// ['Kai', 28, 'Mass Effect', 'Oxygen Not Included', 'event[0]']

get()`

《get` 方法返回给定键的项。

$collection = Collection::make(['foo' => 'bar', 'lorem' => 'ipsum']);

$result = $collection->get('lorem');

// ipsum

groupBy()`

《groupBy` 方法通过回调函数使用关联数组对项进行分组,并返回一个新的 Collection 实例。

$collection = Collection::make([
    'foo' => ['type' => 'foobar'],
    'bar' => ['type' => 'foobar'],
    'lorem' => ['type' => 'lorem ipsum'],
    'ipsum' => ['type' => 'lorem ipsum']
]);

$result = $collection->groupBy(function($item) {
    return $item['type'];
});

$result->all();

//
[
    'foobar' => [
        'foo' => ['type' => 'foobar'],
        'bar' => ['type' => 'foobar'],
    ],
    'lorem ipsum' => [
        'lorem' => ['type' => 'lorem ipsum'],
        'ipsum' => ['type' => 'lorem ipsum']
    ]
]

keys()`

《keys` 方法返回一个包含集合项所有键的新的 Collection 实例。

$collection = Collection::make(['foo' => 'bar', 'lorem' => 'ipsum']);

$result = $collection->keys();

$result->all();

// ['foo', 'lorem']

last()`

《last` 方法返回集合中的最后一个元素。

Collection::make([1, 2, 3, 4])->last();

// 4

map()`

《map` 方法遍历集合,将每个值和键传递给给定的回调函数。回调函数可以自由地修改项并返回它,从而形成一个新的修改后的项集合。

$collection = Collection::make([1, 2, 3, 4, 5]);

$multiplied = $collection->map(function($item, $key) {
    return $item * 2;
});

$multiplied->all();

// [2, 4, 6, 8, 10]

push()`

《push` 方法将项追加到集合的末尾。

$collection = Collection::make([1, 2, 3, 4]);

$collection->push(5);

$collection->all();

// [1, 2, 3, 4, 5]

put()`

《put` 方法在集合中设置给定的键和值。

$collection = Collection::make(['bot_id' => 1, 'name' => 'Odin']);

$collection->put('name', 'Loki');

$collection->all();

// ['bot_id' => 1, 'name' => 'Loki']

reject()`

《reject` 方法使用给定的回调函数过滤集合。如果回调函数返回 `true`,则项应该从结果集合中 移除

$collection = Collection::make([1, 2, 3, 4]);

$filtered = $collection->reject(function($item, $key) {
    return $value > 2;
});

$filtered->all();

// [1, 2]

remove()

《remove` 方法通过其键从集合中移除项。

$collection = Collection::make([0, 1, 2, 3]);

$collection->remove(0);

$collection->all();

// [1, 2, 3]

reverse()`

《reverse` 方法反转集合项。

$collection = Collection::make([4, 1, 2, 3, 5])->reverse()->values()->all();

// [5, 3, 2, 1, 4]
$collection = Collection::make([4, 1, 2, 3, 5])->sort()->reverse()->values()->all();

// [5, 4, 3, 2, 1]

sort()

《sort` 方法按项的值升序排序集合,或通过用户定义的比较函数进行排序。

$data = [4, 1, 2, 3, 5];
$collection = Collection::make($data)->sort()->values()->all();

// [1, 2, 3, 4, 5]
$collection = Collection::make($data)->sort(function($a, $b) {
    if ($a === $b) {
        return 0;
    }

    return ($a < $b) ? -1 : 1;
})->values()->all();

// [1, 2, 3, 4, 5]

sortByKey()

《sortByKey` 方法按项的键升序排序集合,或通过用户定义的比较函数进行排序。

$data = [
    'foo4' => 1,
    'foo2' => 2,
    'foo5' => 3,
    'foo3' => 4,
    'foo1' => 5
];
$collection = Collection::make($data)->sortByKey()->values()->all();

// [5, 2, 4, 1, 3]
$collection = Collection::make($data)->sortByKey(function($a, $b) {
    if ($a === $b) {
        return 0;
    }

    return ($a < $b) ? -1 : 1;
})->values()->all();

// [5, 2, 4, 1, 3]

values()

《values` 方法返回一个包含集合项所有值的新的 Collection 实例。

$collection = Collection::make(['foo' => 'bar', 'lorem' => 'ipsum']);

$result = $collection->values();

$result->all();

// ['bar', 'ipsum']