judahnator / iterable-collection
用于操作可迭代和生成器的流畅接口。
v1.4.0
2022-04-10 18:58 UTC
Requires
- php: >=7.4
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.16.2
- phpstan/phpstan: ^0.12.54
- phpunit/phpunit: ^9.4
This package is auto-updated.
Last update: 2024-09-11 07:15:08 UTC
README
代码片段
这个库允许您使用流畅的接口来操作可迭代项。
目标是至少与所有 array_* 函数具有相同的功能,但由于无法回滚生成器的限制,该库略低于此目标。因此,一些函数已被故意排除。
当然,如果您想出一种实现这些函数的好方法,请随时提交拉取请求。
还添加了一些其他函数,这些函数作为原生 array_* 方法不存在。
用法
对象创建
$o = new IterableCollection(['foo' => 'bar']);
// or
$o = IterableCollection::wrap(['foo' => 'bar']);
迭代
foreach (IterableCollection::wrap(['foo' => 'bar']) as $key => $value) {
// use $key and $value
}
检查所有为真
IterableCollection::wrap([true, false, true])->all();
# false
IterableCollection::wrap([true, true, true])->all();
# true
IterableCollection::wrap([1, 2, 'a'])->all('is_numeric');
# false
检查任意一个为真
IterableCollection::wrap([true, false, true])->any();
# true
IterableCollection::wrap(['a', 'b', 'c'])->any(fn ($i) => $i === 'd');
# false
更改键的大小写
$o = IterableCollection::wrap(['Hello' => 'World!']);
$o->changeKeyCase(CASE_LOWER);
# IterableCollection {
# hello: "World!",
# }
$o->changeKeyCase(CASE_UPPER);
# IterableCollection {
# HELLO: "World!",
# }
分块
$o = IterableCollection::wrap(range(1,4));
// No preserve keys (default)
$o->chunk(2);
# IterableCollection {
# 0: [
# 1,
# 2,
# ],
# 1: [
# 3,
# 4,
# ],
# }
// Preserve keys
$o->chunk(2, true);
# IterableCollection {
# 0: [
# 0 => 1,
# 1 => 2,
# ],
# 1: [
# 2 => 3,
# 3 => 4,
# ],
# }
列
$o = IterableCollection::wrap([
['id' => 123, 'name' => 'Judah Wright',],
['id' => 456, 'name' => 'Jane Doe',],
]);
// Without column key (default)
$o->column('name');
# IterableCollection {
# 0: "Judah Wright",
# 1: "Jane Doe",
# }
// With a column key
$o->column('name', 'id');
# IterableCollection {
# 123: "Judah Wright",
# 456: "Jane Doe",
# }
计数
IterableCollection::wrap([1, 2, 3])->count();
# 3
合并
$keys = ['foo', 'bing'];
$values = ['bar', 'baz'];
IterableCollection::wrap($keys)->combine($values);
# IterableCollection {
# foo: "bar",
# bing: "baz",
# }
计数值
IterableCollection::wrap(['foo', 'bar', 'foo', 'baz'])->countValues();
# IterableCollection {
# foo: 2,
# bar: 1,
# baz: 1,
# }
填充键
IterableCollection::wrap(['a', 'b', 'c'])->fillKeys('foo');
# IterableCollection {
# a: "foo",
# b: "foo",
# c: "foo",
# }
过滤
$inputs = [true, false, null, "Hello!"];
// Without a callback (default) returns keeps the "truthy" values
IterableCollection::wrap($inputs)->filter();
# IterableCollection {
# 0: true,
# 3: "Hello!",
# }
// With a callback you can do your own evaluation
IterableCollection::wrap($inputs)->filter(
static fn ($value): bool => !is_null($value)
);
# IterableCollection {
# 0: true,
# 1: false,
# 3: "Hello!",
# }
翻转
IterableCollection::wrap(['key' => 'value'])->flip();
# IterableCollection {
# value: "key",
# }
在...
$o = IterableCollection::wrap(['foo', 'bar']);
$o->in('foo');
# true
$o->in('baz');
# false
键存在
$o = IterableCollection::wrap(['foo' => 'bar', 'bing' => 'baz']);
$o->keyExists('foo');
# true
$o->keyExists('asdf');
# false
键
IterableCollection::wrap(['foo' => 'bar', 'bing' => 'baz'])->keys();
# IterableCollection {
# 0: "foo",
# 1: "bing",
# }
映射
$o = IterableCollection::wrap(['John' => 'Smith', 'Jane' => 'Doe']);
$names = $o->map(
fn ($firstname, $lastname): string => "{$firstname} {$lastname}"
);
# IterableCollection {
# 0: "Smith John",
# 1: "Doe Jane",
# }
减少
$o = IterableCollection::wrap([1, 2, 3]);
$o->reduce(static fn (int $carry, int $item): int => $carry + $item, 0);
# 6
转换为数组
$iterator = new ArrayIterator(['foo' => 'bar']);
print_r(IterableCollection::wrap($iterator)->toArray());
# Array
# (
# [foo] => bar
# )
值
IterableCollection::wrap(['key1' => 'value1', 'key2' => 'value2'])->values();
# IterableCollection {#2519
# 0: "value1",
# 1: "value2",
# }
递归遍历
$tree = [
'limb1' => [
'branch1' => 'leaf1',
'branch2' => 'leaf2',
],
'limb2' => 'leaf3'
];
IterableCollection::wrap($tree)->walkRecursive(
fn ($item, $key, $userdata) => "{$userdata} - {$key} - {$item}",
'tree',
);
# IterableCollection {
# 0: "tree - branch1 - leaf1",
# 1: "tree - branch2 - leaf2",
# 2: "tree - limb2 - leaf3",
# }
遍历
$o = IterableCollection::wrap(['Bears', 'Beets', 'Battlestar Galactica']);
$o->walk(
function (&$item, &$key, int &$userdata): void {
$item = strtoupper($item);
$key = ['a', 'b', 'c'][$userdata];
$userdata++;
},
0,
);
# IterableCollection {
# a: "BEARS",
# b: "BEETS",
# c: "BATTLESTAR GALACTICA",
# }
故意排除的函数
需要输入实现 Iterator 类的项目,例如
需要某种形式的索引、排序或搜索的项目。这些包括
- array_diff_assoc()
- array_diff_key()
- array_diff_uassoc()
- array_diff_ukey()
- array_diff()
- array_intersect_assoc()
- array_intersect_key()
- array_intersect_uassoc()
- array_intersect_ukey()
- array_intersect()
- array_merge()
- array_merge_recursive()
- arsort()
- asort()
- krsort()
- ksort()
- natcasesort()
- natsort()
一些负责创建新列表的函数,例如