Example #1
0
 /**
  * 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;
 }
Example #2
0
 /**
  * Reduce components of query group to a search string of a simple query.
  *
  * This function implements the recursive reduction of a query group.
  *
  * Finna: Added finalizeSearchString() call
  *
  * @param AbstractQuery $component Component
  *
  * @return string
  *
  * @see  self::reduceQueryGroup()
  * @todo Refactor so that functionality does not need to be copied
  */
 protected function reduceQueryGroupComponents(AbstractQuery $component)
 {
     if ($component instanceof QueryGroup) {
         $reduced = array_map([$this, 'reduceQueryGroupComponents'], $component->getQueries());
         $searchString = $component->isNegated() ? 'NOT ' : '';
         $reduced = array_filter($reduced, function ($s) {
             return '' !== $s;
         });
         if ($reduced) {
             $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) {
             $searchString = $this->createSearchString($searchString, $searchHandler);
         }
     }
     return $searchString;
 }