mkrawczyk/funquery

PHP 函数式编程库

v1.14.2 2024-09-27 10:09 UTC

README

此库允许您以函数式方式对数组进行操作。它类似于 LINQ,但适用于 PHP。

适用于 PHP 7.4 及以上版本。

安装

composer require mkrawczyk/funquery

示例

<?php
use MKrawczyk\FunQuery\FunQuery;
$data=[];//some array of objects
$result=FunQuery::create($data)->filter(fn($x) => $x->a > 5)->sort(fn($x) => $x->b)->map(fn($x) => $x->c)->slice(2,5)->toArray();
?>

结果可以用作迭代器,或转换为数组

<?php
$result=FunQuery::create($data)->filter(fn($x) => $x->a > 5)->sort(fn($x) => $x->b)->map(fn($x) => $x->c)->slice(2,5);
foreach($result as $x){

}
?>

类似于 LINQ,它使用延迟执行

<?php

function generator(){
    for ($i = 0; ; $i++) {
        yield $i;
    }
}

$result=FunQuery::create(generator())->filter(fn($x)=>$x%2==0)->limit(10)->toArray();//it will 

方法

FunQuery::create(array|IteratorAggregateNode|IteratorNode|FunQuery $init)

从数组、生成器或迭代器创建 FunQuery 对象。

$obj1=FunQuery::create([1,2,3,4,5]);
$obj2=FunQuery::create(generator());
$obj3=FunQuery::create(new ArrayIterator([1,2,3,4,5]));

filter(callable(T):bool $fun)

对每个项目执行函数,并且只有当函数返回 true 时才将其发送到输出。

FunQuery::create([1,2,3,4,5])->filter(fn($x) => $x > 2)->toArray() // [3,4,5]

sort(...$funs)

对项目进行排序。如果传递了函数,它将按此函数的结果进行排序。

FunQuery::create([5,2,3,1,4])->sort()->toArray() // [1,2,3,4,5]
FunQuery::create([5,2,3,1,4])->sort(fn($x) => -$x)->toArray() // [5,4,3,2,1]

map(callable(T):T2 $fun)

它保持相同的项数,但每个项都被传递的函数返回的值替换。

FunQuery::create([1,2,3,4,5])->map(fn($x) => $x * 2)->toArray() // [2,4,6,8,10]

first()

返回第一个对象。如果管道为空,则抛出异常。

FunQuery::create([1,2,3,4,5])->first() // 1

firstOrNull()

返回第一个对象或空值,如果管道为空。

```php
FunQuery::create([1,2,3])->firstOrNull() // 1
FunQuery::create([])->firstOrNull() // null
```

skip(int $count = 0)

跳过前 $count 个项。

FunQuery::create([1,2,3,4,5])->skip(2)->toArray() // [3,4,5]

limit(?int $limit = null)

限制项数到 $limit。如果 $limit 为空,则返回所有项。

FunQuery::create([1,2,3,4,5])->limit(2)->toArray() // [1,2]

slice(int $skip = 0, ?int $limit = null)

结合 skip 和 limit。它跳过 $skip 个项,并限制到 $limit 个项。

FunQuery::create([1,2,3,4,5])->slice(1,2)->toArray() // [2,3]

flat()

扁平化数组中的数组。

FunQuery::create([[1,2],[3,4],[5,6]])->flat()->toArray() // [1,2,3,4,5,6]

distinct(callable(T):T2 $fun=null)

删除重复项。如果传递了函数,它将比较此函数的结果。

FunQuery::create([1,2,2,3,3,3])->distinct()->toArray() // [1,2,3]
FunQuery::create([1,2,2,3,3,3])->distinct(fn($x) => $x % 2)->toArray() // [1,2]

toArray()

将 FunQuery 转换为数组。

FunQuery::create([1,2,3,4,5])->toArray() // [1,2,3,4,5]

toAssoc(callable(T):string $keyFun, callable(T):T2 $valueFun)

将 FunQuery 转换为关联数组。如果有重复项,则抛出异常。

FunQuery::create([1,2,3,4,5])->toAssoc(fn($x) => "key$x", fn($x) => $x * 2) // ["key1" => 2, "key2" => 4, "key3" => 6, "key4" => 8, "key5" => 10]

toAssocArrayIgnoreDuplicates(callable(T):string $keyFun, callable(T):T2 $valueFun)

将 FunQuery 转换为关联数组。如果有重复项,则忽略它们。

FunQuery::create([1,2,1,3,4])->toAssocArrayIgnoreDuplicates(fn($x) => "key$x", fn($x) => $x * 2) // ["key1" => 2, "key2" => 4, "key3" => 6, "key4" => 8]

some(callable(T):bool $fun)

如果至少有一个项目符合条件,则返回 true。

FunQuery::create([1,2,3,4,5])->some(fn($x) => $x > 2) // true

every(callable(T):bool $fun)

如果所有项目都符合条件,则返回 true。

FunQuery::create([1,2,3,4,5])->every(fn($x) => $x > 2) // false

count()

返回项数。

FunQuery::create([1,2,3,4,5])->count() // 5

reduce(callable(T,T):T $fun, T $initial)

将项目缩减为单个值。

FunQuery::create([1,2,3,4,5])->reduce(fn($acc, $x) => $acc + $x, 0) // 15

groupBy(callable(T):T2 $fun)

根据函数的结果分组项目。

FunQuery::create([1,2,3,4,5])->groupBy(fn($x) => $x % 2) // [0 => [2,4], 1 => [1,3,5]]

reverse()

反转项。

FunQuery::create([1,2,3,4,5])->reverse() // [5,4,3,2,1]

execute()

执行管道。默认情况下,FunQuery 使用延迟执行,但有时您可能想要强制执行。

$obj = FunQuery::create([1,2,3,4,5])->map(fn($x) => someLongRunningFunction($x))->execute();
$result1=$obj->filter(fn($x) => $x > 2)->toArray();
$result2=$obj->filter(fn($x) => $x < 2)->toArray();

min(callable(T):T2 $fun)

返回最小值。如果传递了函数,它将比较此函数的结果,但返回原始值。

FunQuery::create([1,2,3,4,5])->min() // 1
FunQuery::create([1,2,3,4,5])->min(fn($x) => -$x) // 5

max(callable(T):T2 $fun)

返回最大值。如果传递了函数,它将比较此函数的结果,但返回原始值。

FunQuery::create([1,2,3,4,5])->max() // 5
FunQuery::create([1,2,3,4,5])->max(fn($x) => -$x) // 1

sum(callable(T):T2 $fun)

返回值的总和。如果传递了函数,它将求和此函数的结果。

FunQuery::create([1,2,3,4,5])->sum() // 15
FunQuery::create([1,2,3,4,5])->sum(fn($x) => $x * 2) // 30

avg(callable(T):T2 $fun)

返回值的平均值。如果传递了函数,它将求平均值此函数的结果。

FunQuery::create([1,2,3,4,5])->avg() // 3
FunQuery::create([1,2,3,4,5])->avg(fn($x) => $x * 2) // 6

concat(FunQuery $funQuery)

连接两个 FunQuery 对象。

FunQuery::create([1,2,3])->concat(FunQuery::create([4,5,6]))->toArray() // [1,2,3,4,5,6]