fg/parkour
Composer 安装命令:
composer require fg/parkour
包简介
A collection of utilities to manipulate arrays.
README 文档
README
A collection of utilities to manipulate arrays.
The aim of this library is to provide a consistent API, unlike the one natively implemented in PHP.
Examples
Using your own functions:
Parkour\Traverse::filter([5, 15, 20], function($value) { return $value > 10; }); // [15, 20]
Using some of the built-in functors:
Parkour\Traverse::filter([5, 15, 20], new Parkour\Functor\Greater(10)); // [15, 20] Parkour\Traverse::map([10, 20], new Parkour\Functor\Multiply(2), 0); // [20, 40] Parkour\Traverse::reduce([10, 20], new Parkour\Functor\Add(), 0); // 30
API
Traverse
use Parkour\Traverse;
each(), map(), mapKeys(), filter(), reject(), reduce(), find(), findKey(), some(), every().
each()
Traverse::each(['foo' => 'bar'], function($value, $key) { echo "$key: $value"; }); // foo: bar
map()
$data = [ 'foo' => 1, 'bar' => 2 ]; Traverse::map($data, function($value, $key) { return $value * 2; }); // [ // 'foo' => 2, // 'bar' => 4 // ]
mapKeys()
$data = [ 'foo' => 1, 'bar' => 2 ]; Traverse::mapKeys($data, function($value, $key) { return strtoupper($key); }); // [ // 'FOO' => 1, // 'BAR' => 2 // ]
filter()
$data = [ 'foo' => true, 'bar' => false ]; Traverse::filter($data, function($value, $key) { return $value === true; }); // [ // 'foo' => true // ]
reject()
$data = [ 'foo' => true, 'bar' => false ]; Traverse::reject($data, function($value, $key) { return $value === true; }); // [ // 'bar' => false // ]
reduce()
Traverse::reduce([1, 2], function($memo, $value, $key) { return $memo + $value; }, 0); // 3
Using built-in functors:
Traverse::reduce([1, 2], new Parkour\Functor\Add(), 0); // 3 Traverse::reduce([2, 2], new Parkour\Functor\Mutiply(), 2); // 8
find()
$data = [ 'foo' => 'PHP', 'bar' => 'JavaScript' ]; Traverse::find($data, function($value, $key) { return $key === 'foo'; }); // 'PHP'
findKey()
$data = [ 'foo' => 'PHP', 'bar' => 'JavaScript' ]; Traverse::findKey($data, function($value, $key) { return $value === 'PHP'; }); // 'foo'
some()
Traverse::some([5, 10, 20], function($value, $key) { return $value > 10; }); // true
Using a built-in functor:
Traverse::some([1, 2], new Parkour\Functor\AlwaysFalse()); // false
every()
Traverse::every([1, 2], function($value, $key) { return $value === 1; }); // false
Using a built-in functor:
Traverse::every([1, 2], new Parkour\Functor\AlwaysTrue()); // true
Transform
use Parkour\Transform;
combine(), normalize(), reindex(), merge().
combine()
$data = [ ['id' => 12, 'name' => 'foo'], ['id' => 37, 'name' => 'bar'] ]; Transform::combine($data, function($row, $key) { yield $row['id'] => $row['name']; }); // [ // 12 => 'foo', // 37 => 'bar' // ]
normalize()
$data = [ 'foo' => 'bar' 'baz' ]; Transform::normalize($data, true); // [ // 'foo' => 'bar', // 'baz' => true // ]
reindex()
$data = ['foo' => 'bar']; Transform::reindex($data, [ 'foo' => 'baz' ]); // [ // 'baz' => 'bar' // ]
merge()
$first = [ 'one' => 1, 'two' => 2, 'three' => [ 'four' => 4, 'five' => 5 ] ]; $second = [ 'two' => 'two', 'three' => [ 'four' => 'four' ] ]; Transform::merge($first, $second); // [ // 'one' => 1, // 'two' => 'two', // 'three' => [ // 'four' => 'four', // 'five' => 5 // ] // ]
Access
use Parkour\Access;
has(), get(), set(), update().
has()
$data = [ 'a' => 'foo', 'b' => [ 'c' => 'bar' ] ]; Access::has($data, 'b.c'); // true Access::has($data, ['b', 'c']); // true
get()
$data = [ 'a' => 'foo', 'b' => [ 'c' => 'bar' ] ]; Access::get($data, 'a'); // 'foo' Access::get($data, 'b.c'); // 'bar' Access::get($data, ['b', 'c']); // 'bar'
set()
$data = [ 'a' => 'foo', 'b' => [ 'c' => 'bar' ] ]; $data = Access::set($data, 'a', 'a'); $data = Access::set($data, 'b.c', 'c'); $data = Access::set($data, ['b', 'd'], 'd'); // [ // 'a' => 'a', // 'b' => [ // 'c' => 'c', // 'd' => 'd' // ] // ]
update()
$data = [ 'a' => 'foo', 'b' => [ 'c' => 'bar' ] ]; $data = Access::update($data, 'a', function($value) { return strtoupper($value); }); $data = Access::update($data, 'b.c', function($value) { return $value . $value; }); $data = Access::update($data, ['b', 'd'], 'd'); // [ // 'a' => 'FOO', // 'b' => [ // 'c' => 'barbar' // ] // ]
Functors
Add,
AlwaysFalse,
AlwaysTrue,
Cunjunct,
Disjunct,
Divide,
Equal,
Greater,
GreaterOrEqual,
Identical,
Identity,
Lower,
LowerOrEqual,
Multiply,
NotEqual,
NotIdentical,
Substract.
The vast majority of these functors can be used in two different ways.
Without any configuration:
$Add = new Parkour\Functor\Add(); Traverse::reduce([10, 20], $Add, 0); // is equivalent to: Traverse::reduce([10, 20], function($memo, $value) { return $memo + $value; }, 0);
Or with a fixed parameter:
$Add = new Parkour\Functor\Add(5); Traverse::map([10, 20], $Add, 0); // is equivalent to: Traverse::map([10, 20], function($value) { return $value + 5; }, 0);
fg/parkour 适用场景与选型建议
fg/parkour 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 498.84k 次下载、GitHub Stars 达 14, 最近一次更新时间为 2014 年 10 月 08 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「array」 「manipulation」 「traversing」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 fg/parkour 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 fg/parkour 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 fg/parkour 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A set of useful PHP classes.
On-demand image manipulation service.
Laravel package for resized.co, the on-demand image manipulation service.
Manipulate images in pure PHP
Pop Directory Component for Pop PHP Framework
Trait providing methods to set class properties with an array.
统计信息
- 总下载量: 498.84k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 15
- 点击次数: 19
- 依赖项目数: 3
- 推荐数: 0
其他信息
- 授权协议: BSD-2-Clause
- 更新时间: 2014-10-08