Skip to content

Commit

Permalink
feat: allow replacing filters
Browse files Browse the repository at this point in the history
  • Loading branch information
SychO9 committed Oct 21, 2023
1 parent 701f9c7 commit b477a41
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 3 deletions.
37 changes: 34 additions & 3 deletions framework/core/src/Extend/SearchDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Flarum\Search\SearchCriteria;
use Flarum\Search\SearchState;
use Illuminate\Contracts\Container\Container;
use Illuminate\Support\Arr;

class SearchDriver implements ExtenderInterface
{
Expand Down Expand Up @@ -64,7 +65,28 @@ public function addSearcher(string $modelClass, string $searcherClass): self
*/
public function addFilter(string $searcherClass, string $filterClass): self
{
$this->filters[$searcherClass][] = $filterClass;
$this->filters[$searcherClass][] = compact('filterClass');

return $this;
}

/**
* Replace a filter from this searcher. Filters are used to filter search queries.
*
* @param class-string<AbstractSearcher> $searcherClass : The class of the Searcher for this model
* This searcher must implement \Flarum\Search\SearcherInterface.
* Or extend \Flarum\Search\Database\AbstractSearcher if using the default driver.
* @param class-string<FilterInterface> $replaceFilterClass : The ::class attribute of the filter you are replacing.
* @param class-string<FilterInterface> $filterClass : The ::class attribute of the filter you are adding.
* This filter must implement \Flarum\Search\FilterInterface
* @return self
*/
public function replaceFilter(string $searcherClass, string $replaceFilterClass, string $filterClass): self
{
$this->filters[$searcherClass][] = [
'replace' => $replaceFilterClass,
'filterClass' => $filterClass,
];

return $this;
}
Expand Down Expand Up @@ -130,9 +152,18 @@ public function extend(Container $container, Extension $extension = null): void

$container->extend('flarum.search.filters', function (array $oldFilters) {
foreach ($this->filters as $searcherClass => $filters) {
$oldFilters[$searcherClass] = array_merge(
// Start by removing any filters that will be replaced.
$oldFilters[$searcherClass] = array_filter(
$oldFilters[$searcherClass] ?? [],
$filters
function ($filter) use ($filters) {
return ! in_array($filter, Arr::pluck($filters, 'replace'));
}
);

// Add the new filters.
$oldFilters[$searcherClass] = array_merge(
$oldFilters[$searcherClass],
Arr::pluck($filters, 'filterClass')
);
}

Expand Down
18 changes: 18 additions & 0 deletions framework/core/tests/integration/extenders/SearchDriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Carbon\Carbon;
use Flarum\Discussion\Discussion;
use Flarum\Discussion\Search\DiscussionSearcher;
use Flarum\Discussion\Search\Filter\UnreadFilter;
use Flarum\Extend;
use Flarum\Search\AbstractFulltextFilter;
use Flarum\Search\Database\DatabaseSearchDriver;
Expand Down Expand Up @@ -124,6 +125,23 @@ public function custom_filter_has_effect_if_added()
$this->assertEquals('[]', json_encode($this->searchDiscussions('', 5, ['noResult' => '1'])));
}

/**
* @test
*/
public function existing_filter_can_be_replaced()
{
$this->extend(
(new Extend\SearchDriver(DatabaseSearchDriver::class))
->replaceFilter(DiscussionSearcher::class, UnreadFilter::class, NoResultFilter::class)
);

$this->prepDb();

$this->assertNotContains(UnreadFilter::class, $this->app()->getContainer()->make('flarum.search.filters')[DiscussionSearcher::class]);
$this->assertContains(NoResultFilter::class, $this->app()->getContainer()->make('flarum.search.filters')[DiscussionSearcher::class]);
$this->assertEquals('[]', json_encode($this->searchDiscussions('', 5, ['noResult' => '1'])));
}

/**
* @test
*/
Expand Down

0 comments on commit b477a41

Please sign in to comment.