diff --git a/src/__phutil_library_map__.php b/src/__phutil_library_map__.php --- a/src/__phutil_library_map__.php +++ b/src/__phutil_library_map__.php @@ -390,6 +390,7 @@ 'PhutilSearchQueryCompiler' => 'search/PhutilSearchQueryCompiler.php', 'PhutilSearchQueryCompilerSyntaxException' => 'search/PhutilSearchQueryCompilerSyntaxException.php', 'PhutilSearchQueryCompilerTestCase' => 'search/__tests__/PhutilSearchQueryCompilerTestCase.php', + 'PhutilSearchQueryToken' => 'search/PhutilSearchQueryToken.php', 'PhutilSearchStemmer' => 'search/PhutilSearchStemmer.php', 'PhutilSearchStemmerTestCase' => 'search/__tests__/PhutilSearchStemmerTestCase.php', 'PhutilServiceProfiler' => 'serviceprofiler/PhutilServiceProfiler.php', @@ -1003,6 +1004,7 @@ 'PhutilSearchQueryCompiler' => 'Phobject', 'PhutilSearchQueryCompilerSyntaxException' => 'Exception', 'PhutilSearchQueryCompilerTestCase' => 'PhutilTestCase', + 'PhutilSearchQueryToken' => 'Phobject', 'PhutilSearchStemmer' => 'Phobject', 'PhutilSearchStemmerTestCase' => 'PhutilTestCase', 'PhutilServiceProfiler' => 'Phobject', diff --git a/src/search/PhutilSearchQueryCompiler.php b/src/search/PhutilSearchQueryCompiler.php --- a/src/search/PhutilSearchQueryCompiler.php +++ b/src/search/PhutilSearchQueryCompiler.php @@ -19,15 +19,6 @@ return $this->operators; } - public function setQuery($query) { - $this->query = $query; - return $this; - } - - public function getQuery() { - return $this->query; - } - public function setStemmer(PhutilSearchStemmer $stemmer) { $this->stemmer = $stemmer; return $this; @@ -37,9 +28,8 @@ return $this->stemmer; } - public function compileQuery() { - $query = $this->getQuery(); - $tokens = $this->tokenizeQuery($query); + public function compileQuery(array $tokens) { + assert_instances_of($tokens, 'PhutilSearchQueryToken'); $result = array(); foreach ($tokens as $token) { @@ -49,13 +39,12 @@ return $this->compileRenderedTokens($result); } - public function compileLiteralQuery() { - $query = $this->getQuery(); - $tokens = $this->tokenizeQuery($query); + public function compileLiteralQuery(array $tokens) { + assert_instances_of($tokens, 'PhutilSearchQueryToken'); $result = array(); foreach ($tokens as $token) { - if (!$token['quoted']) { + if (!$token->isQuoted()) { continue; } $result[] = $this->renderToken($token); @@ -64,13 +53,12 @@ return $this->compileRenderedTokens($result); } - public function compileStemmedQuery() { - $query = $this->getQuery(); - $tokens = $this->tokenizeQuery($query); + public function compileStemmedQuery(array $tokens) { + assert_instances_of($tokens, 'PhutilSearchQueryToken'); $result = array(); foreach ($tokens as $token) { - if ($token['quoted']) { + if ($token->isQuoted()) { continue; } $result[] = $this->renderToken($token, $this->getStemmer()); @@ -88,6 +76,17 @@ return implode(' ', $list); } + public function newTokens($query) { + $results = $this->tokenizeQuery($query); + + $tokens = array(); + foreach ($results as $result) { + $tokens[] = PhutilSearchQueryToken::newFromDictionary($result); + } + + return $tokens; + } + private function tokenizeQuery($query) { $maximum_bytes = 1024; @@ -233,16 +232,16 @@ } private function renderToken( - array $token, + PhutilSearchQueryToken $token, PhutilSearchStemmer $stemmer = null) { - $value = $token['value']; + $value = $token->getValue(); if ($stemmer) { $value = $stemmer->stemToken($value); } $value = $this->quoteToken($value); - $operator = $token['operator']; + $operator = $token->getOperator(); $prefix = $this->getOperatorPrefix($operator); $value = $prefix.$value; @@ -283,5 +282,4 @@ return $open_quote.$value.$close_quote; } - } diff --git a/src/search/PhutilSearchQueryToken.php b/src/search/PhutilSearchQueryToken.php new file mode 100644 --- /dev/null +++ b/src/search/PhutilSearchQueryToken.php @@ -0,0 +1,31 @@ +isQuoted = $dictionary['quoted']; + $token->operator = $dictionary['operator']; + $token->value = $dictionary['value']; + + return $token; + } + + public function isQuoted() { + return $this->isQuoted; + } + + public function getValue() { + return $this->value; + } + + public function getOperator() { + return $this->operator; + } + +} diff --git a/src/search/__tests__/PhutilSearchQueryCompilerTestCase.php b/src/search/__tests__/PhutilSearchQueryCompilerTestCase.php --- a/src/search/__tests__/PhutilSearchQueryCompilerTestCase.php +++ b/src/search/__tests__/PhutilSearchQueryCompilerTestCase.php @@ -99,8 +99,7 @@ $stemmed_query = null; try { - $compiler = id(new PhutilSearchQueryCompiler()) - ->setQuery($input); + $compiler = new PhutilSearchQueryCompiler(); if ($operators !== null) { $compiler->setOperators($operators); @@ -110,11 +109,13 @@ $compiler->setStemmer($stemmer); } + $tokens = $compiler->newTokens($input); + if ($stemmer) { - $literal_query = $compiler->compileLiteralQuery(); - $stemmed_query = $compiler->compileStemmedQuery(); + $literal_query = $compiler->compileLiteralQuery($tokens); + $stemmed_query = $compiler->compileStemmedQuery($tokens); } else { - $query = $compiler->compileQuery(); + $query = $compiler->compileQuery($tokens); } } catch (PhutilSearchQueryCompilerSyntaxException $ex) { $caught = $ex;