<?php
namespace PHPFUI;
class BreadCrumbs extends \PHPFUI\HTML5Element implements \Countable
{
protected array $links = [];
private bool $started = false;
public function __construct()
{
parent::__construct('nav');
$this->addAttribute('aria-label', 'You are here:');
}
public function addCrumb(string $text, string $link = '') : static
{
$this->links[$text . '|' . \count($this)] = $link;
return $this;
}
public function count() : int
{
return \count($this->links);
}
protected function getStart() : string
{
if (! $this->started)
{
$this->started = true;
$ul = new \PHPFUI\UnorderedList();
$ul->addClass('breadcrumbs');
$count = \count($this->links);
$i = 1;
foreach ($this->links as $text => $link)
{
[$text, $junk] = \explode('|', $text);
if ($count == $i)
{
$item = new \PHPFUI\ListItem("<span class='show-for-sr'>Current: </span>{$text}");
}
elseif ($link)
{
$a = new \PHPFUI\HTML5Element('a');
$a->add($text);
$a->setAttribute('href', $link);
$item = new \PHPFUI\ListItem($a);
}
else
{
$item = new \PHPFUI\ListItem($text);
$item->addClass('disabled');
}
$ul->add($item);
++$i;
}
$this->add($ul);
}
return parent::getStart();
}
}