My modest go at getting a little bit of a LINQy feel to PHP.
Examples:
class Person {
public $name, $age;
public function __construct($name, $age) {
$this->name = $name;
$this->age = $age;
}
}
$persons = array(
new Person("peter", 10),
new Person("jane", 14),
new Person("allan", 42),
new Person("lis", 23),
new Person("jimmy", 19),
new Person("ruth", 7)
);
// names of persons above 18 ordered alphabetically
$names = phlinq($persons)
->where(function($p) { return $p->age > 18; })
->select(function($p) { return $p->name; })
->order()
;
var_dump($names->toArray());
/*
array(3) {
[0]=>
string(4) "allan"
[1]=>
string(5) "jimmy"
[2]=>
string(3) "lis"
}
*/
// same names but ordered by age (youngest first)
$names = phlinq($persons)
->where(function($p) { return $p->age > 18; })
->orderBy(function($p) { return $p->age; })
->select(function($p) { return $p->name; })
;
var_dump($names->toArray());
/*
array(3) {
[0]=>
string(5) "jimmy"
[1]=>
string(3) "lis"
[2]=>
string(4) "allan"
}
*/
Available Methods:
toArray
- convert to array()count
- count the elements in the resultfirstOrNull
- get the first element or null if none is presentfirst
- get the first element, throws exception on failure to find anylastOrNull
- get last element or null if none is presentlast
- get last element, throws exception on failure to find anysingleOrNull
- get a single element based on a condition or null if no element mathcessingle
- get a single element based on a condition, throws an exception on failure to find anywhere
- filter elements based on conditionselect
- select values from the elementsorder
- order the elementsorderBy
- order the elements by a given valueorderDesc
- like order but, descendingorderByDesc
- like orderBy, but descending