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 a419c5804a
4 changed files with 36 additions and 6 deletions

View File

@ -62,6 +62,7 @@ abstract class AbstractFilterer
$filterKey = substr($filterKey, 1); $filterKey = substr($filterKey, 1);
} }
foreach (Arr::get($this->filters, $filterKey, []) as $filter) { foreach (Arr::get($this->filters, $filterKey, []) as $filter) {
$filterState->addActiveFilter($filter);
$filter->filter($filterState, $filterValue, $negate); $filter->filter($filterState, $filterValue, $negate);
} }
} }
@ -77,7 +78,7 @@ abstract class AbstractFilterer
// END DEPRECATED BC LAYER // END DEPRECATED BC LAYER
foreach ($this->filterMutators as $mutator) { 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 // 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 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 = [ $params = [
'sort' => $sort && isset($sortMap[$sort]) ? $sortMap[$sort] : '', 'sort' => $sort && isset($sortMap[$sort]) ? $sortMap[$sort] : '',
'filter' => compact('q'), 'filter' => [],
'page' => ['offset' => ($page - 1) * 20, 'limit' => 20] 'page' => ['offset' => ($page - 1) * 20, 'limit' => 20]
]; ];
if ($q) {
$params['filter']['q'] = $q;
}
$apiDocument = $this->getApiDocument($request->getAttribute('actor'), $params); $apiDocument = $this->getApiDocument($request->getAttribute('actor'), $params);
$defaultRoute = $this->settings->get('default_route'); $defaultRoute = $this->settings->get('default_route');

View File

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