pckg/集合

实现更人性化的集合处理

dev-next-8.0 2022-12-09 19:32 UTC

This package is auto-updated.

Last update: 2024-09-15 19:57:44 UTC


README

Codacy Badge

Build status

Pckg/collection 提供了一种处理项目/数组和字符串集合的方法。它与 pckg/skeletonpckg/frameworkpckg/database 兼容得非常好。

安装

对于独立使用,只需在 composer 中要求 pckg/collection。

$ composer require pckg/collection

对于高级使用,请参阅 pckg/skeleton。

$ composer install pckg/skeleton .

依赖关系

包不依赖于其他任何包。

测试

可以使用 codeception 运行测试。

$ cp ./codeception.sample.yml ./codeception.yml
$ codecept run

简单使用

// create a new Collection

$collection = new Collection();

// push items to the last position of the collection

// push some single items

$collection->push('foo');
$collection->push('bar');

// push a whole array

$collection->pushArray(['first', 'second']);

// pop last item and remove it from collection

$item = $collection->pop();

// add an item at the first position

$collection->prepend('prepended');

// retrieve and remove first item

$item = $collection->shift();

// retrieve first and last items keeping then in colection

$collection->first();
$collection->last();

回调

has

测试集合是否包含一组值

$collection = new Collection([
                                 'foo' => [
                                     'id'    => 1,
                                     'title' => 'baz',
                                 ],
                                 'bar' => [
                                     'id'    => 2,
                                     'title' => 'unknown',
                                 ],
                             ]);

$collection->has(['id' => 1, 'title' => 'baz'])); // return true

$collection->has(['id' => 2, 'title' => 'baz'])); // return false

过滤条目

返回所有从匿名函数返回 true 的条目。

$filtered = $collection->filter(function($item) {
    return $item['id'] == 1;
});

var_dump($filtered->all()); // ['foo' => ['id' => 1, 'title' => 'baz']]

设置键

通过某些数组项的值设置条目键。在以下示例中,所有键都通过 title 内部数组条目设置。

$keyed = $collection->keyBy('title');

var_dump($keyed->all()); 

/**
* [
*     'baz'     => [
*         'id'    => 1,
*         'title' => 'baz',
*     ],
*     'unknown' => [
*         'id'    => 2,
*         'title' => 'unknown',
*     ],
* ]
**/

根据满足的检查获取第一个项目

返回满足逻辑测试的第一个项目。

$first = $collection->first(function($item) { 
   return $item['id'] > 1; 
});

var_dump($first); //['id' => 2, 'title' => 'unknown']

根据内部值映射键

根据内部键将每个条目的键映射到内部值。

$mapped = $collection->map('title');

var_dump($mapped->all()); // ['foo' => 'baz', 'bar' => 'unknown']

键和值

$collection = new Collection(['foo' => 'bar', 'baz' => 'test', 'john' => 'doe', 'jane' => 'name']);

// get a collection with a removed entry based on it key

$removedOne = $collection->removeKeys('baz');  // ['foo' => 'bar', 'john' => 'doe', 'jane' => 'name']

// get a collection with several entries removed based on they keys
        
$removedMultiple = $collection->removeKeys(['baz', 'john']); // ['foo' => 'bar', 'jane' => 'name']
        
// get a collection with several entries removed based on they values
        
$removedValues = $collection->removeValues(['bar', 'test']); // ['john' => 'doe', 'jane' => 'name']

// get all keys of a collection

$keys = $collection->keys(); // ['foo', 'baz', 'john', 'jane']
        
// get all values of a collection

$values = $collection->values(); // ['bar', 'test', 'doe', 'name']
 
// test if a key exist

$collection->hasKey('baz'); // true
$collection->hasKey('bz'); // false

// retrieve the value of a key

$collection->getKey('baz'); // 'test'

操作

切片集合

$collection = new Collection(['foo', 'bar', 'baz', '', ' untrimmed ']);

$sliced = $collection->slice(1, 2); // ['bar', 'baz']

分块

按块分块。

$chunked = $collection->chunk(2);

/**
* [
*    ['foo', 'bar'], 
*    ['baz', ''], 
*    [' untrimmed ']
* 
**/

扁平化

$flatten = $chunked->flat(); // ['foo', 'bar', 'baz', '', ' untrimmed ']

裁剪

$trimmed = $collection->trim(); // ['foo', 'bar', 'baz', '', 'untrimmed']

乘以 x

通过传递的数字复制项目。

$multiplied = $collection->multiply(2);

/**
* [
*    'foo',
*    'bar',
*    'baz',
*    '',
*    ' untrimmed ',
*    'foo',
*    'bar',
*    'baz',
*    '',
*    ' untrimmed ',
* ]
**/

unique

返回一个没有重复值的集合。

$unique = $multiplied->unique(); // ['foo', 'bar', 'baz', '', 'untrimmed']

Implode

获取一个包含所有集合值且由 imploded 字符分隔的字符串。

$imploded = $collection->implode(' ', ' - '); // 'foo bar baz  -  untrimmed '

删除空值

  
$nonEmpty = $collection->removeEmpty(); // ['foo', 'bar', 'baz', ' untrimmed ']

数学

$collection = new Collection([2, 1, 13, 3, 1, 5, 21, 8]);

$sum = $collection->sum(); // 54

$avg = $collection->avg(); // 6.75
        
$min = $collection->min(); // 1
        
$max = $collection->max(); // 21