1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BenTools\OpenCubes\Component\Filter\Model; |
4
|
|
|
|
5
|
|
|
use function BenTools\OpenCubes\stringify_uri; |
6
|
|
|
|
7
|
|
|
final class CollectionFilter extends Filter implements \Countable |
8
|
|
|
{ |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @var string |
12
|
|
|
*/ |
13
|
|
|
private $field; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var FilterValue[] |
17
|
|
|
*/ |
18
|
|
|
private $values; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
private $satisfiedBy; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* CollectionFilter constructor. |
27
|
|
|
* @param string $field |
28
|
|
|
* @param Filtervalue[] $values |
29
|
|
|
* @param string $satisfiedBy |
30
|
|
|
* @throws \InvalidArgumentException |
31
|
|
|
*/ |
32
|
|
|
public function __construct(string $field, array $values = [], string $satisfiedBy = self::SATISFIED_BY_ANY) |
33
|
|
|
{ |
34
|
|
|
if (!in_array($satisfiedBy, [self::SATISFIED_BY_ANY, self::SATISFIED_BY_ALL])) { |
35
|
|
|
throw new \InvalidArgumentException(sprintf('Invalid "satisfiedBy" condition for %s', $field)); |
36
|
|
|
} |
37
|
|
|
$this->field = $field; |
38
|
|
|
$this->values = (function (FilterValue ... $filterValues) { |
39
|
|
|
return $filterValues; |
40
|
|
|
})(...$values); |
41
|
|
|
$this->satisfiedBy = $satisfiedBy; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param string $field |
46
|
|
|
* @param array $values |
47
|
|
|
* @param string $satisfiedBy |
48
|
|
|
* @return CollectionFilter |
49
|
|
|
* @throws \InvalidArgumentException |
50
|
|
|
*/ |
51
|
|
|
public static function createFromValues(string $field, array $values, string $satisfiedBy = self::SATISFIED_BY_ANY) |
52
|
|
|
{ |
53
|
|
|
return new self( |
54
|
|
|
$field, |
55
|
|
|
array_map(function ($value): FilterValue { |
56
|
|
|
return new FilterValue($value); |
57
|
|
|
}, $values), |
58
|
|
|
$satisfiedBy |
59
|
|
|
); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @inheritDoc |
64
|
|
|
*/ |
65
|
|
|
public function getValues(): array |
66
|
|
|
{ |
67
|
|
|
return array_map(function (FilterValue $value) { |
68
|
|
|
return $value->getValue(); |
69
|
|
|
}, $this->values); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @return FilterValue[] |
74
|
|
|
*/ |
75
|
|
|
public function getFilterValues() |
76
|
|
|
{ |
77
|
|
|
return $this->values; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/*** |
81
|
|
|
* @param $value |
82
|
|
|
* @return CollectionFilter |
83
|
|
|
*/ |
84
|
|
|
public function withoutValue($value): self |
85
|
|
|
{ |
86
|
|
|
$clone = clone $this; |
87
|
|
|
foreach ($clone->values as $v => $filterValue) { |
88
|
|
|
if ($filterValue->getValue() === $value) { |
89
|
|
|
unset($clone->values[$v]); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
$clone->values = array_values($clone->values); |
94
|
|
|
|
95
|
|
|
return $clone; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param $value |
100
|
|
|
* @return bool |
101
|
|
|
*/ |
102
|
|
|
public function contains($value): bool |
103
|
|
|
{ |
104
|
|
|
if (null === $value) { |
105
|
|
|
return in_array(null, $this->getValues(), true); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
return in_array((string) $value, $this->getValues(), true); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @inheritDoc |
113
|
|
|
*/ |
114
|
|
|
public function getField(): string |
115
|
|
|
{ |
116
|
|
|
return $this->field; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @return string |
121
|
|
|
*/ |
122
|
|
|
public function getSatisfiedBy(): string |
123
|
|
|
{ |
124
|
|
|
return $this->satisfiedBy; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @inheritDoc |
129
|
|
|
*/ |
130
|
|
|
public function count() |
131
|
|
|
{ |
132
|
|
|
return count($this->values); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @inheritDoc |
137
|
|
|
*/ |
138
|
|
|
public function getType(): string |
139
|
|
|
{ |
140
|
|
|
return 'collection'; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @inheritDoc |
145
|
|
|
*/ |
146
|
|
|
public function jsonSerialize(): array |
147
|
|
|
{ |
148
|
|
|
$output = [ |
149
|
|
|
'type' => $this->getType(), |
150
|
|
|
'field' => $this->getField(), |
151
|
|
|
'satisfied_by' => $this->getSatisfiedBy(), |
152
|
|
|
'is_applied' => $this->isApplied(), |
153
|
|
|
'is_negated' => $this->isNegated(), |
154
|
|
|
'values' => $this->getFilterValues(), |
155
|
|
|
]; |
156
|
|
|
|
157
|
|
|
if ($this->isApplied()) { |
158
|
|
|
$output['unset_link'] = stringify_uri($this->getToggleUri()); |
159
|
|
|
} else { |
160
|
|
|
$output['link'] = stringify_uri($this->getToggleUri()); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
return $output; |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
|