Merge pull request #2684 from flarum/as/filter-mutator-consistency

Make filter mutator API consistent with search mutator API.
This commit is contained in:
Alexander Skvortsov 2021-03-13 17:01:33 -05:00 committed by GitHub
commit 9c3b6c596f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 36 additions and 6 deletions

View File

@ -62,6 +62,7 @@ abstract class AbstractFilterer
$filterKey = substr($filterKey, 1);
}
foreach (Arr::get($this->filters, $filterKey, []) as $filter) {
$filterState->addActiveFilter($filter);
$filter->filter($filterState, $filterValue, $negate);
}
}
@ -77,7 +78,7 @@ abstract class AbstractFilterer
// END DEPRECATED BC LAYER
foreach ($this->filterMutators as $mutator) {
$mutator($query, $actor, $criteria->query, $criteria->sort);
$mutator($filterState, $criteria);
}
// Execute the filter query and retrieve the results. We get one more

View File

@ -13,4 +13,29 @@ use Flarum\Query\AbstractQueryState;
class FilterState extends AbstractQueryState
{
/**
* @var FilterInterface[]
*/
protected $activeFilters = [];
/**
* Get a list of the filters that are active.
*
* @return FilterInterface[]
*/
public function getActiveFilters()
{
return $this->activeFilters;
}
/**
* Add a filter as being active.
*
* @param FilterInterface $filter
* @return void
*/
public function addActiveFilter(FilterInterface $filter)
{
$this->activeFilters[] = $filter;
}
}

View File

@ -75,10 +75,14 @@ class Index
$params = [
'sort' => $sort && isset($sortMap[$sort]) ? $sortMap[$sort] : '',
'filter' => compact('q'),
'filter' => [],
'page' => ['offset' => ($page - 1) * 20, 'limit' => 20]
];
if ($q) {
$params['filter']['q'] = $q;
}
$apiDocument = $this->getApiDocument($request->getAttribute('actor'), $params);
$defaultRoute = $this->settings->get('default_route');

View File

@ -84,8 +84,8 @@ class FilterTest extends TestCase
*/
public function filter_mutator_has_effect_if_added()
{
$this->extend((new Extend\Filter(DiscussionFilterer::class))->addFilterMutator(function ($query, $actor, $filters, $sort) {
$query->getQuery()->whereRaw('1=0');
$this->extend((new Extend\Filter(DiscussionFilterer::class))->addFilterMutator(function ($filterState, $criteria) {
$filterState->getQuery()->whereRaw('1=0');
}));
$this->prepDb();
@ -127,8 +127,8 @@ class NoResultFilter implements FilterInterface
class CustomFilterMutator
{
public function __invoke($query, $actor, $filters, $sort)
public function __invoke($filterState, $criteria)
{
$query->getQuery()->whereRaw('1=0');
$filterState->getQuery()->whereRaw('1=0');
}
}