generated from yiisoft/package-template
/
Breadcrumbs.php
345 lines (303 loc) · 9.8 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
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
334
335
336
337
338
339
340
341
342
343
344
345
<?php
declare(strict_types=1);
namespace Yiisoft\Yii\Bulma;
use InvalidArgumentException;
use Yiisoft\Html\Html;
use Yiisoft\Html\Tag\A;
use Yiisoft\Html\Tag\CustomTag;
use Yiisoft\Html\Tag\I;
use Yiisoft\Html\Tag\Span;
use Yiisoft\Widget\Widget;
use function array_key_exists;
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 string $activeItemTemplate = "<li class=\"is-active\"><a aria-current=\"page\">{link}</a></li>\n";
private array $attributes = [];
private string $autoIdPrefix = 'w';
private bool $encode = false;
private ?array $homeItem = ['label' => 'Home', 'url' => '/'];
private array $items = [];
private array $itemsAttributes = [];
private string $itemTemplate = "<li>{link}</li>\n";
/**
* Returns a new instance with the specified active item template.
*
* @param string $value 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.
*
* @return self
*/
public function activeItemTemplate(string $value): self
{
$new = clone $this;
$new->activeItemTemplate = $value;
return $new;
}
/**
* Returns a new instance with the specified `aria-label` attribute for the current element.
*
* @param string $value The value of the aria-label attribute.
*
* @return self
*
* @link https://www.w3.org/TR/wai-aria/#aria-label
*/
public function ariaLabel(string $value): self
{
$new = clone $this;
$new->attributes['aria-label'] = $value;
return $new;
}
/**
* Returns a new instance with the specified HTML attributes for widget.
*
* @param array $values Attribute values indexed by attribute names.
*
* @return self
*
* {@see \Yiisoft\Html\Html::renderTagAttributes()} For details on how attributes are being rendered.
*/
public function attributes(array $values): self
{
$new = clone $this;
$new->attributes = $values;
return $new;
}
/**
* Returns a new instance with the specified prefix to the automatically generated widget IDs.
*
* @param string $value The prefix to the automatically generated widget IDs.
*
* @return self
*/
public function autoIdPrefix(string $value): self
{
$new = clone $this;
$new->autoIdPrefix = $value;
return $new;
}
/**
* Returns a new instance with the specified whether the tags for the breadcrumbs are encoded.
*
* @param bool $value Whether to encode the output.
*
* @return self
*/
public function encode(bool $value): self
{
$new = clone $this;
$new->encode = $value;
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;
}
/**
* Returns a new instance with the specified ID of the widget.
*
* @param string $value The ID of the widget.
*
* @return self
*/
public function id(string $value): self
{
$new = clone $this;
$new->attributes['id'] = $value;
return $new;
}
/**
* Returns a new instance with the specified list of items.
*
* @param array $value List of items to appear in the breadcrumbs. If this property is empty, the widget will not
* render anything. Each array element represents a single item in the breadcrumbs 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
* 'encode' => true/false, // optional, is encoded is `true`, the tags will be encoded
* 'icon' => 'icon css class', // optional, icon css class
* 'iconAttributes' => [], // the html attributes for icon container
* ]
* ```
*
* @return self
*/
public function items(array $value): self
{
$new = clone $this;
$new->items = $value;
return $new;
}
/**
* Returns a new instance with the specified items HTML attributes.
*
* @param array $value The HTML attributes for the item's widget.
*
* @return self
*
* {@see Html::renderTagAttributes()} For details on how attributes are being rendered.
*/
public function itemsAttributes(array $value): self
{
$new = clone $this;
$new->itemsAttributes = $value;
return $new;
}
/**
* Returns a new instance with the specified item template.
*
* @param string $value 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.
*
* @return self
*/
public function itemTemplate(string $value): self
{
$new = clone $this;
$new->itemTemplate = $value;
return $new;
}
public function render(): string
{
if (empty($this->items)) {
return '';
}
$attributes = $this->attributes;
$customTag = CustomTag::name('nav');
Html::addCssClass($attributes, 'breadcrumb');
if (!array_key_exists('aria-label', $attributes)) {
$customTag = $customTag->attribute('aria-label', 'breadcrumbs');
}
if (!array_key_exists('id', $attributes)) {
$customTag = $customTag->id(Html::generateId($this->autoIdPrefix) . '-breadcrumbs');
}
$content = PHP_EOL . Html::openTag('ul', $this->itemsAttributes) . PHP_EOL .
implode('', $this->renderItems()) .
Html::closeTag('ul') . PHP_EOL;
return $customTag
->addAttributes($attributes)
->content($content)
->encode(false)
->render();
}
private function renderIcon(?string $icon, array $iconAttributes): string
{
$html = '';
if ($icon !== null) {
$html = Span::tag()
->attributes($iconAttributes)
->content(I::tag()
->attributes(['class' => $icon, 'aria-hidden' => 'true'])
->render())
->encode($this->encode)
->render();
}
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 render the link. The token "{link}" will be replaced by the
* link.
*
* @throws InvalidArgumentException If `$item` does not have "label" element.
*
* @return string The rendering result.
*/
private function renderItem(array $item, string $template): string
{
if (!array_key_exists('label', $item)) {
throw new InvalidArgumentException('The "label" element is required for each link.');
}
/** @var bool */
$encode = $item['encode'] ?? $this->encode;
unset($item['encode']);
/** @var string|null */
$icon = $item['icon'] ?? null;
unset($item['icon']);
/** @var array */
$iconAttributes = $item['iconAttributes'] ?? [];
unset($item['iconAttributes']);
/** @var string */
$template = $item['template'] ?? $template;
unset($item['template']);
/** @var string|null */
$url = $item['url'] ?? null;
unset($item['url']);
/** @var string */
$label = $item['label'];
unset($item['label']);
$icon = $this->renderIcon($icon, $iconAttributes);
if ($icon !== '') {
$label = $icon . Span::tag()
->content($label)
->render();
}
$link = $url !== null
? A::tag()
->attributes($item)
->content($label)
->url($url)
->encode($encode)
->render() : $label;
return strtr($template, ['{link}' => $link, '{label}' => $label, '{icon}' => $icon]);
}
/**
* @psalm-return string[]
*/
private function renderItems(): array
{
$renderItems = [];
if ($this->homeItem !== null) {
$renderItems[] = $this->renderItem($this->homeItem, $this->itemTemplate);
}
/** @psalm-var string[]|string $item */
foreach ($this->items as $item) {
if (!is_array($item)) {
$item = ['label' => $item];
}
$renderItems[] = $this->renderItem(
$item,
isset($item['url']) ? $this->itemTemplate : $this->activeItemTemplate,
);
}
return $renderItems;
}
}