/** * * @param FetcherOperation $operation * @throws \InvalidArgumentException * * @return \Berthe\Fetcher */ protected function addFilterOperation(FetcherOperation $operation) { $newOperation = $operation; $groupName = $operation->getGroupName(); $previousOperation = $this->rootOperation->getOperation($groupName); //If there's already an operation for that group if ($previousOperation != null) { $groupFilterOperator = $this->getFilterOperator($groupName); $createNewList = true; if ($previousOperation instanceof ListOperation) { //If the previous operation is a list containing the groupName, we don't create a new list $subOperations = $previousOperation->getOperations(); foreach ($subOperations as $subOperation) { if ($subOperation->getGroupName() == $groupName) { $createNewList = false; break; } } } if ($createNewList) { //We crate a list operation with the name of the group $newOperation = new ListOperation($groupFilterOperator, $groupName); //We add the retrieved operation to the new operation created $newOperation->addOperation($previousOperation); } else { $newOperation = $previousOperation; } //We add the new operation to the list operation retrieved/created $newOperation->addOperation($operation); } //If it's the first for its group, we keep it unchanged $this->rootOperation->addOperation($newOperation); 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); }