Skip to content
This repository has been archived by the owner on Jan 10, 2018. It is now read-only.

Commit

Permalink
Factor out complex Compiler::compileQuery() to separate functions
Browse files Browse the repository at this point in the history
  • Loading branch information
vierbergenlars committed Oct 6, 2013
1 parent 36f52a7 commit 2726fd9
Showing 1 changed file with 32 additions and 20 deletions.
52 changes: 32 additions & 20 deletions lib/vierbergenlars/Forage/QueryParser/Compiler.php
Expand Up @@ -125,28 +125,10 @@ public function compileQuery($queryExpr)
$searchQuery.= ' ' . $token->getData();
break;
case Token::T_FIELD_NAME:
if(!$this->isAllowedFieldName($token->getData()))
throw new ParseException('Field name not allowed', $queryExpr, $token->getStartPosition());
$nextToken = next($tokens);
if($nextToken === false)
throw new ParseException('Unexpected end of token stream', $queryExpr, strlen($queryExpr));
if(!$this->isAllowedToken($nextToken))
throw new ParseException(Token::getName($nextToken->getType()) . ' is disabled', $queryExpr, $nextToken->getStartPosition());
switch($nextToken->getType()) {
case Token::T_FIELD_VALUE:
$this->queryBuilder->addFilter($token->getData(), $nextToken->getData());
break;
case Token::T_FIELD_WEIGHT:
$this->queryBuilder->addWeight($token->getData(), $nextToken->getData());
break;
default:
throw new ParseException('Unexpected ' . Token::getName($nextToken->getType()), $queryExpr, $nextToken->getStartPosition());
}
$this->compileFieldName($tokens, $queryExpr);
break;
case Token::T_FIELD_SEARCH:
if(!$this->isAllowedSearchField($token->getData()))
throw new ParseException('Search field not allowed', $queryExpr, $token->getStartPosition());
$this->queryBuilder->addSearchField($token->getData());
$this->compileSearchField($tokens, $queryExpr);
break;
default:
throw new ParseException('Unexpected ' . Token::getName($token->getType()) . ' (This is a lexer bug, please report it)', $queryExpr, $token->getStartPosition());
Expand All @@ -157,6 +139,36 @@ public function compileQuery($queryExpr)
return $this;
}

private function compileFieldName(&$tokens, $queryExpr)
{
$token = current($tokens);
if(!$this->isAllowedFieldName($token->getData()))
throw new ParseException('Field name not allowed', $queryExpr, $token->getStartPosition());
$nextToken = next($tokens);
if($nextToken === false)
throw new ParseException('Unexpected end of token stream', $queryExpr, strlen($queryExpr));
if(!$this->isAllowedToken($nextToken))
throw new ParseException(Token::getName($nextToken->getType()) . ' is disabled', $queryExpr, $nextToken->getStartPosition());
switch($nextToken->getType()) {
case Token::T_FIELD_VALUE:
$this->queryBuilder->addFilter($token->getData(), $nextToken->getData());
break;
case Token::T_FIELD_WEIGHT:
$this->queryBuilder->addWeight($token->getData(), $nextToken->getData());
break;
default:
throw new ParseException('Unexpected ' . Token::getName($nextToken->getType()), $queryExpr, $nextToken->getStartPosition());
}
}

private function compileSearchField($tokens, $queryExpr)
{
$token = current($tokens);
if(!$this->isAllowedSearchField($token->getData()))
throw new ParseException('Search field not allowed', $queryExpr, $token->getStartPosition());
$this->queryBuilder->addSearchField($token->getData());
}

/**
* Gets the query builder
* @return \vierbergenlars\Forage\SearchQuery\QueryBuilder
Expand Down

0 comments on commit 2726fd9

Please sign in to comment.