generated from yiisoft/package-template
/
Breadcrumbs.php
275 lines (240 loc) · 7.48 KB
/
Breadcrumbs.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
<?php
declare(strict_types=1);
namespace Yiisoft\Yii\Bulma;
use InvalidArgumentException;
use JsonException;
use Yiisoft\Arrays\ArrayHelper;
use Yiisoft\Html\Html;
use function array_key_exists;
use function array_merge;
use function implode;
use function is_array;
use function strtr;
/**
* The Bulma breadcrumb is a simple navigation component.
*
* ```php
* echo Breadcrumbs::widget()->items([
* ['label' => 'Info'],
* ['label' => 'Contacts'],
* ]);
* ```
*
* @link https://bulma.io/documentation/components/breadcrumb/
*/
final class Breadcrumbs extends Widget
{
private bool $encodeLabels = true;
private ?array $homeItem = ['label' => 'Home', 'url' => '/'];
private string $itemTemplate = "<li>{icon}{link}</li>\n";
private string $activeItemTemplate = "<li class=\"is-active\"><a aria-current=\"page\">{icon}{label}</a></li>\n";
private array $items = [];
private array $options = [];
private array $itemsOptions = [];
protected function run(): string
{
if (empty($this->items)) {
return '';
}
$this->buildOptions();
return
Html::openTag('nav', $this->options) . "\n" .
Html::openTag('ul', $this->itemsOptions) . "\n" .
implode('', $this->renderItems()) .
Html::closeTag('ul') . "\n" .
Html::closeTag('nav');
}
/**
* Disables encoding for labels.
*
* @return self
*/
public function withoutEncodeLabels(): self
{
$new = clone $this;
$new->encodeLabels = false;
return $new;
}
/**
* Returns a new instance with the specified first item in the breadcrumbs (called home link).
*
* If a null is specified, the home item will not be rendered.
*
* @param array|null $value Please refer to {@see items()} on the format.
*
* @throws InvalidArgumentException If an empty array is specified.
*
* @return self
*/
public function homeItem(?array $value): self
{
if ($value === []) {
throw new InvalidArgumentException(
'The home item cannot be an empty array. To disable rendering of the home item, specify null.',
);
}
$new = clone $this;
$new->homeItem = $value;
return $new;
}
/**
* The template used to render each inactive item in the breadcrumbs. The token `{link}` will be replaced with the
* actual HTML link for each inactive item.
*
* @param string $value
*
* @return self
*/
public function itemTemplate(string $value): self
{
$new = clone $this;
$new->itemTemplate = $value;
return $new;
}
/**
* The template used to render each active item in the breadcrumbs. The token `{link}` will be replaced with the
* actual HTML link for each active item.
*
* @param string $value
*
* @return self
*/
public function activeItemTemplate(string $value): self
{
$new = clone $this;
$new->activeItemTemplate = $value;
return $new;
}
/**
* List of items to appear in the breadcrumb. If this property is empty, the widget will not render anything. Each
* array element represents a single link in the breadcrumb with the following structure:
*
* ```php
* [
* 'label' => 'label of the link', // required
* 'url' => 'url of the link', // optional, will be processed by Url::to()
* 'template' => 'own template of the item', // optional, if not set $this->itemTemplate will be used
* ]
* ```
*
* @param array $value
*
* @return self
*/
public function items(array $value): self
{
$new = clone $this;
$new->items = $value;
return $new;
}
/**
* The HTML attributes for the widget container nav tag.
*
* @param array $value
*
* @return self
*
* {@see Html::renderTagAttributes()} for details on how attributes are being rendered.
*/
public function options(array $value): self
{
$new = clone $this;
$new->options = $value;
return $new;
}
/**
* The HTML attributes for the items widget.
*
* @param array $value
*
* @return self
*
* {@see Html::renderTagAttributes()} for details on how attributes are being rendered.
*/
public function itemsOptions(array $value): self
{
$new = clone $this;
$new->itemsOptions = $value;
return $new;
}
private function buildOptions(): void
{
if (!isset($this->options['id'])) {
$this->options['id'] = "{$this->getId()}-breadcrumbs";
}
$this->options = $this->addOptions(
array_merge(
$this->options,
['aria-label' => 'breadcrumbs']
),
'breadcrumb'
);
}
private function renderIcon(string $icon, array $iconOptions): string
{
$html = '';
if ($icon !== '') {
$html = Html::openTag('span', $iconOptions) .
Html::tag('i', '', ['class' => $icon]) .
Html::closeTag('span');
}
return $html;
}
/**
* Renders a single breadcrumb item.
*
* @param array $item The item to be rendered. It must contain the "label" element. The "url" element is optional.
* @param string $template The template to be used to rendered the link. The token "{link}" will be replaced by the
* link.
*
* @throws InvalidArgumentException|JsonException If `$item` does not have "label" element.
*
* @return string The rendering result.
*/
private function renderItem(array $item, string $template): string
{
$encodeLabel = ArrayHelper::remove($item, 'encode', $this->encodeLabels);
$icon = '';
$iconOptions = [];
if (isset($item['icon'])) {
$icon = $item['icon'];
}
if (isset($item['iconOptions']) && is_array($item['iconOptions'])) {
$iconOptions = $this->addOptions($iconOptions, 'icon');
}
unset($item['icon'], $item['iconOptions']);
if (array_key_exists('label', $item)) {
$label = $encodeLabel ? Html::encode($item['label']) : $item['label'];
} else {
throw new InvalidArgumentException('The "label" element is required for each link.');
}
if (isset($item['template'])) {
$template = $item['template'];
}
if (isset($item['url'])) {
$options = $item;
unset($options['template'], $options['label'], $options['url'], $options['icon']);
$link = Html::a($label, $item['url'], $options)->encode(false)->render();
} else {
$link = $label;
}
return strtr(
$template,
['{label}' => $label, '{link}' => $link, '{icon}' => $this->renderIcon($icon, $iconOptions)],
);
}
private function renderItems(): array
{
$items = [];
if ($this->homeItem !== null) {
$items[] = $this->renderItem($this->homeItem, $this->itemTemplate);
}
foreach ($this->items as $item) {
if (!is_array($item)) {
$item = ['label' => $item];
}
$items[] = $this->renderItem($item, isset($item['url']) ? $this->itemTemplate : $this->activeItemTemplate);
}
return $items;
}
}