This guide will show you how to filter any kind of target using a simple language.
Here is a summary of what you will have to do:
- instantiate the RulerZ engine
and be sure to include an instance of
\RulerZ\Target\Native\Native
to the compilation targets ; - write a rule ;
- filter your target.
In the following examples, we'll define rules to filter this collection:
$users = [
['pseudo' => 'Joe', 'gender' => 'M', 'points' => 40],
['pseudo' => 'Moe', 'gender' => 'M', 'points' => 20],
['pseudo' => 'Alice', 'gender' => 'F', 'points' => 60],
];
Note: RulerZ will work the same way, whether you want to filter a collection of arrays or a collection of objects.
Let's say that we want to retrieve the female players having at least 30 points. The rule describing these constraints would look like this:
$rule = 'gender = :gender and points > :min_points';
Where :gender
and :min_points
are parameters that we'll need to define as
an array:
$parameters = [
'min_points' => 30,
'gender' => 'F',
];
Once the rule is written and the parameters are defined, only the easiest part remains: filtering the target.
var_dump(
iterator_to_array(
$rulerz->filter($players, $rule, $parameters) // the parameters can be omitted if empty
)
);
// will return:
/*
array(1) {
[0]=>
array(3) {
["pseudo"]=>
string(5) "Alice"
["gender"]=>
string(1) "F"
["points"]=>
int(60)
}
}
*/
Yup, it's that easy.
Return to the index to explore the other possibilities of the library