-
Notifications
You must be signed in to change notification settings - Fork 113
/
WidgetGroup.php
324 lines (280 loc) · 6.61 KB
/
WidgetGroup.php
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
<?php
namespace Arrilot\Widgets;
use Arrilot\Widgets\Contracts\ApplicationWrapperContract;
use Arrilot\Widgets\Misc\ViewExpressionTrait;
class WidgetGroup
{
use ViewExpressionTrait;
/**
* The widget group name.
*
* @var string
*/
protected $name;
/**
* The application wrapper.
*
* @var ApplicationWrapperContract
*/
protected $app;
/**
* The array of widgets to display in this group.
*
* @var array
*/
protected $widgets = [];
/**
* The position of a widget in this group.
*
* @var int
*/
protected $position = 100;
/**
* The separator to display between widgets in the group.
*
* @var string
*/
protected $separator = '';
/**
* Id that is going to be issued to the next widget when it's added to the group.
*
* @var int
*/
protected $nextWidgetId = 1;
/**
* A callback that defines extra markup that wraps every widget in the group.
*
* @var callable|null
*/
protected $wrapCallback;
/**
* @param $name
* @param ApplicationWrapperContract $app
*/
public function __construct($name, ApplicationWrapperContract $app)
{
$this->name = $name;
$this->app = $app;
}
/**
* Display all widgets from this group in correct order.
*
* @return string
*/
public function display()
{
ksort($this->widgets);
$output = '';
$index = 0;
$count = $this->count();
foreach ($this->widgets as $position => $widgets) {
foreach ($widgets as $widget) {
$output .= $this->performWrap($this->displayWidget($widget), $index, $count);
$index++;
if ($index !== $count) {
$output .= $this->separator;
}
}
}
return $this->convertToViewExpression($output);
}
/**
* Remove a widget by its id.
*
* @param int $id
*/
public function removeById($id)
{
foreach ($this->widgets as $position => $widgets) {
foreach ($widgets as $i => $widget) {
if ($widget['id'] === $id) {
unset($this->widgets[$position][$i]);
return;
}
}
}
}
/**
* Remove all widgets with $name from the group.
*
* @param string $name
*/
public function removeByName($name)
{
foreach ($this->widgets as $position => $widgets) {
foreach ($widgets as $i => $widget) {
if ($widget['arguments'][0] === $name) {
unset($this->widgets[$position][$i]);
}
}
}
}
/**
* Remove all widgets from $position from the group.
*
* @param int|string $position
*/
public function removeByPosition($position)
{
if (array_key_exists($position, $this->widgets)) {
unset($this->widgets[$position]);
}
}
/**
* Remove all widgets from the group.
*/
public function removeAll()
{
$this->widgets = [];
}
/**
* Set widget position.
*
* @param int $position
*
* @return $this
*/
public function position($position)
{
$this->position = $position;
return $this;
}
/**
* Add a widget to the group.
*/
public function addWidget()
{
return $this->addWidgetWithType('sync', func_get_args());
}
/**
* Add an async widget to the group.
*/
public function addAsyncWidget()
{
return $this->addWidgetWithType('async', func_get_args());
}
/**
* Getter for position.
*
* @return int
*/
public function getPosition()
{
return $this->position;
}
/**
* Set a separator to display between widgets in the group.
*
* @param string $separator
*
* @return $this
*/
public function setSeparator($separator)
{
$this->separator = $separator;
return $this;
}
/**
* Setter for $this->wrapCallback.
*
* @param callable $callable
*
* @return $this
*/
public function wrap(callable $callable)
{
$this->wrapCallback = $callable;
return $this;
}
/**
* Check if there are any widgets in the group.
*
* @return bool
*/
public function any()
{
return !$this->isEmpty();
}
/**
* Check if there are no widgets in the group.
*
* @return bool
*/
public function isEmpty()
{
return empty($this->widgets);
}
/**
* Count the number of widgets in this group.
*
* @return int
*/
public function count()
{
$count = 0;
foreach ($this->widgets as $position => $widgets) {
$count += count($widgets);
}
return $count;
}
/**
* Add a widget with a given type to the array.
*
* @param string $type
* @param array $arguments
*
* @return int
*/
protected function addWidgetWithType($type, array $arguments = [])
{
if (!isset($this->widgets[$this->position])) {
$this->widgets[$this->position] = [];
}
$id = $this->nextWidgetId;
$this->widgets[$this->position][] = [
'id' => $id,
'arguments' => $arguments,
'type' => $type,
];
$this->resetPosition();
$this->nextWidgetId++;
return $id;
}
/**
* Display a widget according to its type.
*
* @param $widget
*
* @return mixed
*/
protected function displayWidget($widget)
{
$factory = $this->app->make($widget['type'] === 'sync' ? 'arrilot.widget' : 'arrilot.async-widget');
return call_user_func_array([$factory, 'run'], $widget['arguments']);
}
/**
* Reset the position property back to the default.
* So it does not affect the next widget.
*/
protected function resetPosition()
{
$this->position = 100;
}
/**
* Wraps widget content in a special markup defined by $this->wrap().
*
* @param string $content
* @param int $index
* @param int $total
*
* @return string
*/
protected function performWrap($content, $index, $total)
{
if (is_null($this->wrapCallback)) {
return $content;
}
$callback = $this->wrapCallback;
return $callback($content, $index, $total);
}
}