Ejemplo n.º 1
0
 /**
  * @param Fetcher $fetcher
  * @param FetcherOperation $operation
  * @return array query and params
  */
 protected function getOperationAsString(Fetcher $fetcher, FetcherOperation $operation)
 {
     $query = '';
     $params = array();
     if ($operation instanceof SimpleOperation) {
         $operator = $operation->getOperator();
         if ($operator === Fetcher::TYPE_IN) {
             $newOperation = new ListOperation(Fetcher::OPERATOR_OR);
             foreach ($operation->getValue() as $val) {
                 $newOperation->addOperation(new SimpleOperation(Fetcher::TYPE_EQ, $operation->getColumnName(), $val));
             }
             list($query, $params) = $this->getOperationAsString($fetcher, $newOperation);
         } elseif ($operator === Fetcher::TYPE_NOT_IN) {
             $newOperation = new ListOperation(Fetcher::OPERATOR_AND);
             foreach ($operation->getValue() as $val) {
                 $newOperation->addOperation(new SimpleOperation(Fetcher::TYPE_DIFF, $operation->getColumnName(), $val));
             }
             list($query, $params) = $this->getOperationAsString($fetcher, $newOperation);
         } else {
             if ($operator === Fetcher::TYPE_CUSTOM) {
                 $value = $operation->getValue();
                 if (!$value instanceof OperationValue) {
                     throw new \Exception('OperationValue expected');
                 }
                 list($operationValueQuery, $operationValueParams) = $value->getOperationValue($operation->getColumnName());
                 if (empty($operationValueParams)) {
                     $query .= $operationValueQuery;
                 } else {
                     $query .= vsprintf($operationValueQuery, array_fill(0, count($operationValueParams), '?'));
                     foreach ($operationValueParams as $valueParam) {
                         $params[] = $valueParam;
                     }
                 }
             } else {
                 $query = $fetcher->strColumnFilterToDbNotation($operation->getColumnName(), $operator);
                 $query .= ' ';
                 $query .= $fetcher->strFilterToDbNotation($operator);
                 if ($operator !== Fetcher::TYPE_IS_NOT_NULL && $operator !== Fetcher::TYPE_IS_NULL) {
                     // If the operation has 2 members
                     $params[] = $this->getOperationValue($operation);
                 }
             }
         }
     } elseif ($operation instanceof ListOperation) {
         $operations = $operation->getOperations();
         $strings = array();
         foreach ($operations as $currentOperation) {
             list($returnQuery, $returnParams) = $this->getOperationAsString($fetcher, $currentOperation);
             $strings[] = $returnQuery;
             $params = array_merge($params, $returnParams);
         }
         $query = implode($operation->getOperator(), $strings);
     } else {
         throw new \InvalidArgumentException('Givent operation is not supported');
     }
     return array('(' . $query . ')', $params);
 }
Ejemplo n.º 2
0
 /**
  * Sets the main operator (AND or OR)
  * @param string $operator self::OPERATOR_AND or self::OPERATOR_OR
  * @throws \InvalidArgumentException
  */
 public function setMainOperator($operator)
 {
     switch ($operator) {
         case self::OPERATOR_AND:
             $this->mainOperator = self::OPERATOR_AND;
             break;
         case self::OPERATOR_OR:
             $this->mainOperator = self::OPERATOR_OR;
             break;
         default:
             throw new \InvalidArgumentException(__CLASS__ . '::' . __FUNCTION__ . '() First argument should be ' . __CLASS__ . '::OPERATOR_AND or ' . __CLASS__ . '::OPERATOR_OR constant');
             break;
     }
     $this->rootOperation->setOperator($this->mainOperator);
 }