1 | <?php |
||
2 | |||
3 | namespace ElePHPant\Breadcrumb; |
||
4 | |||
5 | |||
6 | /** |
||
7 | * Class Breadcrumb |
||
8 | * @package ElePHPant\Breadcrumb |
||
9 | * @author Sérgio Danilo Jr. <https://github.com/sergiodanilojr> |
||
10 | */ |
||
11 | class Breadcrumb |
||
12 | { |
||
13 | /** |
||
14 | * @var |
||
15 | */ |
||
16 | private $base; |
||
17 | /** |
||
18 | * @var null|string |
||
19 | */ |
||
20 | private $separator; |
||
21 | /** |
||
22 | * @var array |
||
23 | */ |
||
24 | private $links; |
||
25 | |||
26 | /** |
||
27 | * Breadcrumb constructor. |
||
28 | * @param null|string $separator |
||
29 | */ |
||
30 | public function __construct(?string $separator = null) |
||
31 | { |
||
32 | $this->separator = $separator; |
||
33 | $this->links = []; |
||
34 | } |
||
35 | |||
36 | /** |
||
37 | * @param string $baseUrl |
||
38 | * @param string $title |
||
39 | * @param bool $showTitle |
||
40 | * @param null|string $icon |
||
41 | * @param string|null $class |
||
42 | * @return $this |
||
43 | */ |
||
44 | public function base( |
||
45 | string $baseUrl, |
||
46 | string $title, |
||
47 | bool $showTitle = true, |
||
48 | ?string $icon = null, |
||
49 | string $class = null |
||
50 | ): Breadcrumb |
||
51 | { |
||
52 | $this->base = [ |
||
53 | "url" => $baseUrl, |
||
54 | "title" => $title, |
||
55 | "showTitle" => $showTitle, |
||
56 | "icon" => $icon, |
||
57 | "class" => $class |
||
58 | ]; |
||
59 | return $this; |
||
60 | } |
||
61 | |||
62 | /** |
||
63 | * @param string $url |
||
64 | * @param string $title |
||
65 | * @param null|string $class |
||
66 | * @return Breadcrumb |
||
67 | */ |
||
68 | public function addCrumb(string $title, ?string $url, ?string $class = null): Breadcrumb |
||
69 | { |
||
70 | $this->links[] = $this->parts($title, $url, $class); |
||
71 | return $this; |
||
72 | } |
||
73 | |||
74 | /** |
||
75 | * @return string |
||
76 | */ |
||
77 | public function render(): string |
||
78 | { |
||
79 | $init = "<nav aria-label=\"breadcrumb\"><ol class=\"breadcrumb\">"; |
||
80 | $end = "</ol></nav>"; |
||
81 | |||
82 | return $init . ($this->links ? $this->setBase() : "") . $this->mount($this->links) . $end; |
||
83 | } |
||
84 | |||
85 | public function allCrumbs(): ?array |
||
86 | { |
||
87 | return (!empty($this->links) ? $this->links : null); |
||
88 | } |
||
89 | |||
90 | /** |
||
91 | * @param array $links |
||
92 | * @return string |
||
93 | */ |
||
94 | private function mount(array $links): ?string |
||
95 | { |
||
96 | if (!$links) { |
||
0 ignored issues
–
show
|
|||
97 | return null; |
||
98 | } |
||
99 | $last = count($links) - 1; |
||
100 | $breadcrumb = ""; |
||
101 | |||
102 | for ($b = 0; $b <= $last; $b++) { |
||
103 | |||
104 | if ($b == $last) { |
||
105 | $breadcrumb .= "<li class=\"breadcrumb-item active {$links[$b]["class"]}\" aria-current=\"page\">{$this->separator}{$links[$b]["title"]}</li>" . "\n"; |
||
106 | } else { |
||
107 | $breadcrumb .= "<li class=\"breadcrumb-item {$links[$b]["class"]}\">{$this->separator}<a href=\"{$links[$b]["url"]}\">{$links[$b]["title"]}</a></li>" . "\n"; |
||
108 | } |
||
109 | } |
||
110 | |||
111 | return $breadcrumb; |
||
112 | } |
||
113 | |||
114 | /** |
||
115 | * @return string |
||
116 | */ |
||
117 | private function setBase(): string |
||
118 | { |
||
119 | $title = ($this->base["showTitle"] ? $this->base["title"] : null); |
||
120 | $icon = $this->base["icon"]; |
||
121 | $class = $this->base["class"]; |
||
122 | $url = $this->base["url"]; |
||
123 | |||
124 | if (!$this->links) { |
||
0 ignored issues
–
show
The expression
$this->links of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent. Consider making the comparison explicit by using ![]() |
|||
125 | return "<li class=\"breadcrumb-item active {$class}\" aria-current=\"page\">{$icon}{$title}</li>"; |
||
126 | } |
||
127 | |||
128 | return "<li class=\"breadcrumb-item {$class}\"><a href=\"{$url}\">{$icon}{$title}</a></li>"; |
||
129 | } |
||
130 | |||
131 | /** |
||
132 | * @param string $url |
||
133 | * @param string $title |
||
134 | * @return array |
||
135 | */ |
||
136 | private function parts( |
||
137 | string $title, |
||
138 | string $url = null, |
||
139 | string $class = null |
||
140 | ): array |
||
141 | { |
||
142 | $url = $this->setUrl($url); |
||
143 | |||
144 | return [ |
||
145 | "url" => $url, |
||
146 | "title" => $title, |
||
147 | "class" => $class |
||
148 | ]; |
||
149 | } |
||
150 | |||
151 | /** |
||
152 | * @param string $url |
||
153 | * @return string |
||
154 | */ |
||
155 | private function setUrl(string $url): string |
||
156 | { |
||
157 | $url = str_replace($this->base["url"], "", $url); |
||
158 | $url = ($url[0] == "/" ? $url : "/" . $url); |
||
159 | |||
160 | return $this->base["url"] . $url; |
||
161 | } |
||
162 | } |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.