Package Download: https://github.com/K-Phoen/rulerz
composer安装:composer require 'kphoen/rulerz'
use:
use RulerZ\Compiler\Compiler;
use RulerZ\RulerZ;
use RulerZ\Target\Native\Native;
public function getRulerZChecker()
{
$compiler = Compiler::create();
$rulerz = new RulerZ($compiler, [
new Native([
'length' => 'strlen'
],[
'contains' => function ($a, $b) {
return sprintf('strstr(%s, %s)', $a, $b);
}
])
]);
return $rulerz;
}
We need to search, verification of data:
1, volume matching,
$datas = [
['pseudo' => 'Joe', 'fullname' => 'Joe la frite', 'gender' => 'M', 'points' => 20],
['pseudo' => 'Moe', 'fullname' => 'Moe, from the bar!', 'gender' => 'M', 'points' => 200],
['pseudo' => 'hazel', 'fullname' => 'hazel, from the hazel!', 'gender' => 'M', 'points' => 100],
['pseudo' => 'Alice', 'fullname' => 'Alice, from... you know.', 'gender' => 'F', 'points' => 100],
['pseudo' => 'Alice', 'fullname' => 'Alice, from... you know.', 'gender' => 'F', 'points' => 20],
];
$rule = "gender = :gender and points > :min_points";
$parameters = [
'min_points' => 30,
'gender' => 'M',
];
$rulerz = $this->getRulerZChecker();
$result = iterator_to_array(
$rulerz->filter($datas, $rule, $parameters) // the parameters can be omitted if empty
);
The final result to
array(2) {
[0]=>
array(4) {
["pseudo"]=>
string(3) "Moe"
["fullname"]=>
string(18) "Moe, from the bar!"
["gender"]=>
string(1) "M"
["points"]=>
int(200)
}
[1]=>
array(4) {
["pseudo"]=>
string(5) "hazel"
["fullname"]=>
string(22) "hazel, from the hazel!"
["gender"]=>
string(1) "M"
["points"]=>
int(100)
}
}
2, it is determined whether the data meets the requirements:
rulerz- $> satisfies (Data $, $ rule, $ Parameters);
// returns a Boolean value, true indicates satisfied
$data =['pseudo' => 'Moe', 'fullname' => 'Moe, from the bar!', 'gender' => 'M', 'points' => 200];
$rule = "gender = :gender and points > :min_points";
$parameters = [
'min_points' => 30,
'gender' => 'M',
];
$rulerz = $this->getRulerZChecker();
$result = $rulerz->satisfies($data, $rule, $parameters);//bool(true)
Above contains
is expressed by the function of the system strstr()
to determine whether to include $ a $ b character, because compiled code is generated by a string, the string so you must use the anonymous expressed determination logic function, which is the one of the drawbacks.