/** * * @param FetcherOperation $operation * * @return \Berthe\Fetcher\Operation\ListOperation */ public function addOperation(FetcherOperation $operation) { $groupName = $operation->getGroupName(); if ($groupName != null && $groupName != $this->groupName) { $this->operations[$groupName] = $operation; } else { $this->operations[] = $operation; } return $this; }
/** * @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); }
private function getColumnsFromOperation(FetcherOperation $operation) { $columns = array(); if ($operation instanceof SimpleOperation) { $columns[] = $operation->getColumnName(); } elseif ($operation instanceof ListOperation) { $operations = $operation->getOperations(); foreach ($operations as $curOperation) { $columns = array_merge($columns, $this->getColumnsFromOperation($curOperation)); } } else { throw new \InvalidArgumentException('Invalid operation!'); } return $columns; }