@@ -17,165 +17,165 @@ |
||
| 17 | 17 | |
| 18 | 18 | class MenuItem |
| 19 | 19 | { |
| 20 | - private Request $request; |
|
| 21 | - private string $title; |
|
| 22 | - private string $menuItemLinkType; |
|
| 23 | - private string $link; |
|
| 24 | - private string $baseLink; |
|
| 25 | - private ?Icon $icon; |
|
| 26 | - private Collection $children; |
|
| 27 | - private UuidInterface $hash; |
|
| 28 | - private string $abbr; |
|
| 29 | - private bool $isChild; |
|
| 30 | - private array $classes = [ |
|
| 31 | - 'nav-item', |
|
| 32 | - ]; |
|
| 33 | - |
|
| 34 | - public function __construct( |
|
| 35 | - string $title, |
|
| 36 | - string $menuItemLinkType, |
|
| 37 | - string $link, |
|
| 38 | - ?Icon $icon = null, |
|
| 39 | - array $children = [], |
|
| 40 | - bool $isChild = false |
|
| 41 | - ) |
|
| 42 | - { |
|
| 43 | - $this->title = $title; |
|
| 44 | - $this->menuItemLinkType = $menuItemLinkType; |
|
| 45 | - $this->link = $this->baseLink = $link; |
|
| 46 | - $this->icon = $icon; |
|
| 47 | - $this->children = collect($children); |
|
| 48 | - $this->isChild = $isChild; |
|
| 49 | - $this->hash = Uuid::fromString(md5($this->title)); |
|
| 50 | - $this->request = resolve(Request::class); |
|
| 51 | - |
|
| 52 | - $this->setAbbr(); |
|
| 53 | - $this->setActive(); |
|
| 54 | - } |
|
| 55 | - |
|
| 56 | - private function setAbbr(): void |
|
| 57 | - { |
|
| 58 | - $words = explode(' ', $this->getTitle()); |
|
| 59 | - |
|
| 60 | - $abbrArray = array_map( |
|
| 61 | - static function ($word) { |
|
| 62 | - $array = mb_str_split($word); |
|
| 63 | - |
|
| 64 | - return current($array); |
|
| 65 | - }, |
|
| 66 | - $words |
|
| 67 | - ); |
|
| 68 | - |
|
| 69 | - $abbrString = mb_strtoupper(implode('', $abbrArray)); |
|
| 70 | - |
|
| 71 | - $this->abbr = preg_replace( |
|
| 72 | - '/([^0-9A-Za-zА-я])/u', |
|
| 73 | - '', |
|
| 74 | - $abbrString |
|
| 75 | - ); |
|
| 76 | - } |
|
| 77 | - |
|
| 78 | - public function getTitle(): string |
|
| 79 | - { |
|
| 80 | - return $this->title; |
|
| 81 | - } |
|
| 82 | - |
|
| 83 | - private function setActive(): void |
|
| 84 | - { |
|
| 85 | - /** @var Route $route */ |
|
| 86 | - $route = $this->request->route(); |
|
| 87 | - |
|
| 88 | - if (!is_null($route)) { |
|
| 89 | - switch ($this->menuItemLinkType) { |
|
| 90 | - case MenuItemLinkType::ROUTE: |
|
| 91 | - if ($this->getBaseLink() === $route->getName()) { |
|
| 92 | - $this->classes[] = 'active'; |
|
| 93 | - } |
|
| 94 | - |
|
| 95 | - break; |
|
| 96 | - case MenuItemLinkType::URI: |
|
| 97 | - $match0 = mb_strpos( |
|
| 98 | - $route->uri(), |
|
| 99 | - $this->getBaseLink() |
|
| 100 | - ) === 0; |
|
| 101 | - |
|
| 102 | - if ($match0) { |
|
| 103 | - $this->classes[] = 'active'; |
|
| 104 | - } |
|
| 105 | - break; |
|
| 106 | - } |
|
| 107 | - } |
|
| 108 | - } |
|
| 109 | - |
|
| 110 | - private function getBaseLink(): string |
|
| 111 | - { |
|
| 112 | - return $this->baseLink; |
|
| 113 | - } |
|
| 114 | - |
|
| 115 | - public function getLink(): string |
|
| 116 | - { |
|
| 117 | - $uri = '#'; |
|
| 118 | - |
|
| 119 | - switch ($this->hasChildren()) { |
|
| 120 | - case true: |
|
| 121 | - $uri = sprintf('#%s', $this->getMenuItemHash()); |
|
| 122 | - break; |
|
| 123 | - case false: |
|
| 124 | - switch ($this->menuItemLinkType) { |
|
| 125 | - case MenuItemLinkType::ROUTE: |
|
| 126 | - try { |
|
| 127 | - $uri = route($this->link); |
|
| 128 | - } catch (Throwable $exception) { |
|
| 129 | - Log::error(sprintf('Menu Build Item issue : %s', $exception->getMessage())); |
|
| 130 | - } |
|
| 131 | - break; |
|
| 132 | - case MenuItemLinkType::URI: |
|
| 133 | - $uri = $this->link; |
|
| 134 | - break; |
|
| 135 | - } |
|
| 136 | - break; |
|
| 137 | - } |
|
| 138 | - |
|
| 139 | - return $uri; |
|
| 140 | - } |
|
| 141 | - |
|
| 142 | - public function hasChildren(): bool |
|
| 143 | - { |
|
| 144 | - return $this->children->isNotEmpty(); |
|
| 145 | - } |
|
| 146 | - |
|
| 147 | - public function getMenuItemHash(): string |
|
| 148 | - { |
|
| 149 | - return $this->hash->toString(); |
|
| 150 | - } |
|
| 151 | - |
|
| 152 | - public function hasIcon(): bool |
|
| 153 | - { |
|
| 154 | - return !is_null($this->getIcon()); |
|
| 155 | - } |
|
| 156 | - |
|
| 157 | - public function getIcon(): ?Icon |
|
| 158 | - { |
|
| 159 | - return $this->icon; |
|
| 160 | - } |
|
| 161 | - |
|
| 162 | - public function isChild(): bool |
|
| 163 | - { |
|
| 164 | - return $this->isChild; |
|
| 165 | - } |
|
| 166 | - |
|
| 167 | - public function getChildren(): Collection |
|
| 168 | - { |
|
| 169 | - return $this->children; |
|
| 170 | - } |
|
| 171 | - |
|
| 172 | - public function getAbbr(): string |
|
| 173 | - { |
|
| 174 | - return $this->abbr; |
|
| 175 | - } |
|
| 176 | - |
|
| 177 | - public function getClasses(): string |
|
| 178 | - { |
|
| 179 | - return implode(' ', $this->classes); |
|
| 180 | - } |
|
| 20 | + private Request $request; |
|
| 21 | + private string $title; |
|
| 22 | + private string $menuItemLinkType; |
|
| 23 | + private string $link; |
|
| 24 | + private string $baseLink; |
|
| 25 | + private ?Icon $icon; |
|
| 26 | + private Collection $children; |
|
| 27 | + private UuidInterface $hash; |
|
| 28 | + private string $abbr; |
|
| 29 | + private bool $isChild; |
|
| 30 | + private array $classes = [ |
|
| 31 | + 'nav-item', |
|
| 32 | + ]; |
|
| 33 | + |
|
| 34 | + public function __construct( |
|
| 35 | + string $title, |
|
| 36 | + string $menuItemLinkType, |
|
| 37 | + string $link, |
|
| 38 | + ?Icon $icon = null, |
|
| 39 | + array $children = [], |
|
| 40 | + bool $isChild = false |
|
| 41 | + ) |
|
| 42 | + { |
|
| 43 | + $this->title = $title; |
|
| 44 | + $this->menuItemLinkType = $menuItemLinkType; |
|
| 45 | + $this->link = $this->baseLink = $link; |
|
| 46 | + $this->icon = $icon; |
|
| 47 | + $this->children = collect($children); |
|
| 48 | + $this->isChild = $isChild; |
|
| 49 | + $this->hash = Uuid::fromString(md5($this->title)); |
|
| 50 | + $this->request = resolve(Request::class); |
|
| 51 | + |
|
| 52 | + $this->setAbbr(); |
|
| 53 | + $this->setActive(); |
|
| 54 | + } |
|
| 55 | + |
|
| 56 | + private function setAbbr(): void |
|
| 57 | + { |
|
| 58 | + $words = explode(' ', $this->getTitle()); |
|
| 59 | + |
|
| 60 | + $abbrArray = array_map( |
|
| 61 | + static function ($word) { |
|
| 62 | + $array = mb_str_split($word); |
|
| 63 | + |
|
| 64 | + return current($array); |
|
| 65 | + }, |
|
| 66 | + $words |
|
| 67 | + ); |
|
| 68 | + |
|
| 69 | + $abbrString = mb_strtoupper(implode('', $abbrArray)); |
|
| 70 | + |
|
| 71 | + $this->abbr = preg_replace( |
|
| 72 | + '/([^0-9A-Za-zА-я])/u', |
|
| 73 | + '', |
|
| 74 | + $abbrString |
|
| 75 | + ); |
|
| 76 | + } |
|
| 77 | + |
|
| 78 | + public function getTitle(): string |
|
| 79 | + { |
|
| 80 | + return $this->title; |
|
| 81 | + } |
|
| 82 | + |
|
| 83 | + private function setActive(): void |
|
| 84 | + { |
|
| 85 | + /** @var Route $route */ |
|
| 86 | + $route = $this->request->route(); |
|
| 87 | + |
|
| 88 | + if (!is_null($route)) { |
|
| 89 | + switch ($this->menuItemLinkType) { |
|
| 90 | + case MenuItemLinkType::ROUTE: |
|
| 91 | + if ($this->getBaseLink() === $route->getName()) { |
|
| 92 | + $this->classes[] = 'active'; |
|
| 93 | + } |
|
| 94 | + |
|
| 95 | + break; |
|
| 96 | + case MenuItemLinkType::URI: |
|
| 97 | + $match0 = mb_strpos( |
|
| 98 | + $route->uri(), |
|
| 99 | + $this->getBaseLink() |
|
| 100 | + ) === 0; |
|
| 101 | + |
|
| 102 | + if ($match0) { |
|
| 103 | + $this->classes[] = 'active'; |
|
| 104 | + } |
|
| 105 | + break; |
|
| 106 | + } |
|
| 107 | + } |
|
| 108 | + } |
|
| 109 | + |
|
| 110 | + private function getBaseLink(): string |
|
| 111 | + { |
|
| 112 | + return $this->baseLink; |
|
| 113 | + } |
|
| 114 | + |
|
| 115 | + public function getLink(): string |
|
| 116 | + { |
|
| 117 | + $uri = '#'; |
|
| 118 | + |
|
| 119 | + switch ($this->hasChildren()) { |
|
| 120 | + case true: |
|
| 121 | + $uri = sprintf('#%s', $this->getMenuItemHash()); |
|
| 122 | + break; |
|
| 123 | + case false: |
|
| 124 | + switch ($this->menuItemLinkType) { |
|
| 125 | + case MenuItemLinkType::ROUTE: |
|
| 126 | + try { |
|
| 127 | + $uri = route($this->link); |
|
| 128 | + } catch (Throwable $exception) { |
|
| 129 | + Log::error(sprintf('Menu Build Item issue : %s', $exception->getMessage())); |
|
| 130 | + } |
|
| 131 | + break; |
|
| 132 | + case MenuItemLinkType::URI: |
|
| 133 | + $uri = $this->link; |
|
| 134 | + break; |
|
| 135 | + } |
|
| 136 | + break; |
|
| 137 | + } |
|
| 138 | + |
|
| 139 | + return $uri; |
|
| 140 | + } |
|
| 141 | + |
|
| 142 | + public function hasChildren(): bool |
|
| 143 | + { |
|
| 144 | + return $this->children->isNotEmpty(); |
|
| 145 | + } |
|
| 146 | + |
|
| 147 | + public function getMenuItemHash(): string |
|
| 148 | + { |
|
| 149 | + return $this->hash->toString(); |
|
| 150 | + } |
|
| 151 | + |
|
| 152 | + public function hasIcon(): bool |
|
| 153 | + { |
|
| 154 | + return !is_null($this->getIcon()); |
|
| 155 | + } |
|
| 156 | + |
|
| 157 | + public function getIcon(): ?Icon |
|
| 158 | + { |
|
| 159 | + return $this->icon; |
|
| 160 | + } |
|
| 161 | + |
|
| 162 | + public function isChild(): bool |
|
| 163 | + { |
|
| 164 | + return $this->isChild; |
|
| 165 | + } |
|
| 166 | + |
|
| 167 | + public function getChildren(): Collection |
|
| 168 | + { |
|
| 169 | + return $this->children; |
|
| 170 | + } |
|
| 171 | + |
|
| 172 | + public function getAbbr(): string |
|
| 173 | + { |
|
| 174 | + return $this->abbr; |
|
| 175 | + } |
|
| 176 | + |
|
| 177 | + public function getClasses(): string |
|
| 178 | + { |
|
| 179 | + return implode(' ', $this->classes); |
|
| 180 | + } |
|
| 181 | 181 | } |