From 3555f54fd5a908a0209ada089fa8fc4b6bbf2ceb Mon Sep 17 00:00:00 2001 From: Nenad Ticaric Date: Wed, 30 Sep 2020 15:27:23 +0200 Subject: [PATCH] adding filters --- composer.json | 16 +++++----- phpunit.xml | 2 +- src/Engines/TNTSearchEngine.php | 52 +++++++++++++++++++++++++++++++++ tests/TNTSearchEngineTest.php | 36 +++++++++++++++++++---- 4 files changed, 91 insertions(+), 15 deletions(-) diff --git a/composer.json b/composer.json index 618c686..5e466fd 100644 --- a/composer.json +++ b/composer.json @@ -8,12 +8,10 @@ "laravel" ], "license": "MIT", - "authors": [ - { - "name": "TNT Studio", - "email": "info@tntstudio.hr" - } - ], + "authors": [{ + "name": "TNT Studio", + "email": "info@tntstudio.hr" + }], "require": { "php": ">=7.1", "teamtnt/tntsearch": "2.*", @@ -26,8 +24,8 @@ "illuminate/support": "~5.4|^6.0|^7.0|^8.0" }, "require-dev": { - "mockery/mockery": "~0.9|^1.3.1", - "phpunit/phpunit": "~5.0|^9.3" + "mockery/mockery": "^1.0", + "phpunit/phpunit": "^8.0|^9.3" }, "autoload": { "psr-4": { @@ -54,4 +52,4 @@ }, "minimum-stability": "dev", "prefer-stable": true -} +} \ No newline at end of file diff --git a/phpunit.xml b/phpunit.xml index e89ac6d..b6adbca 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -1,6 +1,7 @@ diff --git a/src/Engines/TNTSearchEngine.php b/src/Engines/TNTSearchEngine.php index 57d07e6..88193bc 100644 --- a/src/Engines/TNTSearchEngine.php +++ b/src/Engines/TNTSearchEngine.php @@ -13,6 +13,8 @@ class TNTSearchEngine extends Engine { + + private $filters; /** * @var TNTSearch */ @@ -161,6 +163,9 @@ protected function performSearch(Builder $builder, array $options = []) $options ); } + + $builder->query = $this->applyFilters('query_expansion', $builder->query, get_class($builder->model)); + if (isset($this->tnt->config['searchBoolean']) ? $this->tnt->config['searchBoolean'] : false) { $res = $this->tnt->searchBoolean($builder->query, $limit); event(new SearchPerformed($builder, $res, true)); @@ -408,4 +413,51 @@ public function flush($model) unlink($pathToIndex); } } + + /** + * Adds a filter + * + * @param string + * @param callback + * @return void + */ + public function addFilter($name, $callback) + { + if (!is_callable($callback, true)) { + throw new InvalidArgumentException(sprintf('Filter is an invalid callback: %s.', print_r($callback, true))); + } + $this->filters[$name][] = $callback; + } + + /** + * Returns an array of filters + * + * @param string + * @return array + */ + public function getFilters($name) + { + return isset($this->filters[$name]) ? $this->filters[$name] : []; + } + + /** + * Returns a string on which a filter is applied + * + * @param string + * @param string + * @return string + */ + public function applyFilters($name, $result, $model) + { + foreach ($this->getFilters($name) as $callback) { + // prevent fatal errors, do your own warning or + // exception here as you need it. + if (!is_callable($callback)) { + continue; + } + + $result = call_user_func($callback, $result, $model); + } + return $result; + } } diff --git a/tests/TNTSearchEngineTest.php b/tests/TNTSearchEngineTest.php index 22c4285..4d29c23 100644 --- a/tests/TNTSearchEngineTest.php +++ b/tests/TNTSearchEngineTest.php @@ -1,21 +1,25 @@ shouldReceive('createIndex') ->with('table.index') - ->andReturn($index = Mockery::mock('TeamTNT\TNTSearch\Indexer\TNTIndexer')); + ->andReturn($index = m::mock('TeamTNT\TNTSearch\Indexer\TNTIndexer')); $index->shouldReceive('setDatabaseHandle'); $index->shouldReceive('setPrimaryKey'); $index->shouldReceive('query'); @@ -32,6 +36,28 @@ public function test_update_adds_objects_to_index() $engine = new TNTSearchEngine($client); $engine->update(Collection::make([new TNTSearchEngineTestModel()])); } + + public function testApplyingFilters() + { + $tnt = new TNTSearch; + $engine = new TeamTNT\Scout\Engines\TNTSearchEngine($tnt); + + $engine->addFilter("query_expansion", function ($query, $model) { + if ($query == "test" && $model == "TeamTNT\TNTSearch\TNTSearch") { + return "modified-".$query; + } + return $query; + + }); + + $query = $engine->applyFilters('query_expansion', "test", TNTSearch::class); + $query2 = $engine->applyFilters('query_expansion', "test", Collection::class); + $query3 = $engine->applyFilters('query_expansion', "test2", TNTSearch::class); + + $this->assertTrue($query == "modified-test"); + $this->assertTrue($query2 == "test"); + $this->assertTrue($query3 == "test2"); + } } class TNTSearchEngineTestModel