function where2mg($str)
{
    # Make infix stuff to polishstuff
    $arr = preg_split('/ *?(\\() *?| *?(\\)) *?| +(and) +| +(or) +| +(not) +|(not)/i', $str, null, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
    #print_r ($arr);
    $stack = array();
    $polish = array();
    foreach ($arr as $val) {
        $val = trim($val);
        #echo "'$val'\n<br/>";
        switch (strtolower($val)) {
            case 'not':
                #while ((sizeof($stack) > 0) && ($stack[sizeof($stack) - 1] != 'and' || $stack[sizeof($stack) - 1] != 'or') ) {
                #	$e = array_pop($stack);
                #	$polish[] = $e;
                #}
                $stack[] = strtolower($val);
                break;
            case 'or':
                while (sizeof($stack) > 0 && $stack[sizeof($stack) - 1] == 'and') {
                    $e = array_pop($stack);
                    $polish[] = $e;
                }
                $stack[] = strtolower($val);
                break;
            case 'and':
                $stack[] = strtolower($val);
                break;
            case '(':
                $stack[] = $val;
                break;
            case ')':
                while (sizeof($stack) > 0 && $stack[sizeof($stack) - 1] != '(') {
                    $e = array_pop($stack);
                    $polish[] = $e;
                }
                $null .= array_pop($stack);
                break;
            default:
                ### handle not
                $polish[] = $val;
                if (1) {
                    while (sizeof($stack) > 0 && $stack[sizeof($stack) - 1] == 'not') {
                        #echo "... $val";
                        $e = array_pop($stack);
                        $polish[] = $e;
                    }
                }
                break;
        }
    }
    # empty stack
    while (sizeof($stack) > 0) {
        $e = array_pop($stack);
        $polish[] = $e;
    }
    #foreach ($polish as $key) { echo $key . " "; }
    #echo "<br/>";
    #### Polish stuff to mongo
    $tmpval = array();
    $cnt = 0;
    $nextoper = '';
    $popcount = 2;
    $tmparr = array();
    foreach ($polish as $val) {
        $cnt++;
        switch (strtolower($val)) {
            case 'and':
            case 'not':
            case 'or':
                if ($val == 'or') {
                    $mgoper = '$or';
                }
                if ($val == 'and') {
                    $mgoper = '$and';
                }
                if ($val == 'not') {
                    #echo "not found";
                    $mgoper = '$not';
                    $popcount = 1;
                }
                if ($val == $polish[$cnt] && $popcount > 1) {
                    $popcount++;
                    #echo "same oper $val\n";
                    continue;
                }
                $tmparr2 = array();
                for ($i = 1; $i <= $popcount; $i++) {
                    $tmparr2[] = array_pop($tmparr);
                }
                $operstring = join(", ", array_reverse($tmparr2));
                if ($popcount == 1) {
                    $e = $operstring;
                    ### Rewrite 'not' stuff
                    if ($val == 'not') {
                        # fx { b : { $ne : 5 } } = { b : 5 }
                        $e = preg_replace('/{ (\\w+) : ([\\w\'"]+) }/', '{ $1 : { $ne : $2 } }', $e);
                        # fx:  { a : 5 }  = { a : { $ne : 5 } };
                        ### if no change
                        if ($e == $operstring) {
                            $e = preg_replace('/{ (\\w+) : { \\$ne : ([\\w\'"]+) } }/', '{ $1 : $2 }', $e);
                        }
                        echo $e;
                    } else {
                        $e = " { {$mgoper} : {$operstring} } ..";
                    }
                    $tmparr[] = $e;
                    $popcount = 2;
                    continue;
                } else {
                    $e = " { {$mgoper} : [  {$operstring} ] } ";
                }
                $popcount = 2;
                if (isset($polish[$cnt])) {
                    # if stuff is still left
                    # push it back
                    $tmparr[] = $e;
                } else {
                    # only called once
                    $rs .= $e;
                }
                break;
            default:
                $tmparr[] = equation2mg($val);
                #$tmparr[] = ($val);
                break;
        }
    }
    foreach ($tmparr as $val) {
        $rs .= $val;
    }
    return $rs;
}
Exemple #2
0
                echo "same oper {$val}\n";
                continue;
            }
            $tmparr2 = array();
            for ($i = 1; $i <= $popcount; $i++) {
                $tmparr2[] = array_pop($tmparr);
            }
            $popcount = 2;
            $operstring = join(', ', array_reverse($tmparr2));
            $e = " { {$mgoper} : [ {$operstring} ] } ";
            if (isset($outarr[$cnt])) {
                # if stuff is still left
                # push it back
                $tmparr[] = $e;
                echo "hm";
            } else {
                echo "e: {$e}\n";
            }
            break;
        default:
            $tmparr[] = equation2mg($val);
            break;
    }
}
# empty tmparr
foreach ($tmparr as $val) {
    echo "{$val}\n";
}
echo "{$e}\n";
#print ($out . "\n");
#print_r (($outarr));