|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Rinvex\Widgets\Models; |
|
6
|
|
|
|
|
7
|
|
|
use Illuminate\Support\Collection; |
|
8
|
|
|
use Illuminate\Support\HtmlString; |
|
9
|
|
|
|
|
10
|
|
|
class WidgetGroup extends Collection |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* The separator to display between widgets in the group. |
|
14
|
|
|
* |
|
15
|
|
|
* @var string |
|
16
|
|
|
*/ |
|
17
|
|
|
protected $separator; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* The callback that defines extra markup |
|
21
|
|
|
* that wraps every widget in the group. |
|
22
|
|
|
* |
|
23
|
|
|
* @var callable |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $wrapCallback; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Render all widgets from the group in correct order. |
|
29
|
|
|
* |
|
30
|
|
|
* @return \Illuminate\Support\HtmlString |
|
31
|
|
|
*/ |
|
32
|
|
|
public function render(): HtmlString |
|
33
|
|
|
{ |
|
34
|
|
|
$output = $this->sortBy('position')->transform(function ($widget, $key) { |
|
35
|
|
|
$content = $this->performWrap($key, $widget); |
|
36
|
|
|
|
|
37
|
|
|
return $this->keys()->last() !== $key ? $this->separator.$content : $content; |
|
38
|
|
|
})->reduce(function ($carry, $widget) { |
|
39
|
|
|
return $carry.$widget; |
|
40
|
|
|
}); |
|
41
|
|
|
|
|
42
|
|
|
return new HtmlString($output); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Add a widget to the group. |
|
47
|
|
|
* |
|
48
|
|
|
* @param \Rinvex\Widgets\Models\AbstractWidget $widget |
|
49
|
|
|
* @param int $position |
|
50
|
|
|
* |
|
51
|
|
|
* @return $this |
|
52
|
|
|
*/ |
|
53
|
|
|
public function addWidget(AbstractWidget $widget, int $position = 100) |
|
54
|
|
|
{ |
|
55
|
|
|
$this->offsetSet($position, $widget); |
|
56
|
|
|
|
|
57
|
|
|
return $this; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Set a separator to display between widgets in the group. |
|
62
|
|
|
* |
|
63
|
|
|
* @param string $separator |
|
64
|
|
|
* |
|
65
|
|
|
* @return $this |
|
66
|
|
|
*/ |
|
67
|
|
|
public function separateWith(string $separator) |
|
68
|
|
|
{ |
|
69
|
|
|
$this->separator = $separator; |
|
70
|
|
|
|
|
71
|
|
|
return $this; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Set the callback that defines extra markup |
|
76
|
|
|
* that wraps every widget in the group. |
|
77
|
|
|
* |
|
78
|
|
|
* @param callable $callable |
|
79
|
|
|
* |
|
80
|
|
|
* @return $this |
|
81
|
|
|
*/ |
|
82
|
|
|
public function wrapCallback(callable $callable) |
|
83
|
|
|
{ |
|
84
|
|
|
$this->wrapCallback = $callable; |
|
85
|
|
|
|
|
86
|
|
|
return $this; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Execute the callback that defines extra markup that |
|
91
|
|
|
* wraps every widget in the group with a special markup. |
|
92
|
|
|
* |
|
93
|
|
|
* @param int $key |
|
94
|
|
|
* @param \Illuminate\Support\HtmlString $widget |
|
95
|
|
|
* |
|
96
|
|
|
* @return string |
|
97
|
|
|
*/ |
|
98
|
|
|
protected function performWrap(int $key, HtmlString $widget): string |
|
99
|
|
|
{ |
|
100
|
|
|
return is_callable($callback = $this->wrapCallback) ? $callback($key, $widget) : $widget; |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|