fg/parkour

一组用于操作数组的工具。

1.1.1 2015-10-03 10:39 UTC

This package is auto-updated.

Last update: 2024-09-24 04:14:53 UTC


README

Build status Scrutinizer Code Quality Code Coverage

一组用于操作数组的工具。

该库的目的是提供一个一致的API,与PHP中默认实现的API不同。

示例

使用您自己的函数

Parkour\Traverse::filter([5, 15, 20], function($value) {
	return $value > 10;
});

// [15, 20]

使用一些内置的函数式编程器

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

遍历

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

使用内置的函数式编程器

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

使用内置的函数式编程器

Traverse::some([1, 2], new Parkour\Functor\AlwaysFalse()); // false

every()

Traverse::every([1, 2], function($value, $key) {
	return $value === 1;
});

// false

使用内置的函数式编程器

Traverse::every([1, 2], new Parkour\Functor\AlwaysTrue()); // true

转换

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
// 	]
// ]

访问

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'
// 	]
// ]

函数式编程器

Add, AlwaysFalse, AlwaysTrue, Cunjunct, Disjunct, Divide, Equal, Greater, GreaterOrEqual, Identical, Identity, Lower, LowerOrEqual, Multiply, NotEqual, NotIdentical, Substract.

这些函数式编程器中的绝大多数可以有两种不同的使用方式。

无需配置

$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);

或使用固定参数

$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);