Skip to content

Commit 3555f54

Browse files
committed
adding filters
1 parent a8a4c30 commit 3555f54

File tree

4 files changed

+91
-15
lines changed

4 files changed

+91
-15
lines changed

composer.json

+7-9
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,10 @@
88
"laravel"
99
],
1010
"license": "MIT",
11-
"authors": [
12-
{
13-
"name": "TNT Studio",
14-
"email": "info@tntstudio.hr"
15-
}
16-
],
11+
"authors": [{
12+
"name": "TNT Studio",
13+
"email": "info@tntstudio.hr"
14+
}],
1715
"require": {
1816
"php": ">=7.1",
1917
"teamtnt/tntsearch": "2.*",
@@ -26,8 +24,8 @@
2624
"illuminate/support": "~5.4|^6.0|^7.0|^8.0"
2725
},
2826
"require-dev": {
29-
"mockery/mockery": "~0.9|^1.3.1",
30-
"phpunit/phpunit": "~5.0|^9.3"
27+
"mockery/mockery": "^1.0",
28+
"phpunit/phpunit": "^8.0|^9.3"
3129
},
3230
"autoload": {
3331
"psr-4": {
@@ -54,4 +52,4 @@
5452
},
5553
"minimum-stability": "dev",
5654
"prefer-stable": true
57-
}
55+
}

phpunit.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<phpunit backupGlobals="false"
33
backupStaticAttributes="false"
4+
beStrictAboutTestsThatDoNotTestAnything="false"
45
bootstrap="vendor/autoload.php"
56
colors="true"
67
convertErrorsToExceptions="true"
78
convertNoticesToExceptions="true"
89
convertWarningsToExceptions="true"
910
processIsolation="false"
1011
stopOnFailure="false"
11-
syntaxCheck="false"
1212
>
1313
<testsuites>
1414
<testsuite name="Package Test Suite">

src/Engines/TNTSearchEngine.php

+52
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
class TNTSearchEngine extends Engine
1515
{
16+
17+
private $filters;
1618
/**
1719
* @var TNTSearch
1820
*/
@@ -161,6 +163,9 @@ protected function performSearch(Builder $builder, array $options = [])
161163
$options
162164
);
163165
}
166+
167+
$builder->query = $this->applyFilters('query_expansion', $builder->query, get_class($builder->model));
168+
164169
if (isset($this->tnt->config['searchBoolean']) ? $this->tnt->config['searchBoolean'] : false) {
165170
$res = $this->tnt->searchBoolean($builder->query, $limit);
166171
event(new SearchPerformed($builder, $res, true));
@@ -408,4 +413,51 @@ public function flush($model)
408413
unlink($pathToIndex);
409414
}
410415
}
416+
417+
/**
418+
* Adds a filter
419+
*
420+
* @param string
421+
* @param callback
422+
* @return void
423+
*/
424+
public function addFilter($name, $callback)
425+
{
426+
if (!is_callable($callback, true)) {
427+
throw new InvalidArgumentException(sprintf('Filter is an invalid callback: %s.', print_r($callback, true)));
428+
}
429+
$this->filters[$name][] = $callback;
430+
}
431+
432+
/**
433+
* Returns an array of filters
434+
*
435+
* @param string
436+
* @return array
437+
*/
438+
public function getFilters($name)
439+
{
440+
return isset($this->filters[$name]) ? $this->filters[$name] : [];
441+
}
442+
443+
/**
444+
* Returns a string on which a filter is applied
445+
*
446+
* @param string
447+
* @param string
448+
* @return string
449+
*/
450+
public function applyFilters($name, $result, $model)
451+
{
452+
foreach ($this->getFilters($name) as $callback) {
453+
// prevent fatal errors, do your own warning or
454+
// exception here as you need it.
455+
if (!is_callable($callback)) {
456+
continue;
457+
}
458+
459+
$result = call_user_func($callback, $result, $model);
460+
}
461+
return $result;
462+
}
411463
}

tests/TNTSearchEngineTest.php

+31-5
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,25 @@
11
<?php
22

33
use Illuminate\Database\Eloquent\Collection;
4+
use Mockery as m;
5+
use PHPUnit\Framework\TestCase;
46
use TeamTNT\Scout\Engines\TNTSearchEngine;
7+
use TeamTNT\TNTSearch\TNTSearch;
58

6-
class TNTSearchEngineTest extends PHPUnit_Framework_TestCase
9+
class TNTSearchEngineTest extends TestCase
710
{
8-
public function tearDown()
11+
12+
protected function tearDown(): void
913
{
10-
Mockery::close();
14+
m::close();
1115
}
1216

1317
public function test_update_adds_objects_to_index()
1418
{
15-
$client = Mockery::mock('TeamTNT\TNTSearch\TNTSearch');
19+
$client = m::mock('TeamTNT\TNTSearch\TNTSearch');
1620
$client->shouldReceive('createIndex')
1721
->with('table.index')
18-
->andReturn($index = Mockery::mock('TeamTNT\TNTSearch\Indexer\TNTIndexer'));
22+
->andReturn($index = m::mock('TeamTNT\TNTSearch\Indexer\TNTIndexer'));
1923
$index->shouldReceive('setDatabaseHandle');
2024
$index->shouldReceive('setPrimaryKey');
2125
$index->shouldReceive('query');
@@ -32,6 +36,28 @@ public function test_update_adds_objects_to_index()
3236
$engine = new TNTSearchEngine($client);
3337
$engine->update(Collection::make([new TNTSearchEngineTestModel()]));
3438
}
39+
40+
public function testApplyingFilters()
41+
{
42+
$tnt = new TNTSearch;
43+
$engine = new TeamTNT\Scout\Engines\TNTSearchEngine($tnt);
44+
45+
$engine->addFilter("query_expansion", function ($query, $model) {
46+
if ($query == "test" && $model == "TeamTNT\TNTSearch\TNTSearch") {
47+
return "modified-".$query;
48+
}
49+
return $query;
50+
51+
});
52+
53+
$query = $engine->applyFilters('query_expansion', "test", TNTSearch::class);
54+
$query2 = $engine->applyFilters('query_expansion', "test", Collection::class);
55+
$query3 = $engine->applyFilters('query_expansion', "test2", TNTSearch::class);
56+
57+
$this->assertTrue($query == "modified-test");
58+
$this->assertTrue($query2 == "test");
59+
$this->assertTrue($query3 == "test2");
60+
}
3561
}
3662

3763
class TNTSearchEngineTestModel

0 commit comments

Comments
 (0)