|
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 JsonSerializable; |
|
15
|
|
|
use WBW\Bundle\JQuery\QueryBuilderBundle\Decorator\QueryBuilderDecoratorInterface; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* jQuery QueryBuilder filter set. |
|
19
|
|
|
* |
|
20
|
|
|
* @author webeweb <https://github.com/webeweb/> |
|
21
|
|
|
* @package WBW\Bundle\JQuery\QueryBuilderBundle\API |
|
22
|
|
|
*/ |
|
23
|
|
|
class QueryBuilderFilterSet implements JsonSerializable { |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Decorators. |
|
27
|
|
|
* |
|
28
|
|
|
* @var QueryBuilderDecoratorInterface[] |
|
29
|
|
|
*/ |
|
30
|
|
|
private $decorators = []; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Filters. |
|
34
|
|
|
* |
|
35
|
|
|
* @var QueryBuilderFilter[] |
|
36
|
|
|
*/ |
|
37
|
|
|
private $filters = []; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Constructor. |
|
41
|
|
|
*/ |
|
42
|
|
|
public function __construcct() { |
|
43
|
|
|
// NOTHING TO DO. |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Add a filter. |
|
48
|
|
|
* |
|
49
|
|
|
* @param QueryBuilderFilter $filter The filter. |
|
50
|
|
|
* @return QueryBuilderFilterSet Returns the QueryBuilder filter set. |
|
51
|
|
|
*/ |
|
52
|
|
|
final public function addFilter(QueryBuilderFilter $filter) { |
|
53
|
|
|
if (true === ($filter instanceof QueryBuilderDecoratorInterface)) { |
|
54
|
|
|
$this->decorators[$filter->getId()] = $filter; |
|
55
|
|
|
} |
|
56
|
|
|
$this->filters[$filter->getId()] = $filter; |
|
57
|
|
|
return $this; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Get a decorator. |
|
62
|
|
|
* |
|
63
|
|
|
* @param string $id The id. |
|
64
|
|
|
* @return QueryBuilderDecoratorInterface Returns the decorator in case of success, null otherwise. |
|
65
|
|
|
*/ |
|
66
|
|
|
final public function getDecorator($id) { |
|
67
|
|
|
if (false === array_key_exists($id, $this->decorators)) { |
|
68
|
|
|
return null; |
|
69
|
|
|
} |
|
70
|
|
|
return $this->decorators[$id]; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Get the filters. |
|
75
|
|
|
* |
|
76
|
|
|
* @return QueryBuilderFilter[] Returns the filters. |
|
77
|
|
|
*/ |
|
78
|
|
|
final public function getFilters() { |
|
79
|
|
|
return $this->filters; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Serialize this instance. |
|
84
|
|
|
* |
|
85
|
|
|
* @return array Returns an array representing this instance. |
|
86
|
|
|
*/ |
|
87
|
|
|
final public function jsonSerialize() { |
|
88
|
|
|
return $this->toArray(); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Remove a filter. |
|
93
|
|
|
* |
|
94
|
|
|
* @param QueryBuilderFilter $filter The filter. |
|
95
|
|
|
* @return QueryBuilderFilterSet Returns the QueryBuilder filter set. |
|
96
|
|
|
*/ |
|
97
|
|
|
final public function removeFilter(QueryBuilderFilter $filter) { |
|
98
|
|
|
if (true === array_key_exists($filter->getId(), $this->filters)) { |
|
99
|
|
|
unset($this->filters[$filter->getId()]); |
|
100
|
|
|
} |
|
101
|
|
|
return $this; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Convert into an array representing this instance. |
|
106
|
|
|
* |
|
107
|
|
|
* @return array Returns an array representing this instance. |
|
108
|
|
|
*/ |
|
109
|
|
|
final public function toArray() { |
|
110
|
|
|
|
|
111
|
|
|
// Initialize the output. |
|
112
|
|
|
$output = []; |
|
113
|
|
|
|
|
114
|
|
|
// Handle each filter. |
|
115
|
|
|
foreach ($this->filters as $current) { |
|
116
|
|
|
$output[] = $current->toArray(); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
// Return the output. |
|
120
|
|
|
return $output; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
} |
|
124
|
|
|
|