Example #1
0
 protected static function clause_match($row, $clause)
 {
     // by default the row doesn't match
     // notice that we also break out of the match loop as soon as it doesn't match anymore
     // but make sure the default is false here
     $result = false;
     for ($i = 0; $i < count($clause); $i++) {
         // scan for arrays and recurse into them
         if (is_array($clause[$i])) {
             $result = self::clause_match($row, $clause[$i]);
         } else {
             if (strcasecmp('AND', $clause[$i]) == 0 || strcasecmp('OR', $clause[$i]) == 0) {
                 $before = array_slice($clause, 0, $i);
                 $after = array_slice($clause, $i + 1);
                 if (strcasecmp('AND', $clause[$i]) == 0) {
                     $result = self::clause_match($row, $before) && self::clause_match($row, $after);
                 } else {
                     if (strcasecmp('OR', $clause[$i]) == 0) {
                         $result = self::clause_match($row, $before) || self::clause_match($row, $after);
                     }
                 }
                 break;
             } else {
                 if (in_array($clause[$i], array('=', '!=', '<>'))) {
                     // evaluate the clause operator against the row
                     // is the left hand item a column in this row?
                     if (in_array($clause[$i - 1], array_keys($row))) {
                         // then compare that column to the right hand side of the oper
                         // SQL to PHP comparison operator filter
                         $operator = $clause[$i];
                         if ($operator == '=') {
                             $operator = '==';
                         } else {
                             if ($operator = '<>') {
                                 $operator = '!=';
                             }
                         }
                         $left_side = pgsql8::strip_escaping_e($row[$clause[$i - 1]]);
                         // row[col]'s value
                         $right_side = pgsql8::strip_escaping_e($clause[$i + 1]);
                         // comparison value
                         $expression = $left_side . ' ' . $operator . ' ' . $right_side;
                         $eval_expression = '$result = ( ' . $expression . ' ) ;';
                         if (eval($eval_expression) === false) {
                             throw new exception("eval() failed on eval_expression: " . $eval_expression);
                         }
                     }
                     // if the row doesn't match anymore, stop trying
                     if (!$result) {
                         break;
                     }
                 } else {
                     // dbsteward::console_line(6, "clause[$i] = " . $clause[$i]);
                     //$result = rand(1,5) != 2;
                 }
             }
         }
     }
     return $result;
 }