mkrawczyk / funquery
Functional programming library for PHP
Installs: 3 019
Dependents: 2
Suggesters: 0
Security: 0
Stars: 1
Watchers: 2
Forks: 0
Open Issues: 0
Requires
- php: >=7.4.0
- ext-json: *
Requires (Dev)
- phpunit/phpunit: ^9.6.7
README
This lib allows you to make operations on arrays in functional style. It is like LINQ, but for PHP.
Designed for PHP 7.4 and higher.
Example
<?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(); ?>
Result can be used as iterator, or converted to Array
<?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){ } ?>
Like LINQ, it uses lazy execution
<?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() Creates FunQuery object from array, generator or iterator.
#filter() Executes function for every item and sends to output only, if function returns true #sort() Sorts items. If function is passed, it sorts by result of this function #map() It keeps the same number of items, but each item is replaced by what was returned by passed function.
#first() Returns first object. Throws exception if pipeline is empty. #firstOrNull() Returns first object or null if pipeline is empty.