amber/collection

用于处理数组的包装类。

v0.6.9-beta 2020-01-20 16:01 UTC

README

GitHub last commit Latest Stable Version Latest Beta Version PHP from Packagist Build Status Coverage Status CodeFactor Total Downloads GitHub

集合

数据结构作为数组的替代品。

用法

使用 composer

composer require amber/collection

在您的代码中

Use Amber\Collection\Collection;
  1. 实例化一个空的集合
$collection = new Collection();
  1. 实例化一个数组集合
$array = ['key' => 'value'];

$collection = new Collection($array);
  1. 或使用静态工厂方法 make()
$collection = Collection::make();

...

$array = ['key' => 'value'];

$collection = Collection::make($array);

可用方法

has()

判断一个项目是否存在于集合中。

has(string $key) : boolean

$collection->set('name', 'Amber');

$collection->has('name'); // returns true.

$collection->has('other'); // returns false.

contains()

has 的别名。

contains(string $key) : boolean

hasNot()

判断一个项目是否不存在于集合中。

hasNot(string $key) : boolean

$collection->set('name', 'Amber');

$collection->hasNot('name'); // returns false.

$collection->hasNot('other'); // returns true.

set()

设置或更新集合中的一个项目。

set(string $key, mixed $value) : void

$collection->set('name', 'Amber');

$collection->has('name'); // returns true.

put()

set 的别名。

put(string $key, mixed $value) : void

add()

将一个新项目添加到集合中。

add(string $key, mixed $value) : boolean

$collection->add('name', 'Amber'); // returns true.

$collection->add('name', 'Amber'); // returns false, since the item already exists.

insert()

add 的别名。

insert(string $key, mixed $value) : boolean

update()

更新集合中的一个现有项目。

update(string $key, mixed $value) : boolean

$collection->update('name', 'Amber'); // returns false, since the item doesn't exists in the collecction

$collection->set('name', 'Amber');

$collection->update('name', 'Collection'); // returns true.

push()

在集合的末尾设置一个新的项目。

push(mixed $value) : void

$collection->push('apple');
$collection->push('orange');
$collection->push('grapes');

$collection->all(); // returns ['apple', 'orange', 'grapes']

pushTo()

将一个新项目推送到集合中的某个项目末尾。

pushTo(string $key, mixed $value) : boolean

$collection->set('colors', []); // Sets an empty array
$collection->pushTo('colors', 'red');
$collection->pushTo('colors', 'blue');
$collection->pushTo('colors', 'green');

$collection->get('colors'); // returns ['red', 'blue', 'green']

get()

从集合中获取一个项目。

get(string $key) : mixed|void

$collection->set('name', 'Amber');

$collection->get('name'); // returns 'Amber'

find()

get 的别名。

find(string $key) : mixed

first()

返回集合的第一个元素。

first() : mixed

$collection->push('apple');
$collection->push('orange');
$collection->push('grapes');

$collection->first(); // returns 'apple'

last()

返回集合的最后一个元素。

last() : mixed

$collection->push('apple');
$collection->push('orange');
$collection->push('grapes');

$collection->last(); // returns 'grapes'

remove()

删除并从集合中检索一个项目。

remove(string $key) : mixed

$collection->set('name', 'Amber');

$name = $collection->remove('name');

$collection->has('name'); // returns false

echo $name; // prints 'Amber'

delete()

从集合中删除一个项目。

delete(string $key) : boolean

$collection->set('name', 'Amber');

$collection->delete('name'); // returns true

$collection->delete('name'); // returns false, since the items was aleady deleted

map()

遍历集合,并将每个值传递给给定的回调函数。

map(\Closure $callback) : \Amber\Collection\Base\Collection

$collection->push('apple');
$collection->push('orange');
$collection->push('grapes');

$maped = $collection->map(function ($value) {
    return ucfirst($value);
});

$maped->all(); // returns ['Apple', 'Orange', 'Grapes']

filter()

返回一个使用用户定义函数过滤的新集合。

filter(\Closure $callback) : \Amber\Collection\Base\Collection

$collection->push('apple');
$collection->push('orange');
$collection->push('grapes');

$filtered = $collection->filter(function ($value) {
    return $value == 'apple';
});

$filtered->all(); // returns ['apple']

sort()

返回一个使用用户定义的比较函数排序的新集合。

sort(\Closure $callback) : \Amber\Collection\Base\Collection

$collection->push(3);
$collection->push(5);
$collection->push(2);

$sorted = $collection->filter(function ($a, $b) {
    return $a <=> $b;
});

$sorted->all(); // returns [2, 3, 5]

reverse()

返回一个新反转的集合。

reverse() : \Amber\Collection\Base\Collection

$collection->push(1);
$collection->push(2);
$collection->push(3);

$reversed = $collection->reverse();

$reversed->all() // returns [3, 2, 1]

merge()

返回一个与一个或多个数组合并的新集合。

merge(array $array) : \Amber\Collection\Base\Collection

$collection->push(1);
$collection->push(2);
$collection->push(3);

$collection->all(); // returns [1, 2, 3]

$array = [4, 5, 6];

$merged = $collection->merge($array);

$merged->all() // returns [1, 2, 3, 4, 5, 6]

chunk()

将数组分割成块。

chunk(integer $size, boolean $preserve_keys = false) : \Amber\Collection\Base\Collection

column()

返回单个列的值。

column(string $column) : \Amber\Collection\Base\Collection

$collection->push(['id' => '1', 'color' => 'red', 'name' => 'Fire']);
$collection->push(['id' => '2', 'color' => 'blue', 'name' => 'Sea']);
$collection->push(['id' => '3', 'color' => 'green', 'name' => 'Forest']);

$colors = $collection->column('color');

$colors->all(); // returns ['red', 'blue', 'green']

flip()

交换所有键与它们的关联值。

flip() : \Amber\Collection\Base\Collection

$collection->set('color' => 'red');

$fliped = $collection->flip();

$fliped->all(); // returns ['red' => 'color']

setMultiple()

设置或更新集合中的多个项目,并在成功时返回 true。

setMultiple(array $array) : void

getMultiple()

从集合中获取多个项目。

getMultiple(array $array) : mixed

hasMultiple()

检查集合中是否包含多个项目。

hasMultiple(array $array) : boolean

select()

返回包含指定列中项目的新的 Collection。

select(array|string $columns) : \Amber\Collection\Base\Collection

$collection->push(['id' => '1', 'color' => 'red', 'name' => 'Fire']);
$collection->push(['id' => '2', 'color' => 'blue', 'name' => 'Sea']);
$collection->push(['id' => '3', 'color' => 'green', 'name' => 'Forest']);

$items = $collection->select('id', 'color');

$items->all(); // returns [[1, 'red'], [2, 'blue'], [3, 'green']]

where()

返回包含指定列中等于指定值的项目的新的 Collection。

where(string $column, mixed $value) : \Amber\Collection\Base\Collection

$collection->push(['id' => '1', 'color' => 'red', 'name' => 'Fire']);
$collection->push(['id' => '2', 'color' => 'blue', 'name' => 'Sea']);
$collection->push(['id' => '3', 'color' => 'green', 'name' => 'Forest']);

$filtered = $collection->where('color', 'red');

$filtered->all(); // returns ['id' => '1', 'color' => 'red', 'name' => 'Fire']

whereNot()

返回包含指定列中不等于指定值的项目的新的 Collection。

whereNot(string $column, mixed $value) : \Amber\Collection\Base\Collection

$collection->push(['id' => '1', 'color' => 'red', 'name' => 'Fire']);
$collection->push(['id' => '2', 'color' => 'blue', 'name' => 'Sea']);
$collection->push(['id' => '3', 'color' => 'green', 'name' => 'Forest']);

$filtered = $collection->whereNot('color', 'red');

$filtered->all();
// returns [
    ['id' => '2', 'color' => 'blue', 'name' => 'Sea'],
    ['id' => '3', 'color' => 'green', 'name' => 'Forest']
]

whereIn()

返回包含指定列中等于指定值(或值数组)的项目的新的 Collection。

whereIn(string $column, array $values = array()) : \Amber\Collection\Base\Collection

$collection->push(['id' => '1', 'color' => 'red', 'name' => 'Fire']);
$collection->push(['id' => '2', 'color' => 'blue', 'name' => 'Sea']);
$collection->push(['id' => '3', 'color' => 'green', 'name' => 'Forest']);

$filtered = $collection->whereIn('color', ['blue', 'green']);

$filtered->all();
// returns [
    ['id' => '2', 'color' => 'blue', 'name' => 'Sea'],
    ['id' => '3', 'color' => 'green', 'name' => 'Forest']
]

whereNotIn()

返回包含指定列中不等于指定值(或值数组)的项目的新的 Collection。

whereNotIn(string $column, array $values = array()) : \Amber\Collection\Base\Collection

$collection->push(['id' => '1', 'color' => 'red', 'name' => 'Fire']);
$collection->push(['id' => '2', 'color' => 'blue', 'name' => 'Sea']);
$collection->push(['id' => '3', 'color' => 'green', 'name' => 'Forest']);

$filtered = $collection->whereNotIn('color', ['blue', 'green']);

$filtered->all(); // returns ['id' => '1', 'color' => 'red', 'name' => 'Fire']

orderBy()

返回按指定列排序的项目的新的 Collection。

orderBy(string $column, string $order = 'ASC') : \Amber\Collection\Base\Collection

$collection->push(['id' => '1', 'color' => 'red', 'name' => 'Fire']);
$collection->push(['id' => '2', 'color' => 'blue', 'name' => 'Sea']);
$collection->push(['id' => '3', 'color' => 'green', 'name' => 'Forest']);

$asc = $collection->orderBy('color');

$asc->all();
// returns [
    ['id' => '2', 'color' => 'blue', 'name' => 'Sea'],
    ['id' => '3', 'color' => 'green', 'name' => 'Forest'],
    ['id' => '1', 'color' => 'red', 'name' => 'Fire']
]

$desc = $collection->orderBy('color', 'desc'); // The $order param is not case sensitive.

$desc->all();
// returns [
    ['id' => '1', 'color' => 'red', 'name' => 'Fire'],
    ['id' => '3', 'color' => 'green', 'name' => 'Forest'],
    ['id' => '2', 'color' => 'blue', 'name' => 'Sea']   
]

groupBy()

返回按指定列分组的新的 Collection。

groupBy(string $column) : \Amber\Collection\Base\Collection

$collection->push(['id' => '1', 'color' => 'red', 'name' => 'Fire']);
$collection->push(['id' => '2', 'color' => 'blue', 'name' => 'Sea']);
$collection->push(['id' => '3', 'color' => 'green', 'name' => 'Forest']);

$grouped = $collection->groupBy('color');

$grouped->all();
// returns [
    'red' => ['id' => '1', 'color' => 'red', 'name' => 'Fire'],
    'blue' => ['id' => '2', 'color' => 'blue', 'name' => 'Sea'],
    'green' => ['id' => '3', 'color' => 'green', 'name' => 'Forest']
]

join()

返回通过指定列连接的新的 Collection。

join(array $array, string $pkey, string $fkey) : \Amber\Collection\Base\Collection

sum()

计算集合中值的总和。

sum(string $column = null) : integer

$collection->push(2);
$collection->push(5);
$collection->push(3);

$collection->sum(); // returns 10

或者可以计算特定列的总和

$collection->push(['id' => 1, 'name' => 'red', 'quantity' => 2]);
$collection->push(['id' => 2, 'name' => 'blue', 'quantity' => 5]);
$collection->push(['id' => 3, 'name' => 'green', 'quantity' => 3]);

$collection->sum('quantity'); // returns 10

firstOrNew()

获取 Collection 的第一个项目或添加并返回一个新的。

firstOrNew(string $key, mixed $value) : mixed

updateOrNew()

更新 Collection 中的项目或添加一个新的。

updateOrNew(string $key, mixed $value) : mixed