private static function evaluate($func, $a, $b)
{
$r = false;
switch ($func) {
case '$eq':
$r = $a == $b;
break;
case '$not':
$r = $a != $b;
break;
case '$gte':
case '$gt':
if (is_numeric($a) && is_numeric($b)) {
$r = $a > $b;
}
break;
case '$lte':
case '$lt':
if (is_numeric($a) && is_numeric($b)) {
$r = $a < $b;
}
break;
case '$in':
if (!is_array($b)) {
throw new \InvalidArgumentException('Invalid argument for $in option must be array');
}
$r = in_array($a, $b);
break;
case '$has':
if (is_array($b)) {
throw new \InvalidArgumentException('Invalid argument for $has array not supported');
}
if (!is_array($a)) {
$a = @json_decode($a, true) ?: array();
}
$r = in_array($b, $a);
break;
case '$all':
if (!is_array($a)) {
$a = @json_decode($a, true) ?: array();
}
if (!is_array($b)) {
throw new \InvalidArgumentException('Invalid argument for $all option must be array');
}
$r = count(array_intersect_key($a, $b)) == count($b);
break;
case '$regex':
case '$preg':
case '$match':
$r = (bool) @preg_match(isset($b[0]) && $b[0] == '/' ? $b : '/' . $b . '/i', $a, $match);
break;
case '$size':
if (!is_array($a)) {
$a = @json_decode($a, true) ?: array();
}
$r = (int) $b == count($a);
break;
case '$mod':
if (!is_array($b)) {
throw new \InvalidArgumentException('Invalid argument for $mod option must be array');
}
list($x, $y) = each($b);
$r = $a % $x == 0;
break;
case '$func':
case '$fn':
case '$f':
if (!is_callable($b)) {
throw new \InvalidArgumentException('Function should be callable');
}
$r = $b($a);
break;
default:
throw new \ErrorException("Condition not valid ... Use {$func} for custom operations");
break;
}
return $r;
}