|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
namespace floor12\Breadcrumbs; |
|
5
|
|
|
|
|
6
|
|
|
|
|
7
|
|
|
class Breadcrumbs |
|
8
|
|
|
{ |
|
9
|
|
|
const HTML_OL = '<ol itemscope itemtype="http://schema.org/BreadcrumbList" class="%s" id="%s">%s</ol>'; |
|
10
|
|
|
const HTML_LI = '<li itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem">%s</li>'; |
|
11
|
|
|
const HTML_A = '<a data-pjax="0" itemprop="item" href="%s">%s</a>'; |
|
12
|
|
|
const HTML_SPAN = '<span itemprop="name">%s</span>'; |
|
13
|
|
|
const HTML_META_POSITION = '<meta itemprop="position" content="%d">'; |
|
14
|
|
|
|
|
15
|
|
|
protected $cssClass = 'f12-breadcrumbs'; |
|
16
|
|
|
protected $mainId = 'f12-breadcrumbs'; |
|
17
|
|
|
protected $elements = []; |
|
18
|
|
|
|
|
19
|
8 |
|
public function __construct(array $elements = []) |
|
20
|
|
|
{ |
|
21
|
8 |
|
$this->elements = $elements; |
|
22
|
8 |
|
} |
|
23
|
|
|
|
|
24
|
6 |
|
public function addElement(string $name, string $url = null): self |
|
25
|
|
|
{ |
|
26
|
6 |
|
if ($url) |
|
27
|
2 |
|
$this->elements[$url] = $name; |
|
28
|
|
|
else |
|
29
|
6 |
|
array_push($this->elements, $name); |
|
30
|
6 |
|
return $this; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
6 |
|
public function getHtml(): ?string |
|
34
|
|
|
{ |
|
35
|
6 |
|
if (\count($this->elements) === 0) { |
|
36
|
1 |
|
return null; |
|
37
|
|
|
} |
|
38
|
5 |
|
$renderedElements = []; |
|
39
|
5 |
|
$position = 0; |
|
40
|
5 |
|
foreach ($this->elements as $url => $name) { |
|
41
|
5 |
|
$position++; |
|
42
|
5 |
|
$element = sprintf(self::HTML_SPAN, $name) . sprintf(self::HTML_META_POSITION, $position); |
|
43
|
5 |
|
if (!is_numeric($url)) { |
|
44
|
1 |
|
$element = sprintf(self::HTML_A, $url, $element); |
|
45
|
|
|
} |
|
46
|
5 |
|
$renderedElements[] = sprintf(self::HTML_LI, $element); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
5 |
|
return sprintf( |
|
50
|
5 |
|
self::HTML_OL, |
|
51
|
5 |
|
$this->cssClass, |
|
52
|
5 |
|
$this->mainId, |
|
53
|
5 |
|
implode($renderedElements) |
|
54
|
|
|
); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
1 |
|
public function setCssClass(string $cssClass): self |
|
58
|
|
|
{ |
|
59
|
1 |
|
$this->cssClass = $cssClass; |
|
60
|
1 |
|
return $this; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
1 |
|
public function setMainId(string $mainId): self |
|
64
|
|
|
{ |
|
65
|
1 |
|
$this->mainId = $mainId; |
|
66
|
1 |
|
return $this; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
2 |
|
public function getElements(): array |
|
70
|
|
|
{ |
|
71
|
2 |
|
return $this->elements; |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|