forked from silverstripe/silverstripe-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathListDecorator.php
More file actions
333 lines (292 loc) · 8.67 KB
/
ListDecorator.php
File metadata and controls
333 lines (292 loc) · 8.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
<?php
namespace SilverStripe\ORM;
use SilverStripe\View\ViewableData;
use LogicException;
use SilverStripe\Dev\Deprecation;
use Traversable;
/**
* A base class for decorators that wrap around a list to provide additional
* functionality. It passes through list methods to the underlying list
* implementation.
*
* @template TList of SS_List&Sortable&Filterable&Limitable
* @template T
* @implements SS_List<T>
* @implements Sortable<T>
* @implements Filterable<T>
* @implements Limitable<T>
* @deprecated 5.4.0 Will be renamed to SilverStripe\Model\List\ListDecorator
*/
abstract class ListDecorator extends ViewableData implements SS_List, Sortable, Filterable, Limitable
{
/**
* @var TList<T>
*/
protected SS_List&Sortable&Filterable&Limitable $list;
/**
* @param TList<T> $list
*/
public function __construct(SS_List&Sortable&Filterable&Limitable $list)
{
Deprecation::withSuppressedNotice(function () {
Deprecation::notice('5.4.0', 'Will be renamed to SilverStripe\Model\List\ListDecorator', Deprecation::SCOPE_CLASS);
});
$this->setList($list);
parent::__construct();
}
/**
* @return TList<T>
*/
public function getList(): SS_List&Sortable&Filterable&Limitable
{
return $this->list;
}
/**
* Set the list this decorator wraps around.
*
* Useful for keeping a decorator/paginated list configuration intact while modifying
* the underlying list.
*
* @template TListA
* @template TA
* @param TListA<TA> $list
* @return static<TListA, TA>
*/
public function setList(SS_List&Sortable&Filterable&Limitable $list): ListDecorator
{
$this->list = $list;
$this->failover = $this->list;
return $this;
}
public function offsetExists(mixed $key): bool
{
return $this->list->offsetExists($key);
}
/**
* @return T
*/
public function offsetGet(mixed $key): mixed
{
return $this->list->offsetGet($key);
}
public function offsetSet(mixed $key, mixed $value): void
{
$this->list->offsetSet($key, $value);
}
public function offsetUnset(mixed $key): void
{
$this->list->offsetUnset($key);
}
public function toArray()
{
return $this->list->toArray();
}
public function toNestedArray()
{
return $this->list->toNestedArray();
}
public function add($item)
{
$this->list->add($item);
}
public function remove($itemObject)
{
$this->list->remove($itemObject);
}
/**
* @return Traversable<T>
*/
public function getIterator(): Traversable
{
return $this->list->getIterator();
}
public function exists()
{
return $this->list->exists();
}
public function first()
{
return $this->list->first();
}
public function last()
{
return $this->list->last();
}
public function getTotalItems()
{
return $this->list->count();
}
/**
* @return int
* @depreated 5.4.0 Use getTotalItems() instead.
*/
public function TotalItems()
{
Deprecation::notice('5.4.0', 'Use getTotalItems() instead.');
return $this->getTotalItems();
}
public function Count(): int
{
return $this->list->count();
}
public function forTemplate()
{
return $this->list->forTemplate();
}
public function map($index = 'ID', $titleField = 'Title')
{
return $this->list->map($index, $titleField);
}
public function find($key, $value)
{
return $this->list->find($key, $value);
}
public function column($value = 'ID')
{
return $this->list->column($value);
}
public function columnUnique($value = "ID")
{
return $this->list->columnUnique($value);
}
/**
* @return TList<T>
*/
public function each($callback)
{
return $this->list->each($callback);
}
public function canSortBy($by)
{
return $this->list->canSortBy($by);
}
public function reverse()
{
return $this->list->reverse();
}
/**
* Sorts this list by one or more fields. You can either pass in a single
* field name and direction, or a map of field names to sort directions.
*
* @example $list->sort('Name'); // default ASC sorting
* @example $list->sort('Name DESC'); // DESC sorting
* @example $list->sort('Name', 'ASC');
* @example $list->sort(array('Name'=>'ASC,'Age'=>'DESC'));
*
* @return TList<T>
*/
public function sort()
{
return $this->list->sort(...func_get_args());
}
public function canFilterBy($by)
{
return $this->list->canFilterBy($by);
}
/**
* Filter the list to include items with these characteristics
*
* @example $list->filter('Name', 'bob'); // only bob in list
* @example $list->filter('Name', array('aziz', 'bob'); // aziz and bob in list
* @example $list->filter(array('Name'=>'bob, 'Age'=>21)); // bob or someone with Age 21
* @example $list->filter(array('Name'=>'bob, 'Age'=>array(21, 43))); // bob or anyone with Age 21 or 43
*
* @return TList<T>
*/
public function filter()
{
return $this->list->filter(...func_get_args());
}
/**
* Return a copy of this list which contains items matching any of these characteristics.
*
* @example // only bob in the list
* $list = $list->filterAny('Name', 'bob');
* // SQL: WHERE "Name" = 'bob'
* @example // azis or bob in the list
* $list = $list->filterAny('Name', array('aziz', 'bob');
* // SQL: WHERE ("Name" IN ('aziz','bob'))
* @example // bob or anyone aged 21 in the list
* $list = $list->filterAny(array('Name'=>'bob, 'Age'=>21));
* // SQL: WHERE ("Name" = 'bob' OR "Age" = '21')
* @example // bob or anyone aged 21 or 43 in the list
* $list = $list->filterAny(array('Name'=>'bob, 'Age'=>array(21, 43)));
* // SQL: WHERE ("Name" = 'bob' OR ("Age" IN ('21', '43'))
* @example // all bobs, phils or anyone aged 21 or 43 in the list
* $list = $list->filterAny(array('Name'=>array('bob','phil'), 'Age'=>array(21, 43)));
* // SQL: WHERE (("Name" IN ('bob', 'phil')) OR ("Age" IN ('21', '43'))
*
* @param string|array See {@link filter()}
*
* @return TList<T>
*/
public function filterAny()
{
return $this->list->filterAny(...func_get_args());
}
/**
* Note that, in the current implementation, the filtered list will be an ArrayList, but this may change in a
* future implementation.
* @see Filterable::filterByCallback()
*
* @example $list = $list->filterByCallback(function($item, $list) { return $item->Age == 9; })
* @param callable $callback
* @return ArrayList<T>
*/
public function filterByCallback($callback)
{
if (!is_callable($callback)) {
throw new LogicException(sprintf(
"SS_Filterable::filterByCallback() passed callback must be callable, '%s' given",
gettype($callback)
));
}
$output = ArrayList::create();
foreach ($this->list as $item) {
if ($callback($item, $this->list)) {
$output->push($item);
}
}
return $output;
}
/**
* @return TList<T>
*/
public function limit(?int $length, int $offset = 0): SS_List&Sortable&Filterable&Limitable
{
return $this->list->limit($length, $offset);
}
public function byID($id)
{
return $this->list->byID($id);
}
/**
* Filter this list to only contain the given Primary IDs
*
* @param array $ids Array of integers
*
* @return TList<T>
*/
public function byIDs($ids)
{
return $this->list->byIDs($ids);
}
/**
* Exclude the list to not contain items with these characteristics
*
* @example $list->exclude('Name', 'bob'); // exclude bob from list
* @example $list->exclude('Name', array('aziz', 'bob'); // exclude aziz and bob from list
* @example $list->exclude(array('Name'=>'bob, 'Age'=>21)); // exclude bob or someone with Age 21
* @example $list->exclude(array('Name'=>'bob, 'Age'=>array(21, 43))); // exclude bob or anyone with Age 21 or 43
*
* @return TList<T>
*/
public function exclude()
{
return $this->list->exclude(...func_get_args());
}
public function debug()
{
return $this->list->debug();
}
}