-
Notifications
You must be signed in to change notification settings - Fork 1
/
FiltersTrait.php
98 lines (82 loc) · 2.03 KB
/
FiltersTrait.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
<?php
/*
* doctrine-base-repositories (https://github.com/juliangut/doctrine-base-repositories).
* Doctrine2 utility repositories.
*
* @license MIT
* @link https://github.com/juliangut/doctrine-base-repositories
* @author Julián Gutiérrez <juliangut@gmail.com>
*/
declare(strict_types=1);
namespace Jgut\Doctrine\Repository;
/**
* Filters trait.
*/
trait FiltersTrait
{
/**
* List of disabled filters.
*
* @var object[]
*/
protected $disabledFilters = [];
/**
* Disable all filters.
*/
public function disableFilters()
{
foreach (array_keys($this->getFilterCollection()->getEnabledFilters()) as $filter) {
$this->disableFilter($filter);
}
}
/**
* Disable filter.
*
* @param string $filter
*/
public function disableFilter(string $filter)
{
if (in_array($filter, $this->disabledFilters, true)) {
return;
}
$this->getFilterCollection()->disable($filter);
$this->disabledFilters[] = $filter;
}
/**
* Restore all disabled filters.
*/
public function restoreFilters()
{
$filterCollection = $this->getFilterCollection();
foreach ($this->disabledFilters as $filter) {
$filterCollection->enable($filter);
}
$this->disabledFilters = [];
}
/**
* Restore disabled filter.
*
* @param string $filter
*/
public function restoreFilter(string $filter)
{
$position = array_search($filter, $this->disabledFilters, true);
if ($position === false) {
return;
}
$this->getFilterCollection()->enable($filter);
array_splice($this->disabledFilters, $position, 1);
}
/**
* Get filter collection.
*
* @return object
*/
abstract protected function getFilterCollection();
/**
* Get object manager.
*
* @return \Doctrine\Common\Persistence\ObjectManager
*/
abstract protected function getManager();
}