/** * Support method for display() -- process advanced queries. * * @param AbstractQuery $query Query to convert * @param callable $translate Callback to translate strings * @param callable $showName Callback to translate field names * * @return string */ protected static function displayAdvanced(AbstractQuery $query, $translate, $showName) { $output = ''; //There should only ever be 1 group with EDS queries. $all = []; foreach ($query->getQueries() as $search) { if ($search instanceof QueryGroup) { // Process each search group. There should only be 1 with EDS queries $groupQueries = $search->getQueries(); for ($i = 0; $i < count($groupQueries); $i++) { $group = $groupQueries[$i]; if ($group instanceof Query) { // Build this group individually as a basic search $queryOperator = $group->getOperator(); $op = null != $queryOperator && 0 != $i ? call_user_func($translate, $queryOperator) . ' ' : ''; $all[] = $op . call_user_func($showName, $group->getHandler()) . ':' . $group->getString(); } else { throw new \Exception('Unexpected ' . get_class($group)); } } } else { throw new \Exception('Unexpected ' . get_class($search)); } } $output = '(' . join(' ', $all) . ')'; return $output; }
/** * Reduce components of query group to a search string of a simple query. * * This function implements the recursive reduction of a query group. * * @param AbstractQuery $component Component * * @return string * * @see self::reduceQueryGroup() */ protected function reduceQueryGroupComponents(AbstractQuery $component) { if ($component instanceof QueryGroup) { $reduced = array_map([$this, 'reduceQueryGroupComponents'], $component->getQueries()); $searchString = $component->isNegated() ? 'NOT ' : ''; $searchString .= sprintf('(%s)', implode(" {$component->getOperator()} ", $reduced)); } else { $searchString = $this->getLuceneHelper()->normalizeSearchString($component->getString()); $searchString = $this->getLuceneHelper()->finalizeSearchString($searchString); $searchHandler = $this->getSearchHandler($component->getHandler(), $searchString); if ($searchHandler) { $searchString = $this->createSearchString($searchString, $searchHandler); } } return $searchString; }
/** * Convert a Query or QueryGroup into minified search arguments. * * @param AbstractQuery $query Query to minify * @param bool $topLevel Is this a top-level query? (Used for recursion) * * @return array */ public static function minify(AbstractQuery $query, $topLevel = true) { // Simple query: if ($query instanceof Query) { return [['l' => $query->getString(), 'i' => $query->getHandler()]]; } // Advanced query: $retVal = []; $operator = $query->isNegated() ? 'NOT' : $query->getOperator(); foreach ($query->getQueries() as $current) { if ($topLevel) { $retVal[] = ['g' => self::minify($current, false), 'j' => $operator]; } elseif ($current instanceof QueryGroup) { throw new \Exception('Not sure how to minify this query!'); } else { $currentArr = ['f' => $current->getHandler(), 'l' => $current->getString(), 'b' => $operator]; if (null !== ($op = $current->getOperator())) { $currentArr['o'] = $op; } $retVal[] = $currentArr; } } return $retVal; }