1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BenTools\OpenCubes\Component\Sort; |
4
|
|
|
|
5
|
|
|
use BenTools\OpenCubes\Component\ComponentInterface; |
6
|
|
|
use BenTools\OpenCubes\Component\Sort\Model\Sort; |
7
|
|
|
use Countable; |
8
|
|
|
use IteratorAggregate; |
9
|
|
|
use JsonSerializable; |
10
|
|
|
|
11
|
|
|
final class SortComponent implements ComponentInterface, IteratorAggregate, Countable, JsonSerializable |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var Sort[] |
15
|
|
|
*/ |
16
|
|
|
private $sorts = []; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* SortComponent constructor. |
20
|
|
|
* @param array $sorts |
21
|
|
|
*/ |
22
|
|
|
public function __construct(array $sorts) |
23
|
|
|
{ |
24
|
|
|
foreach ($sorts as $sort) { |
25
|
|
|
$this->add($sort); |
26
|
|
|
} |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param Sort $sort |
31
|
|
|
*/ |
32
|
|
|
public function add(Sort $sort): void |
33
|
|
|
{ |
34
|
|
|
$this->sorts[] = $sort; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param string $field |
39
|
|
|
* @param null $direction |
40
|
|
|
* @return bool |
41
|
|
|
*/ |
42
|
|
|
public function has(string $field, $direction = null): bool |
43
|
|
|
{ |
44
|
|
|
if (1 === func_num_args()) { |
45
|
|
|
foreach ($this->sorts as $sort) { |
46
|
|
|
if ($sort->getField() === $field) { |
47
|
|
|
return true; |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
return false; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
foreach ($this->sorts as $sort) { |
55
|
|
|
if ($sort->getField() === $field && $sort->getDirection() === $direction) { |
56
|
|
|
return true; |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
return false; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param string $field |
65
|
|
|
* @param null $direction |
66
|
|
|
* @return Sort[] |
67
|
|
|
*/ |
68
|
|
|
public function get(string $field, $direction = null): iterable |
69
|
|
|
{ |
70
|
|
|
if (1 === func_num_args()) { |
71
|
|
|
foreach ($this->sorts as $sort) { |
72
|
|
|
if ($sort->getField() === $field) { |
73
|
|
|
yield $sort; |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
foreach ($this->sorts as $sort) { |
81
|
|
|
if ($sort->getField() === $field && $sort->getDirection() === $direction) { |
82
|
|
|
yield $sort; |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @return Sort[] |
89
|
|
|
*/ |
90
|
|
|
public function all(): array |
91
|
|
|
{ |
92
|
|
|
return $this->sorts; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @return Sort[] |
97
|
|
|
*/ |
98
|
|
|
public function getAppliedSorts(): array |
99
|
|
|
{ |
100
|
|
|
return array_values( |
101
|
|
|
array_filter( |
102
|
|
|
$this->sorts, |
103
|
|
|
function (Sort $sort) { |
104
|
|
|
return $sort->isApplied(); |
105
|
|
|
} |
106
|
|
|
) |
107
|
|
|
); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @return Sort[] |
112
|
|
|
*/ |
113
|
|
|
public function getIterator(): iterable |
114
|
|
|
{ |
115
|
|
|
return new \ArrayIterator($this->sorts); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @inheritDoc |
120
|
|
|
*/ |
121
|
|
|
public function count(): int |
122
|
|
|
{ |
123
|
|
|
return count($this->sorts); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @inheritDoc |
128
|
|
|
*/ |
129
|
|
|
public static function getName(): string |
130
|
|
|
{ |
131
|
|
|
return 'sort'; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @param string $field |
136
|
|
|
* @param string|null $direction |
137
|
|
|
* @return bool |
138
|
|
|
*/ |
139
|
|
|
public function isApplied(string $field, ?string $direction = null): bool |
140
|
|
|
{ |
141
|
|
|
if (1 === func_num_args()) { |
142
|
|
|
foreach ($this->sorts as $sort) { |
143
|
|
|
if ($sort->getField() === $field && $sort->isApplied()) { |
144
|
|
|
return true; |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
return false; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
foreach ($this->sorts as $sort) { |
152
|
|
|
if ($sort->getField() === $field && $sort->isApplied() && $sort->getDirection() === $direction) { |
153
|
|
|
return true; |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
return false; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @inheritDoc |
162
|
|
|
*/ |
163
|
|
|
public function jsonSerialize(): array |
164
|
|
|
{ |
165
|
|
|
$output = []; |
166
|
|
|
|
167
|
|
|
// Group by field |
168
|
|
|
$fields = array_values( |
169
|
|
|
array_unique( |
170
|
|
|
array_map( |
171
|
|
|
function (Sort $sort): string { |
172
|
|
|
return $sort->getField(); |
173
|
|
|
}, |
174
|
|
|
$this->sorts |
175
|
|
|
) |
176
|
|
|
) |
177
|
|
|
); |
178
|
|
|
|
179
|
|
|
foreach ($fields as $field) { |
180
|
|
|
$output[] = [ |
181
|
|
|
'field' => $field, |
182
|
|
|
'is_applied' => $this->isApplied($field), |
183
|
|
|
'directions' => array_values( |
184
|
|
|
array_filter( |
185
|
|
|
$this->sorts, |
186
|
|
|
function (Sort $sort) use ($field): bool { |
187
|
|
|
return $sort->getField() === $field; |
188
|
|
|
} |
189
|
|
|
) |
190
|
|
|
), |
191
|
|
|
]; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
return [ |
195
|
|
|
'sorts' => $output, |
196
|
|
|
]; |
197
|
|
|
} |
198
|
|
|
} |
199
|
|
|
|