Skip to content
Permalink
Browse files Browse the repository at this point in the history
remove $fn, $func, $f as field filter ... add $where as an alternativ…
…e (MongoLite)
  • Loading branch information
aheinze committed Sep 25, 2020
1 parent 6a7cb0a commit 2a385af
Showing 1 changed file with 14 additions and 11 deletions.
25 changes: 14 additions & 11 deletions lib/MongoLite/Database.php
Expand Up @@ -235,6 +235,12 @@ public function __get($collection) {

class UtilArrayQuery {

protected static $closures = [];

public static function closureCall($uid, $doc) {
return call_user_func_array(self::$closures[$uid], [$doc]);
}

public static function buildCondition($criteria, $concat = ' && ') {

$fn = [];
Expand Down Expand Up @@ -268,11 +274,16 @@ public static function buildCondition($criteria, $concat = ' && ') {

case '$where':

if (\is_callable($value)) {

// need implementation
if (\is_string($value) || !\is_callable($value)) {
throw new \InvalidArgumentException($key.' Function should be callable');
}

$uid = \uniqid('mongoliteCallable').bin2hex(random_bytes(5));

self::$closures[$uid] = $value;

$fn[] = '\\MongoLite\\UtilArrayQuery::closureCall("'.$uid.'", $document)';

break;

default:
Expand Down Expand Up @@ -426,14 +437,6 @@ private static function evaluate($func, $a, $b) {
$r = $a % $b[0] == $b[1] ?? 0;
break;

case '$func' :
case '$fn' :
case '$f' :
if (\is_string($b) || !\is_callable($b))
throw new \InvalidArgumentException('Function should be callable');
$r = $b($a);
break;

case '$exists':
$r = $b ? !\is_null($a) : \is_null($a);
break;
Expand Down

0 comments on commit 2a385af

Please sign in to comment.