selvinortiz / collective
基于原生数组的简单集合实现
0.5.0
2016-06-16 05:01 UTC
Requires
- php: >=5.4.0
Requires (Dev)
- phpunit/phpunit: ^4.8
README
描述
Collective 是一个轻量级的库,它允许您以更灵活、更优雅的方式与原生数组交互。它受到 Laravel Collections 的启发,并专注于性能。
要求
- PHP 5.5+
- Composer 和 selvinortiz/collective
安装
composer require selvinortiz/collective
测试
sh spec.sh
使用
// 1. Create a new instance of Collective // 2. Give it an (optional) input array // 3. Call methods on it $input = ['name' => 'Collective', 'release' => 'alpha']; (new Collective($input))->get('name'); // 'Collective'
参考
get($key, $default = null)
$input = [ 'user' => [ 'name' => 'Brad', 'salary' => '100000' ] ]; (new Collective($input))->get('users.name'); // 'Brad'
set($key, $value)
(new Collection())->set('user.name', 'Matt')->toArray(); // ['user' => ['name' => 'Matt']]
count()
$input = [0, 1, 2, 3, 4, 5]; (new Collective($input))->count(); // 6
first(callable $callback = null, $default = null)
$input = [128, 256, 512, 'Brad', 'Brandon', 'Matt']; $collective = new Collective($input); $collective->first(); // 128 $collective->first(function($value) { return strpos($value, 'Bra') !== false; }); // Brad
last(callable $callback = null, $default = null)
$input = [128, 256, 512 'Brad', 'Brandon', 'Matt']; $collective = new Collective($input); $collective->last(); // 'Matt' $collective->last(function($value) { return strpos($value, 'Bra') !== false; }); // Brandon
map(callable $callback, array $default = [])
将您的 callable 应用于集合中的每个项目
$input = [128, 256, 512 'Brad', 'Brandon', 'Matt']; (new Collective($input))->map(function($value) { return is_string($value) ? '- '.$value : $value; })->toArray(); // 128, 256, 512, '- Brad', '- Brandon', '- Matt'
filter(callable $callback = null, array $default = null)
使用您自己的 callable 过滤集合中的每个项目
$input = [128, 256, 512 'Brad', 'Brandon', 'Matt']; (new Collective($input))->filter(function($value) { return is_numeric($value); })->toArray(); // 128, 256, 512
reduce(callable $callback, $initial = null)
将集合减少到单个值
$input = [ ['name' => 'Brad', 'salary' => 100000, 'type' => 'yearly'], ['name' => 'Brandon', 'salary' => 250000, 'type' => 'yearly'] ]; $callback = function ($value, $carry) { return $carry + $value['salary']; }; (new Collective($input))->reduce($callback); // 350000
reverse()
(new Collective([128, 256, 512]))->reverse()->toArray(); // [512, 256, 128]
flatten($depth = INF)
$input = [ 'level1' => [ 'name' => 'Level 1', 'level2' => [ 'name' => 'Level 2', 'level3' => [ 'name' => 'Level 3' ] ] ] ]; (new Collective($input))->flatten()->toArray(); // ['Level 1', 'Level 2', 'Level 3']
then(callable $callback)
链式调用未由 collection 定义的函数,而不中断 pipe
function filterToStrings($collective) { return $collective->filter(function ($value) { return is_string($value); }); } function fourCharsOnly($collective) { return $collective->filter(function ($value) { return strlen($value) == 4; }); } $collective->then('filterToStrings')->then('filterToLength')->toArray(); // 'Brad', 'Matt'
许可证
Collective 是开源软件,许可协议为 MIT 许可证