protected function _andQuery() { $query = new Node\Query('AND'); $leftTerm = $this->_term(); $query->add($leftTerm); while ($this->_tokenStream->look()->getType() === 'AND') { $this->_tokenStream->expect('AND'); $rightTerm = $this->_term(); $query->add($rightTerm); } return $query; }
protected function _optimize(Node\Query $query) { $terms = $query->getTerms(); $termCount = count($terms); $subQueries = $query->getSubQueries(); $subQueryCount = count($subQueries); // Only contains a single term if ($subQueryCount === 0 && $termCount === 1) { return $terms[0]; } else { if ($subQueryCount == 1 && $termCount === 0) { return $this->_optimize($subQueries[0]); } else { $newQuery = new Node\Query($query->getOperator()); foreach ($terms as $term) { $newQuery->addTerm($term); } foreach ($subQueries as $subQuery) { $newQuery->add($this->_optimize($subQuery)); } return $newQuery; } } }