stk2k / collection
简单的PHP集合类库
0.1.3
2021-06-05 19:12 UTC
Requires
- php: >=7.2
- ext-json: *
Requires (Dev)
- php-coveralls/php-coveralls: ^2.0
- phpunit/phpunit: ^8.5.15
This package is auto-updated.
Last update: 2024-09-06 01:57:41 UTC
README
描述
简单的PHP集合类库
特性
- 可排序的(ArrayList/Queue/Stack/Vector)
支持的数据结构
- ArrayList
- Collection
- 属性(支持对字符串/整数/浮点数/布尔值的层次访问)
- Queue
- Set
- Stack
- Vector
演示
Collection
$data = ['red', 'green', 'blue']; echo 'iterate:' . PHP_EOL; $output = []; foreach(new Collection($data) as $item){ $output[] = $item; } echo ' ' . implode(',', $output) . PHP_EOL; // red,green,blue echo 'join:' . PHP_EOL; echo ' ' . (new Collection($data))->join() . PHP_EOL; // red,green,blue echo 'replace:' . PHP_EOL; echo ' ' . (new Collection($data))->replace('green', 'yellow')->join() . PHP_EOL; // red,yellow,blue echo 'each:' . PHP_EOL; (new Collection($data))->each(function($item){ echo "[$item],"; }); // [red],[green],[blue], echo 'map:' . PHP_EOL; echo ' ' . (new Collection($data))->map(function($item){ return "[$item]"; })->join() . PHP_EOL; // [red],[green],[blue] echo 'reduce:' . PHP_EOL; echo ' ' . (new Collection($data))->reduce(function($tmp,$item){ return $tmp + strlen($item); }) . PHP_EOL; // 12
ArrayList
$data = ['red', 'green', 'blue']; echo 'iterate:' . PHP_EOL; $output = []; foreach(new ArrayList($data) as $item){ $output[] = $item; } echo ' ' . implode(',', $output) . PHP_EOL; // red,green,blue echo 'join:' . PHP_EOL; echo ' ' . (new ArrayList($data))->join() . PHP_EOL; // red,green,blue echo 'first:' . PHP_EOL; echo ' ' . (new ArrayList($data))->first() . PHP_EOL; // red echo 'last:' . PHP_EOL; echo ' ' . (new ArrayList($data))->last() . PHP_EOL; // blue echo 'reverse:' . PHP_EOL; echo ' ' . (new ArrayList($data))->reverse()->join() . PHP_EOL; // blue,green,red echo 'replace then reverse:' . PHP_EOL; echo ' ' . (new ArrayList($data))->replace('green', 'yellow')->reverse()->join() . PHP_EOL; // blue,yellow,red echo 'shift:' . PHP_EOL; echo ' ' . (new ArrayList($data))->shift($item)->join() . PHP_EOL; // green,blue echo 'unshift:' . PHP_EOL; echo ' ' . (new ArrayList($data))->unshift('yellow')->join() . PHP_EOL; // yellow,red,green,blue echo 'push:' . PHP_EOL; echo ' ' . (new ArrayList($data))->push('yellow')->join() . PHP_EOL; // red,green,blue,yellow echo 'pop:' . PHP_EOL; echo ' ' . (new ArrayList($data))->pop($item)->join() . PHP_EOL; // red,green echo 'sort:' . PHP_EOL; echo ' ' . (new ArrayList($data))->sort()->join() . PHP_EOL; // blue,green,red
Vector
$data = ['red', 'green', 'blue']; echo 'iterate:' . PHP_EOL; $output = []; foreach(new Vector($data) as $item){ $output[] = $item; } echo ' ' . implode(',', $output) . PHP_EOL; // red,green,blue echo 'join:' . PHP_EOL; echo ' ' . (new Vector($data))->join() . PHP_EOL; // red,green,blue echo 'first:' . PHP_EOL; echo ' ' . (new Vector($data))->first() . PHP_EOL; // red echo 'last:' . PHP_EOL; echo ' ' . (new Vector($data))->last() . PHP_EOL; // blue echo 'reverse:' . PHP_EOL; echo ' ' . (new Vector($data))->reverse()->join() . PHP_EOL; // blue,green,red echo 'replace then reverse:' . PHP_EOL; echo ' ' . (new Vector($data))->replace('green', 'yellow')->reverse()->join() . PHP_EOL; // blue,yellow,red echo 'shift:' . PHP_EOL; echo ' ' . (new Vector($data))->shift($item)->join() . PHP_EOL; // green,blue echo 'unshift:' . PHP_EOL; echo ' ' . (new Vector($data))->unshift('yellow')->join() . PHP_EOL; // yellow,red,green,blue echo 'push:' . PHP_EOL; echo ' ' . (new Vector($data))->push('yellow')->join() . PHP_EOL; // red,green,blue,yellow echo 'pop:' . PHP_EOL; echo ' ' . (new Vector($data))->pop($item)->join() . PHP_EOL; // red,green echo 'sort:' . PHP_EOL; echo ' ' . (new Vector($data))->sort()->join() . PHP_EOL; // blue,green,red
Stack
$data = ['red', 'green', 'blue']; echo 'iterate:' . PHP_EOL; $output = []; foreach(new Stack($data) as $item){ $output[] = $item; } echo ' ' . implode(',', $output) . PHP_EOL; // red,green,blue echo 'join:' . PHP_EOL; echo ' ' . (new Stack($data))->join() . PHP_EOL; // red,green,blue echo 'peek:' . PHP_EOL; echo ' ' . (new Stack($data))->peek() . PHP_EOL; // red echo 'reverse:' . PHP_EOL; echo ' ' . (new Stack($data))->reverse()->join() . PHP_EOL; // blue,green,red echo 'replace then reverse:' . PHP_EOL; echo ' ' . (new Stack($data))->replace('green', 'yellow')->reverse()->join() . PHP_EOL; // blue,yellow,red echo 'push:' . PHP_EOL; echo ' ' . (new Stack($data))->push('yellow')->join() . PHP_EOL; // red,green,blue,yellow echo 'pop:' . PHP_EOL; echo ' ' . (new Stack($data))->pop($item)->join() . PHP_EOL; // red,green echo 'sort:' . PHP_EOL; echo ' ' . (new Stack($data))->sort()->join() . PHP_EOL; // blue,green,red
HashMap
$data = ['name' => 'David', 'age' => 21, 'height' => 172.2]; echo 'iterate:' . PHP_EOL; $output = []; foreach(new HashMap($data) as $item){ $output[] = $item; } echo ' ' . implode(',', $output) . PHP_EOL; // David,21,172.2 echo 'join:' . PHP_EOL; echo ' ' . (new HashMap($data))->join() . PHP_EOL; // David,21,172.2
Set
$data = ['red', 'green', 'blue']; echo 'iterate:' . PHP_EOL; $output = []; foreach(new Set($data) as $item){ $output[] = $item; } echo ' ' . implode(',', $output) . PHP_EOL; // red,green,blue echo 'join:' . PHP_EOL; echo ' ' . (new Set($data))->join() . PHP_EOL; // red,green,blue
需求
PHP 7.2 或更高版本
安装 stk2k/collection
推荐通过 Composer 安装 stk2k/collection。
composer require stk2k/collection
安装后,您需要引入Composer的自动加载器
require 'vendor/autoload.php';
许可证
此库使用MIT许可证。
作者
免责声明
此软件无任何保修。
我们不对此软件使用造成的任何结果负责。
请自行负责。