protected function filterToArray(Filter $filter)
 {
     if ($filter->isChain()) {
         return $this->filterChainToArray($filter);
     } else {
         return $this->filterExpressionToArray($filter);
     }
     return $array;
 }
 /**
  * Renders the Elasticsearch query as a recursive function, walking through the FilterChain.
  *
  * @param Filter  $filter    Current object
  * @param int     $level     Current depth in integer
  *
  * @return array|null|string
  *
  * @throws ProgrammingError
  */
 protected function renderFilter(Filter $filter, $level = 0)
 {
     $container = array();
     if ($filter->isChain()) {
         if ($filter instanceof FilterAnd) {
             $section = 'must';
         } elseif ($filter instanceof FilterOr) {
             $section = 'should';
         } elseif ($filter instanceof FilterNot) {
             $section = 'must_not';
         } else {
             throw new ProgrammingError('Cannot render filter chain type: %s', get_class($filter));
         }
         if (!$filter->isEmpty()) {
             /** @var Filter $filterPart */
             foreach ($filter->filters() as $filterPart) {
                 $part = $this->renderFilter($filterPart, $level + 1);
                 if ($part) {
                     if ($filter instanceof FilterNot) {
                         // add in a new bool to flip expression
                         if ($filterPart instanceof FilterMatchNot) {
                             $container[$section][] = array('bool' => array('must_not' => $part));
                             continue;
                         }
                     } elseif ($filter instanceof FilterAnd) {
                         // add match not to must_not instead of must
                         if ($filterPart instanceof FilterMatchNot) {
                             $container['must_not'][] = $part;
                             continue;
                         } elseif ($filterPart instanceof FilterNot) {
                             $container['must_not'] = $part['bool']['must_not'];
                             continue;
                         }
                     }
                     $container[$section][] = $part;
                 }
             }
             // return the bool of the chain
             return array('bool' => $container);
         } else {
             // return match_all
             return array('match_all' => (object) array());
         }
     } else {
         // return the simple part
         return $this->renderFilterExpression($filter);
     }
 }
Пример #3
0
 protected function resetSearchColumns(Filter &$filter)
 {
     if ($filter->isChain()) {
         $filters =& $filter->filters();
         if (!($empty = empty($filters))) {
             foreach ($filters as $k => &$f) {
                 if (false === $this->resetSearchColumns($f)) {
                     unset($filters[$k]);
                 }
             }
         }
         return $empty || !empty($filters);
     }
     return $filter->isExpression() ? !(in_array($filter->getColumn(), $this->searchColumns) && $filter->getSign() === '=') : true;
 }
Пример #4
0
 /**
  * Render and return the given filter as SQL-WHERE clause
  *
  * @param   Filter  $filter
  *
  * @return  string
  */
 public function renderFilter(Filter $filter, $level = 0)
 {
     // TODO: This is supposed to supersede DbQuery::renderFilter()
     $where = '';
     if ($filter->isChain()) {
         if ($filter instanceof FilterAnd) {
             $operator = ' AND ';
         } elseif ($filter instanceof FilterOr) {
             $operator = ' OR ';
         } elseif ($filter instanceof FilterNot) {
             $operator = ' AND ';
             $where .= ' NOT ';
         } else {
             throw new ProgrammingError('Cannot render filter: %s', get_class($filter));
         }
         if (!$filter->isEmpty()) {
             $parts = array();
             foreach ($filter->filters() as $filterPart) {
                 $part = $this->renderFilter($filterPart, $level + 1);
                 if ($part) {
                     $parts[] = $part;
                 }
             }
             if (!empty($parts)) {
                 if ($level > 0) {
                     $where .= ' (' . implode($operator, $parts) . ') ';
                 } else {
                     $where .= implode($operator, $parts);
                 }
             }
         } else {
             return '';
             // Explicitly return the empty string due to the FilterNot case
         }
     } else {
         $where .= $this->renderFilterExpression($filter);
     }
     return $where;
 }
Пример #5
0
 public function addFilter(Filter $filter)
 {
     // TODO: This should be considered a quick fix only.
     //       Drop this entirely once support for Icinga\Data\Filter is available
     if ($filter->isExpression()) {
         $this->where($filter->getColumn(), $filter->getExpression());
     } elseif ($filter->isChain()) {
         foreach ($filter->filters() as $chainOrExpression) {
             $this->addFilter($chainOrExpression);
         }
     }
 }