1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BenTools\OpenCubes\Component\Filter\Model; |
4
|
|
|
|
5
|
|
|
use function BenTools\OpenCubes\stringify_uri; |
6
|
|
|
|
7
|
|
|
final class CompositeFilter extends Filter |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* @var string |
11
|
|
|
*/ |
12
|
|
|
private $field; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @var Filter[] |
16
|
|
|
*/ |
17
|
|
|
private $filters = []; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
private $satisfiedBy; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* CompositeFilter constructor. |
26
|
|
|
* @param string $field |
27
|
|
|
* @param array $filters |
28
|
|
|
* @param string $satisfiedBy |
29
|
|
|
* @throws \InvalidArgumentException |
30
|
|
|
*/ |
31
|
|
|
public function __construct(string $field, array $filters, string $satisfiedBy = self::SATISFIED_BY_ALL) |
32
|
|
|
{ |
33
|
|
|
$filters = (function (Filter ...$filters) use ($field) { |
34
|
|
|
foreach ($filters as $filter) { |
35
|
|
|
if ($filter->getField() !== $field) { // Composite filters must share the same field |
36
|
|
|
throw new \InvalidArgumentException( |
37
|
|
|
sprintf('Expected %s filters, got %s', $field, $filter->getField()) |
38
|
|
|
); |
39
|
|
|
} |
40
|
|
|
} |
41
|
|
|
return $filters; |
42
|
|
|
})(...array_values($filters)); |
43
|
|
|
$this->field = $field; |
44
|
|
|
$this->filters = $filters; |
45
|
|
|
$this->satisfiedBy = $satisfiedBy; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @inheritDoc |
50
|
|
|
*/ |
51
|
|
|
public function getField(): string |
52
|
|
|
{ |
53
|
|
|
return $this->field; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @return Filter[] |
58
|
|
|
*/ |
59
|
|
|
public function getFilters(): array |
60
|
|
|
{ |
61
|
|
|
return $this->filters; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @return string |
66
|
|
|
*/ |
67
|
|
|
public function getSatisfiedBy(): string |
68
|
|
|
{ |
69
|
|
|
return $this->satisfiedBy; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @inheritDoc |
74
|
|
|
*/ |
75
|
|
|
public function count(): int |
76
|
|
|
{ |
77
|
|
|
return count($this->filters); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @inheritDoc |
82
|
|
|
*/ |
83
|
|
|
public function getType(): string |
84
|
|
|
{ |
85
|
|
|
return 'composite'; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @inheritDoc |
90
|
|
|
*/ |
91
|
|
|
public function jsonSerialize(): array |
92
|
|
|
{ |
93
|
|
|
$output = [ |
94
|
|
|
'type' => $this->getType(), |
95
|
|
|
'field' => $this->getField(), |
96
|
|
|
'satisfied_by' => $this->getSatisfiedBy(), |
97
|
|
|
'is_applied' => $this->isApplied(), |
98
|
|
|
'is_negated' => $this->isNegated(), |
99
|
|
|
'filters' => $this->getFilters(), |
100
|
|
|
]; |
101
|
|
|
|
102
|
|
|
if ($this->isApplied()) { |
103
|
|
|
$output['unset_link'] = stringify_uri($this->getToggleUri()); |
104
|
|
|
} else { |
105
|
|
|
$output['link'] = stringify_uri($this->getToggleUri()); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
return $output; |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|