Lines
100.00%
21 / 21
Methods
100.00%
1 / 1
Classes
100.00%
1 / 1
| Name | Lines | Methods | CRAP | ||||
|---|---|---|---|---|---|---|---|
| render | 100.00% | 21 / 21 | 100.00% | 1 / 1 | 6 | ||
| 27 | final class BreadcrumbsHtmlRenderer | |
| 28 | { | |
| 29 | /** | |
| 30 | * @param array<int, array{id:int, name:string, description:string}> $segments | |
| 31 | */ | |
| 32 | public function render(Configuration $configuration, array $segments, string $useCssClass = 'breadcrumb'): string | |
| 33 | { | |
| 34 | $items = []; | |
| 35 | foreach ($segments as $index => $segment) { | |
| 36 | $url = match ($segment['id']) { | |
| 37 | // Start page → homepage URL | |
| 38 | -1 => $configuration->getDefaultUrl(), | |
| 39 | // All-categories overview → dedicated route, not a category URL | |
| 40 | 0 => $configuration->getDefaultUrl() . 'show-categories.html', | |
| 41 | default => strtr('{base}category/{id}/{slug}.html', [ | |
| 42 | '{base}' => $configuration->getDefaultUrl(), | |
| 43 | '{id}' => (string) $segment['id'], | |
| 44 | '{slug}' => TitleSlugifier::slug(Strings::htmlentities($segment['name'])), | |
| 45 | ]), | |
| 46 | }; | |
| 47 | ||
| 48 | $oLink = new Link($url, $configuration); | |
| 49 | $oLink->text = Strings::htmlentities($segment['name']); | |
| 50 | $oLink->setTitle(Strings::htmlentities($segment['name'])); | |
| 51 | $oLink->tooltip = Strings::htmlentities($segment['description'] ?? ''); | |
| 52 | if (0 === $index) { | |
| 53 | $oLink->setRelation(rel: 'index'); | |
| 54 | } | |
| 55 | ||
| 56 | $items[] = sprintf('<li class="breadcrumb-item">%s</li>', $oLink->toHtmlAnchor()); | |
| 57 | } | |
| 58 | ||
| 59 | return strtr('<ol class="{class}">{items}</ol>', [ | |
| 60 | '{class}' => $useCssClass, | |
| 61 | '{items}' => implode(separator: '', array: $items), | |
| 62 | ]); | |
| 63 | } | |
| 64 | } |