1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the jquery-querybuilder-bundle package. |
5
|
|
|
* |
6
|
|
|
* (c) 2017 WEBEWEB |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace WBW\Bundle\JQuery\QueryBuilderBundle\API; |
13
|
|
|
|
14
|
|
|
use WBW\Bundle\JQuery\QueryBuilderBundle\Normalizer\QueryBuilderNormalizer; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* QueryBuilder filter set. |
18
|
|
|
* |
19
|
|
|
* @author webeweb <https://github.com/webeweb/> |
20
|
|
|
* @package WBW\Bundle\JQuery\QueryBuilderBundle\API |
21
|
|
|
*/ |
22
|
|
|
class QueryBuilderFilterSet implements QueryBuilderFilterSetInterface { |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Filters. |
26
|
|
|
* |
27
|
|
|
* @var QueryBuilderFilterInterface[] |
28
|
|
|
*/ |
29
|
|
|
private $filters; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Constructor. |
33
|
|
|
*/ |
34
|
|
|
public function __construct() { |
35
|
|
|
$this->setFilters([]); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* {@inheritDoc} |
40
|
|
|
*/ |
41
|
|
|
public function addFilter(QueryBuilderFilterInterface $filter) { |
42
|
|
|
$this->filters[$filter->getId()] = $filter; |
43
|
|
|
return $this; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* {@inheritDoc} |
48
|
|
|
*/ |
49
|
|
|
public function getDecorator($id) { |
50
|
|
|
$filter = $this->getFilter($id); |
51
|
|
|
if (null === $filter) { |
52
|
|
|
return $filter; |
53
|
|
|
} |
54
|
|
|
return $filter->getDecorator(); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* {@inheritDoc} |
59
|
|
|
*/ |
60
|
|
|
public function getFilter($id) { |
61
|
|
|
if (false === array_key_exists($id, $this->filters)) { |
62
|
|
|
return null; |
63
|
|
|
} |
64
|
|
|
return $this->filters[$id]; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* {@inheritDoc} |
69
|
|
|
*/ |
70
|
|
|
public function getFilters() { |
71
|
|
|
return $this->filters; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Serialize this instance. |
76
|
|
|
* |
77
|
|
|
* @return array Returns an array representing this instance. |
78
|
|
|
*/ |
79
|
|
|
public function jsonSerialize() { |
80
|
|
|
return QueryBuilderNormalizer::normalizeQueryBuilderFilterSet($this); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* {@inheritDoc} |
85
|
|
|
*/ |
86
|
|
|
public function removeFilter(QueryBuilderFilterInterface $filter) { |
87
|
|
|
if (true === array_key_exists($filter->getId(), $this->filters)) { |
88
|
|
|
unset($this->filters[$filter->getId()]); |
89
|
|
|
} |
90
|
|
|
return $this; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Set the filters. |
95
|
|
|
* |
96
|
|
|
* @param QueryBuilderFilterInterface[] $filters The filters. |
97
|
|
|
* @return QueryBuilderFilterSetInterface Returns this filter set. |
98
|
|
|
*/ |
99
|
|
|
protected function setFilters(array $filters) { |
100
|
|
|
$this->filters = $filters; |
101
|
|
|
return $this; |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|