示例#1
0
 /**
  * Create Expression for Comparison
  *
  * @param string|integer $key
  * @param mixed $value
  * @return Expression\Comparison
  * @throws \InvalidArgumentException
  */
 protected function createComparisonExpression($key, $value)
 {
     $singleComparisonOperators = ['=', '<=', '=>', '<', '>', '!=', '<>'];
     // expect comparison of 2 fields -> don't escape (f.e. join conditions)
     if (is_int($key)) {
         $conditionParts = explode(' ', $value);
         if (count($conditionParts) != 3 || !in_array($conditionParts[1], $singleComparisonOperators)) {
             throw new \InvalidArgumentException('Invalid comparison expression');
         }
         $left = $this->db->quote($conditionParts[0]);
         $operator = $conditionParts[1];
         $right = $this->db->quote($conditionParts[2]);
     } else {
         // string key -> comparison with value(s)
         $keyParts = explode(' ', $key);
         $left = $this->db->quote(array_shift($keyParts));
         if (!empty($keyParts)) {
             $operator = implode(' ', $keyParts);
         } else {
             $operator = '=';
         }
         if (is_array($value)) {
             if ($operator === '=') {
                 $operator = 'IN';
             } elseif (in_array($operator, ['!=', '<>'])) {
                 $operator = 'NOT IN';
             }
             if (!in_array($operator, ['IN', 'NOT IN'])) {
                 throw new \InvalidArgumentException('invalid operator for multiple value comparison');
             }
             $right = '(' . implode(', ', $this->db->escapeArray($value, true)) . ')';
         } else {
             if (!in_array($operator, $singleComparisonOperators)) {
                 throw new \InvalidArgumentException('invalid operator for single value comparison');
             }
             $right = $this->db->escape($value, true);
         }
     }
     return new Expression\Comparison($left, $operator, $right);
 }