|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * (c) domProjects (https://domprojects.com) |
| 5 | + * |
| 6 | + * For the full copyright and license information, please view |
| 7 | + * the LICENSE file that was distributed with this source code. |
| 8 | + */ |
| 9 | + |
| 10 | +namespace domProjects\CodeIgniterBreadcrumb; |
| 11 | + |
| 12 | +class Breadcrumb |
| 13 | +{ |
| 14 | + public const VERSION = '1.0.0'; |
| 15 | + |
| 16 | + /** |
| 17 | + * @var list<array{label: string, url: string|null}> |
| 18 | + */ |
| 19 | + protected array $items = []; |
| 20 | + |
| 21 | + /** |
| 22 | + * @var array<string, string> |
| 23 | + */ |
| 24 | + protected array $template = []; |
| 25 | + |
| 26 | + protected string $newline = "\n"; |
| 27 | + |
| 28 | + /** |
| 29 | + * @param array<string, string> $template |
| 30 | + */ |
| 31 | + public function __construct(array $template = []) |
| 32 | + { |
| 33 | + helper(['url', 'html']); |
| 34 | + |
| 35 | + $this->template = $template; |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * @param array<string, string> $template |
| 40 | + */ |
| 41 | + public function setTemplate(array $template): self |
| 42 | + { |
| 43 | + $this->template = $template; |
| 44 | + |
| 45 | + return $this; |
| 46 | + } |
| 47 | + |
| 48 | + public function add(string $label, ?string $url = null): self |
| 49 | + { |
| 50 | + $label = trim($label); |
| 51 | + |
| 52 | + if ($label === '') { |
| 53 | + return $this; |
| 54 | + } |
| 55 | + |
| 56 | + $this->items[] = [ |
| 57 | + 'label' => $label, |
| 58 | + 'url' => $url !== null && $url !== '' ? $url : null, |
| 59 | + ]; |
| 60 | + |
| 61 | + return $this; |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * @param array<int|string, mixed> $items |
| 66 | + */ |
| 67 | + public function addMany(array $items): self |
| 68 | + { |
| 69 | + foreach ($items as $key => $value) { |
| 70 | + if (is_array($value) && isset($value['label'])) { |
| 71 | + $this->add( |
| 72 | + (string) $value['label'], |
| 73 | + isset($value['url']) ? (string) $value['url'] : null |
| 74 | + ); |
| 75 | + |
| 76 | + continue; |
| 77 | + } |
| 78 | + |
| 79 | + if (is_string($key) && ! is_array($value)) { |
| 80 | + $this->add($key, (string) $value); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + return $this; |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * @return list<array{label: string, url: string|null}> |
| 89 | + */ |
| 90 | + public function getItems(): array |
| 91 | + { |
| 92 | + return $this->items; |
| 93 | + } |
| 94 | + |
| 95 | + public function clear(): self |
| 96 | + { |
| 97 | + $this->items = []; |
| 98 | + |
| 99 | + return $this; |
| 100 | + } |
| 101 | + |
| 102 | + public function render(): string |
| 103 | + { |
| 104 | + if ($this->items === []) { |
| 105 | + return ''; |
| 106 | + } |
| 107 | + |
| 108 | + $template = $this->resolveTemplate(); |
| 109 | + $output = $template['tag_open'] . $this->newline; |
| 110 | + $last = array_key_last($this->items); |
| 111 | + |
| 112 | + foreach ($this->items as $index => $item) { |
| 113 | + $label = esc($item['label']); |
| 114 | + $url = $item['url']; |
| 115 | + |
| 116 | + if ($index === $last || $url === null) { |
| 117 | + $output .= $template['crumb_active'] |
| 118 | + . $label |
| 119 | + . $template['crumb_close'] |
| 120 | + . $this->newline; |
| 121 | + |
| 122 | + continue; |
| 123 | + } |
| 124 | + |
| 125 | + $output .= $template['crumb_open'] |
| 126 | + . anchor($url, $label) |
| 127 | + . $template['crumb_close'] |
| 128 | + . $this->newline; |
| 129 | + } |
| 130 | + |
| 131 | + $output .= $template['tag_close'] . $this->newline; |
| 132 | + |
| 133 | + return $output; |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + * @return array<string, string> |
| 138 | + */ |
| 139 | + protected function resolveTemplate(): array |
| 140 | + { |
| 141 | + $template = $this->template; |
| 142 | + |
| 143 | + foreach ($this->defaultTemplate() as $key => $value) { |
| 144 | + if (! isset($template[$key])) { |
| 145 | + $template[$key] = $value; |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + return $template; |
| 150 | + } |
| 151 | + |
| 152 | + /** |
| 153 | + * @return array<string, string> |
| 154 | + */ |
| 155 | + protected function defaultTemplate(): array |
| 156 | + { |
| 157 | + return [ |
| 158 | + 'tag_open' => '<ol>', |
| 159 | + 'tag_close' => '</ol>', |
| 160 | + 'crumb_open' => '<li>', |
| 161 | + 'crumb_close' => '</li>', |
| 162 | + 'crumb_active' => '<li class="active" aria-current="page">', |
| 163 | + ]; |
| 164 | + } |
| 165 | +} |
0 commit comments