/** * @param \Leapt\AdminBundle\Datalist\Filter\DatalistFilterExpressionBuilder $builder * @param \Leapt\AdminBundle\Datalist\Filter\DatalistFilterInterface $filter * @param mixed $value * @param array $options */ public function buildExpression(DatalistFilterExpressionBuilder $builder, DatalistFilterInterface $filter, $value, array $options) { if (is_array($options['search_fields'])) { $expression = new CombinedExpression(CombinedExpression::OPERATOR_OR); foreach ($options['search_fields'] as $searchField) { $comparisonExpression = new ComparisonExpression($searchField, ComparisonExpression::OPERATOR_LIKE, $value); $expression->addExpression($comparisonExpression); } } else { $expression = new ComparisonExpression($options['search_fields'], ComparisonExpression::OPERATOR_LIKE, $value); } $builder->add($expression); }
/** * @param \Leapt\AdminBundle\Datalist\Filter\Expression\CombinedExpression $expression * @return callable * @throws \UnexpectedValueException */ private function buildCombinedExpressionCallback(CombinedExpression $expression) { $tests = array(); foreach ($expression->getExpressions() as $subExpression) { $tests[] = $this->buildExpressionCallback($subExpression); } $operator = $expression->getOperator(); // If we have a "AND" expression, return a function testing that all sub-expressions succeed if (CombinedExpression::OPERATOR_AND === $operator) { $function = function ($item) use($tests) { foreach ($tests as $test) { if (!call_user_func($test, $item)) { return false; } } return true; }; } elseif (CombinedExpression::OPERATOR_OR === $operator) { $function = function ($item) use($tests) { foreach ($tests as $test) { if (call_user_func($test, $item)) { return true; } } return false; }; } else { throw new \UnexpectedValueException(sprintf('Unknown operator "%s"', $operator)); } return $function; }
/** * @param Expression\ExpressionInterface $expression */ public function add(ExpressionInterface $expression) { $this->expression->addExpression($expression); }
/** * @param \Leapt\AdminBundle\Datalist\Filter\Expression\CombinedExpression $expression * @return \Doctrine\ORM\Query\Expr\Andx|\Doctrine\ORM\Query\Expr\Orx * @throws \UnexpectedValueException */ private function buildQueryBuilderCombinedExpression(CombinedExpression $expression) { $queryBuilderSubExpressions = array(); foreach ($expression->getExpressions() as $subExpression) { $queryBuilderSubExpressions[] = $this->buildQueryBuilderExpression($subExpression); } $operator = $expression->getOperator(); if (CombinedExpression::OPERATOR_AND === $operator) { $expr = $this->queryBuilder->expr()->andX(); } elseif (CombinedExpression::OPERATOR_OR === $operator) { $expr = $this->queryBuilder->expr()->orX(); } else { throw new \UnexpectedValueException(sprintf('Unknown operator "%s"', $operator)); } $expr->addMultiple($queryBuilderSubExpressions); return $expr; }