/
Filter.php
79 lines (67 loc) · 2.08 KB
/
Filter.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<?php
/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/
namespace Flarum\Extend;
use Flarum\Extension\Extension;
use Illuminate\Contracts\Container\Container;
class Filter implements ExtenderInterface
{
private $filtererClass;
private $filters = [];
private $filterMutators = [];
/**
* @param string $filtererClass: The ::class attribute of the filterer to extend.
*/
public function __construct($filtererClass)
{
$this->filtererClass = $filtererClass;
}
/**
* Add a filter to run when the filtererClass is filtered.
*
* @param string $filterClass: The ::class attribute of the filter you are adding.
* @return self
*/
public function addFilter(string $filterClass): self
{
$this->filters[] = $filterClass;
return $this;
}
/**
* Add a callback through which to run all filter queries after filters have been applied.
*
* @param callable|string $callback
*
* The callback can be a closure or an invokable class, and should accept:
* - Flarum\Filter\FilterState $filter
* - Flarum\Query\QueryCriteria $criteria
*
* The callable should return void.
*
* @return self
*/
public function addFilterMutator($callback): self
{
$this->filterMutators[] = $callback;
return $this;
}
public function extend(Container $container, Extension $extension = null)
{
$container->extend('flarum.filter.filters', function ($originalFilters) {
foreach ($this->filters as $filter) {
$originalFilters[$this->filtererClass][] = $filter;
}
return $originalFilters;
});
$container->extend('flarum.filter.filter_mutators', function ($originalMutators) {
foreach ($this->filterMutators as $mutator) {
$originalMutators[$this->filtererClass][] = $mutator;
}
return $originalMutators;
});
}
}