Ejemplo n.º 1
0
 public function where(array $filter)
 {
     try {
         Entity\Filter::validate($this->entityReflection, $filter);
     } catch (Exception\FilterException $e) {
         throw new Exception\QueryException($e->getMessage());
     }
     $this->filter = Entity\Filter::merge($this->filter, $filter);
     return $this;
 }
Ejemplo n.º 2
0
 public function convertFilter(array $filter)
 {
     $result = [];
     if (Filter::isGroup($filter)) {
         // Filter group
         foreach ($filter as $modifier => $item) {
             $result[] = [$modifier === Filter::_OR ? "%or" : "%and", $this->convertFilter($item)];
         }
     } else {
         // Filter item
         foreach ($filter as $name => $item) {
             foreach ($item as $operator => $value) {
                 // Convert data type definition to modificator
                 $type = gettype($value);
                 if ($type === "object") {
                     $type = get_class($value);
                 }
                 if (!isset($this->modificators[$type])) {
                     throw new \Exception("Unsupported value type " . $type . " given!");
                 }
                 $modificator = $this->modificators[$type];
                 if ($operator === Filter::START) {
                     $operator = "LIKE";
                     $modificator = "%like~";
                 } elseif ($operator === Filter::END) {
                     $operator = "LIKE";
                     $modificator = "%~like";
                 } elseif ($operator === Filter::CONTAIN) {
                     $operator = "LIKE";
                     $modificator = "%~like~";
                 }
                 if ($modificator === "%in") {
                     if ($operator === Filter::EQUAL) {
                         $operator = "IN";
                     } elseif ($operator === Filter::NOT) {
                         $operator = "NOT IN";
                     }
                 } elseif (in_array($modificator, ["%sN", "%b"], true)) {
                     if ($operator === Filter::EQUAL) {
                         $operator = "IS";
                     } elseif ($operator === Filter::NOT) {
                         $operator = "IS NOT";
                     }
                 }
                 if ($operator === Filter::NOT) {
                     $operator = "!=";
                 }
                 $result[] = ["%n %sql " . $modificator, $name, $operator, $value];
             }
         }
     }
     return $result;
 }
Ejemplo n.º 3
0
 private function _formatFilter(array $filter, $or = false)
 {
     $result = "";
     if (Filter::isGroup($filter)) {
         // Filter group
         foreach ($filter as $modifier => $item) {
             $formatted = "(" . $this->_formatFilter($item, $modifier === Filter::_OR ? true : false) . ")";
             if (empty($result)) {
                 $result = $formatted;
             } else {
                 $result .= " " . ($or ? "OR" : "AND") . " " . $formatted;
             }
         }
         return "(" . $result . ")";
     } else {
         // Filter item
         foreach ($filter as $name => $item) {
             foreach ($item as $modifier => $value) {
                 if ($name === "stitky") {
                     $value = explode(",", $value);
                 }
                 if ($modifier === Filter::START) {
                     $modifier = "BEGINS";
                     if (Adapter::$likeWithSimilar) {
                         $modifier .= " SIMILAR";
                     }
                     $value = "'" . $value . "'";
                 } elseif ($modifier === Filter::END) {
                     $modifier = "ENDS";
                     if (Adapter::$likeWithSimilar) {
                         $modifier .= " SIMILAR";
                     }
                     $value = "'" . $value . "'";
                 } elseif ($modifier === Filter::CONTAIN) {
                     $modifier = "LIKE";
                     if (Adapter::$likeWithSimilar) {
                         $modifier .= " SIMILAR";
                     }
                     $value = "'" . $value . "'";
                 } elseif ($modifier === Filter::NOT && is_array($value)) {
                     // NOT IN
                     foreach ($value as $index => $item) {
                         $value[$index] = $name . " != " . (is_string($item) ? "'" . $item . "'" : $item);
                     }
                     $value = "(" . implode(" AND ", $value) . ")";
                     unset($modifier);
                     unset($name);
                 } elseif ($modifier === Filter::EQUAL && is_array($value)) {
                     // IN
                     if ($name === "stitky") {
                         foreach ($value as $index => $item) {
                             $value[$index] = $name . " = '" . $item . "'";
                         }
                         $value = "(" . implode(" OR ", $value) . ")";
                         unset($modifier);
                         unset($name);
                     } else {
                         $modifier = "IN";
                         if (array_filter($value, 'is_string')) {
                             $value = "('" . implode("','", $value) . "')";
                         } else {
                             $value = "(" . implode(",", $value) . ")";
                         }
                     }
                 } elseif (in_array($modifier, [Filter::EQUAL, Filter::NOT], true)) {
                     // IS, IS NOT, =, !=
                     if (is_bool($value)) {
                         if ($modifier === Filter::NOT) {
                             $value = !$value;
                             // Flexibee does not support IS NOT with bool values
                         }
                         $modifier = "IS";
                     } elseif ($value === null || $value === "") {
                         if ($modifier === Filter::NOT) {
                             $modifier = "IS NOT";
                         } else {
                             $modifier = "IS";
                         }
                         $value = "NULL";
                     } elseif ($value === "''" || $value === '""') {
                         if ($modifier === Filter::NOT) {
                             $modifier = "IS NOT";
                         } else {
                             $modifier = "IS";
                         }
                         $value = "empty";
                     } else {
                         if ($modifier === Filter::EQUAL) {
                             $modifier = "=";
                         } else {
                             $modifier = "!=";
                         }
                         $value = is_string($value) ? "'" . $value . "'" : $value;
                     }
                 } else {
                     // Other modifiers
                     $value = is_string($value) ? "'" . $value . "'" : $value;
                 }
                 if (is_bool($value)) {
                     $value = var_export($value, true);
                 }
                 if (!empty($result)) {
                     $result .= " " . ($or ? "OR" : "AND") . " ";
                 }
                 if (isset($name)) {
                     $result .= $name . " ";
                 }
                 if (isset($modifier)) {
                     $result .= $modifier . " ";
                 }
                 $result .= $value;
             }
         }
         return $result;
     }
 }
Ejemplo n.º 4
0
 /**
  * @param Reflection $reflection
  * @param array      $filter
  *
  * @return array
  */
 public function unmapFilter(Reflection $reflection, array $filter)
 {
     $result = [];
     if (Filter::isGroup($filter)) {
         foreach ($filter as $modifier => $item) {
             $result[$modifier] = $this->unmapFilter($reflection, $item);
         }
     } else {
         foreach ($filter as $name => $item) {
             $property = $reflection->getProperty($name);
             $unmappedName = $property->getName(true);
             foreach ($item as $modifier => $value) {
                 $result[$unmappedName][$modifier] = $this->unmapValue($property, $value);
             }
         }
     }
     return $result;
 }