commit 6318760807f554731b56d1d4432f05dd41c64db3 Author: Andrey Pokidov Date: Thu Mar 26 17:25:18 2020 +0700 +Библиотека хлебных крошек для Laravel diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..41e14da --- /dev/null +++ b/composer.json @@ -0,0 +1,18 @@ +{ + "name": "artmark/laravel-breadcrumbs", + "type": "library", + "license": "Proprietary", + "authors": [ + { + "name": "Andrey Pokidov", + "email": "pokidov@e-traffic.ru" + } + ], + "require": { + }, + "autoload": { + "psr-0": { + "Artmark\\BreadCrumbs\\": "src/" + } + } +} diff --git a/src/Breadcrumbs.php b/src/Breadcrumbs.php new file mode 100644 index 0000000..fd4bd07 --- /dev/null +++ b/src/Breadcrumbs.php @@ -0,0 +1,90 @@ + + */ +class CrumbSequence +{ + /** + * + * @var CrumbSequence + */ + private static $instance = null; + + /** + * + * @var CrumbInterface[] + */ + private $crumbs = []; + + private function __construct() + { + } + + /** + * + * @return CrumbSequence + */ + public static function instance() + { + if (is_null(self::$instance)) { + self::$instance = new CrumbSequence(); + } + + return self::$instance; + } + + /** + * + * @return CrumbInterface[] + */ + public function crumbs() + { + return $this->crumbs; + } + + /** + * + * @return $this + */ + public function reset() + { + if (count($this->crumbs) > 0) { + $this->crumbs = []; + } + + return $this; + } + + /** + * + * @param string $url + * @param string $label + * @param string $icon + * @return $this + */ + public function append(string $url, string $label, string $icon = '') + { + $this->push(new Crumb($url, $label, $icon)); + + return $this; + } + + /** + * + * @param CrumbInterface $crumb + * @return $this + */ + public function push(CrumbInterface $crumb) + { + if (!in_array($crumb, $this->crumbs)) { + $this->crumbs[] = $crumb; + } + + return $this; + } +} diff --git a/src/Crumb.php b/src/Crumb.php new file mode 100644 index 0000000..a27c66d --- /dev/null +++ b/src/Crumb.php @@ -0,0 +1,72 @@ + + */ +class Crumb implements CrumbInterface +{ + /** + * + * @var string + */ + private $url; + + /** + * + * @var string + */ + private $label; + + /** + * + * @var string + */ + private $icon; + + public function __construct(string $url, string $label, string $icon = '') + { + $this->url = trim($url); + $this->label = trim($label); + $this->icon = trim($icon); + } + + /** + * URL ссылки элемента хлебных крошек + * @return string + */ + public function url() + { + return $this->url; + } + + /** + * Текст элемента хлебных крошек + * @return string + */ + public function label() + { + return $this->label; + } + + /** + * Задана или нет иконка для элемента хлебных крошек + * @return boolean + */ + public function hasIcon() + { + return !empty($this->icon); + } + + /** + * Возвращает URL иконки + * @return string + */ + public function icon() + { + return $this->icon; + } +} diff --git a/src/CrumbInterface.php b/src/CrumbInterface.php new file mode 100644 index 0000000..1efdb86 --- /dev/null +++ b/src/CrumbInterface.php @@ -0,0 +1,35 @@ + + */ +interface CrumbInterface +{ + /** + * URL ссылки элемента хлебных крошек + * @return string + */ + public function url(); + + /** + * Текст элемента хлебных крошек + * @return string + */ + public function label(); + + /** + * Задана или нет иконка для элемента хлебных крошек + * @return boolean + */ + public function hasIcon(); + + /** + * Возвращает URL иконки + * @return string + */ + public function icon(); +}