KyleMassacre /
laravel-menus
| 1 | <?php |
||
| 2 | |||
| 3 | namespace KyleMassacre\Menus; |
||
| 4 | |||
| 5 | use Illuminate\Support\Arr; |
||
| 6 | use Illuminate\Support\Facades\Request; |
||
| 7 | use KyleMassacre\Menus\Contracts\MenuItemContract; |
||
| 8 | use KyleMassacre\Menus\Traits\CanHide; |
||
| 9 | use Spatie\Html\Facades\Html as HTML; |
||
| 10 | |||
| 11 | /** |
||
| 12 | * @property string url |
||
| 13 | * @property string route |
||
| 14 | * @property string title |
||
| 15 | * @property string name |
||
| 16 | * @property string icon |
||
| 17 | * @property int parent |
||
| 18 | * @property array attributes |
||
| 19 | * @property bool active |
||
| 20 | * @property int order |
||
| 21 | * @property array badge |
||
| 22 | */ |
||
| 23 | class MenuItem extends MenuItemContract |
||
| 24 | { |
||
| 25 | use CanHide; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Constructor. |
||
| 29 | * |
||
| 30 | * @param array $properties |
||
| 31 | */ |
||
| 32 | 26 | public function __construct(array $properties = array()) |
|
| 33 | { |
||
| 34 | 26 | $this->properties = $properties; |
|
| 35 | 26 | $this->fill($properties); |
|
| 36 | } |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Fill the attributes. |
||
| 40 | * |
||
| 41 | * @param array $attributes |
||
| 42 | */ |
||
| 43 | 26 | public function fill(array $attributes): void |
|
| 44 | { |
||
| 45 | 26 | foreach ($attributes as $key => $value) { |
|
| 46 | 25 | if (in_array($key, $this->fillable)) { |
|
| 47 | 25 | $this->{$key} = $value; |
|
| 48 | } |
||
| 49 | } |
||
| 50 | } |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Create new menu child item using array. |
||
| 54 | * |
||
| 55 | * @param array $attributes |
||
| 56 | * |
||
| 57 | * @return self |
||
| 58 | */ |
||
| 59 | 2 | public function child(array $attributes): self |
|
| 60 | { |
||
| 61 | 2 | $this->childs[] = static::make($attributes); |
|
| 62 | |||
| 63 | 2 | return $this; |
|
| 64 | } |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Register new child menu with dropdown. |
||
| 68 | * |
||
| 69 | * @param $title |
||
| 70 | * @param \Closure $callback |
||
| 71 | * @param int $order |
||
| 72 | * @param array $attributes |
||
| 73 | * @return $this |
||
| 74 | */ |
||
| 75 | 9 | public function dropdown(string $title, \Closure $callback, int $order = 0, array $attributes = array()): static |
|
| 76 | { |
||
| 77 | 9 | $properties = compact('title', 'order', 'attributes'); |
|
| 78 | |||
| 79 | 9 | if (func_num_args() === 3) { |
|
| 80 | $arguments = func_get_args(); |
||
| 81 | |||
| 82 | $title = Arr::get($arguments, 0); |
||
| 83 | $attributes = Arr::get($arguments, 2); |
||
| 84 | |||
| 85 | $properties = compact('title', 'attributes'); |
||
| 86 | } |
||
| 87 | |||
| 88 | 9 | $child = static::make($properties); |
|
| 89 | |||
| 90 | 9 | call_user_func($callback, $child); |
|
| 91 | |||
| 92 | 9 | $this->childs[] = $child; |
|
| 93 | |||
| 94 | 9 | return $child; |
|
| 95 | } |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Create new menu item and set the action to route. |
||
| 99 | * |
||
| 100 | * @param $route |
||
| 101 | * @param $title |
||
| 102 | * @param array $parameters |
||
| 103 | * @param array $attributes |
||
| 104 | * |
||
| 105 | * @return MenuItemContract |
||
| 106 | */ |
||
| 107 | 3 | public function route($route, $title, array $parameters = array(), $order = 0, array $attributes = array()): static |
|
| 108 | { |
||
| 109 | 3 | if (func_num_args() === 4) { |
|
| 110 | $arguments = func_get_args(); |
||
| 111 | |||
| 112 | return $this->add([ |
||
| 113 | 'route' => [Arr::get($arguments, 0), Arr::get($arguments, 2)], |
||
| 114 | 'title' => Arr::get($arguments, 1), |
||
| 115 | 'attributes' => Arr::get($arguments, 3), |
||
| 116 | ]); |
||
| 117 | } |
||
| 118 | |||
| 119 | 3 | $route = array($route, $parameters); |
|
| 120 | |||
| 121 | 3 | return $this->add(compact('route', 'title', 'order', 'attributes')); |
|
| 122 | } |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Create new menu item and set the action to url. |
||
| 126 | * |
||
| 127 | * @param $url |
||
| 128 | * @param $title |
||
| 129 | * @param array $attributes |
||
| 130 | * |
||
| 131 | * @return static |
||
| 132 | */ |
||
| 133 | 6 | public function url($url, $title, $order = 0, array $attributes = array()): static |
|
| 134 | { |
||
| 135 | 6 | if (func_num_args() === 3) { |
|
| 136 | $arguments = func_get_args(); |
||
| 137 | |||
| 138 | return $this->add([ |
||
| 139 | 'url' => Arr::get($arguments, 0), |
||
| 140 | 'title' => Arr::get($arguments, 1), |
||
| 141 | 'attributes' => Arr::get($arguments, 2), |
||
| 142 | ]); |
||
| 143 | } |
||
| 144 | |||
| 145 | 6 | return $this->add(compact('url', 'title', 'order', 'attributes')); |
|
| 146 | } |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Add new divider. |
||
| 150 | * |
||
| 151 | * @param int $order |
||
| 152 | * |
||
| 153 | * @return self |
||
| 154 | */ |
||
| 155 | 1 | public function addDivider($order = null): static |
|
| 156 | { |
||
| 157 | 1 | $item = static::make(array('name' => 'divider', 'order' => $order)); |
|
| 158 | |||
| 159 | 1 | $this->childs[] = $item; |
|
| 160 | |||
| 161 | 1 | return $item; |
|
| 162 | } |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Alias method instead "addDivider". |
||
| 166 | * |
||
| 167 | * @param int $order |
||
| 168 | * |
||
| 169 | * @return MenuItem |
||
| 170 | */ |
||
| 171 | 1 | public function divider($order = null): static |
|
| 172 | { |
||
| 173 | 1 | return $this->addDivider($order); |
|
| 174 | } |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Add dropdown header. |
||
| 178 | * |
||
| 179 | * @param $title |
||
| 180 | * |
||
| 181 | * @return $this |
||
| 182 | */ |
||
| 183 | 2 | public function addHeader($title): static |
|
| 184 | { |
||
| 185 | 2 | $item = static::make(array( |
|
| 186 | 2 | 'name' => 'header', |
|
| 187 | 2 | 'title' => $title, |
|
| 188 | 2 | )); |
|
| 189 | |||
| 190 | 2 | $this->childs[] = $item; |
|
| 191 | |||
| 192 | 2 | return $item; |
|
| 193 | } |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Same with "addHeader" method. |
||
| 197 | * |
||
| 198 | * @param $title |
||
| 199 | * |
||
| 200 | * @return $this |
||
| 201 | */ |
||
| 202 | 2 | public function header($title): static |
|
| 203 | { |
||
| 204 | 2 | return $this->addHeader($title); |
|
| 205 | } |
||
| 206 | |||
| 207 | public function addBadge(string $type, $text): static |
||
| 208 | { |
||
| 209 | $properties = array( |
||
| 210 | 'type' => $type, |
||
| 211 | 'text' => $text, |
||
| 212 | 'name' => 'badge', |
||
| 213 | ); |
||
| 214 | $item = static::make($properties); |
||
| 215 | $this->badge = $properties; |
||
| 216 | |||
| 217 | return $item; |
||
| 218 | } |
||
| 219 | |||
| 220 | /** |
||
| 221 | * @deprecated See `getChildren` |
||
| 222 | * @return array |
||
| 223 | */ |
||
| 224 | 8 | public function getChilds(): array |
|
| 225 | { |
||
| 226 | 8 | return $this->getChildren(); |
|
| 227 | } |
||
| 228 | |||
| 229 | /** |
||
| 230 | * Get childs. |
||
| 231 | * |
||
| 232 | * @return array |
||
| 233 | */ |
||
| 234 | 10 | public function getChildren(): array |
|
| 235 | { |
||
| 236 | 10 | if (config('menus.ordering')) { |
|
| 237 | 1 | return collect($this->childs)->sortBy('order')->all(); |
|
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 238 | } |
||
| 239 | |||
| 240 | 9 | return $this->childs; |
|
| 241 | } |
||
| 242 | |||
| 243 | /** |
||
| 244 | * Get url. |
||
| 245 | * |
||
| 246 | * @return string |
||
| 247 | */ |
||
| 248 | 3 | public function getUrl(): string |
|
| 249 | { |
||
| 250 | 3 | if ($this->route !== null) { |
|
| 251 | 1 | return route($this->route[0], $this->route[1]); |
|
| 252 | } |
||
| 253 | |||
| 254 | 2 | if (empty($this->url)) { |
|
| 255 | return url('/#'); |
||
| 256 | } |
||
| 257 | |||
| 258 | 2 | return url($this->url); |
|
| 259 | } |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Get request url. |
||
| 263 | * |
||
| 264 | * @return string |
||
| 265 | */ |
||
| 266 | 1 | public function getRequest(): string |
|
| 267 | { |
||
| 268 | 1 | return ltrim(str_replace(url('/'), '', $this->getUrl()), '/'); |
|
| 269 | } |
||
| 270 | |||
| 271 | /** |
||
| 272 | * @return string |
||
| 273 | */ |
||
| 274 | public function getBadge(): string |
||
| 275 | { |
||
| 276 | if ($this->hasBadge()) { |
||
| 277 | extract($this->badge); |
||
| 278 | |||
| 279 | return '<span class="' . $type . '">' . $text . '</span>'; |
||
| 280 | } |
||
| 281 | return ''; |
||
| 282 | } |
||
| 283 | |||
| 284 | /** |
||
| 285 | * Get icon. |
||
| 286 | * |
||
| 287 | * @param string|null $default |
||
| 288 | * |
||
| 289 | * @return string|null |
||
| 290 | */ |
||
| 291 | 3 | public function getIcon(string $default = null): ?string |
|
| 292 | { |
||
| 293 | 3 | if ($this->icon !== null && $this->icon !== '') { |
|
| 294 | 1 | return '<i class="' . $this->icon . '"></i>'; |
|
| 295 | } |
||
| 296 | 2 | if ($default === null) { |
|
| 297 | 1 | return $default; |
|
| 298 | } |
||
| 299 | |||
| 300 | 1 | return '<i class="' . $default . '"></i>'; |
|
| 301 | } |
||
| 302 | |||
| 303 | /** |
||
| 304 | * Get properties. |
||
| 305 | * |
||
| 306 | * @return array |
||
| 307 | */ |
||
| 308 | 4 | public function getProperties(): array |
|
| 309 | { |
||
| 310 | 4 | return $this->properties; |
|
| 311 | } |
||
| 312 | |||
| 313 | /** |
||
| 314 | * Get HTML attribute data. |
||
| 315 | * |
||
| 316 | * @return string |
||
| 317 | */ |
||
| 318 | 1 | public function getAttributes(): string |
|
| 319 | { |
||
| 320 | 1 | $attributes = $this->attributes ?: []; |
|
| 321 | |||
| 322 | 1 | Arr::forget($attributes, ['active', 'icon']); |
|
| 323 | |||
| 324 | 1 | $attributeString = collect($attributes) |
|
| 325 | 1 | ->map(function ($value, $key) { |
|
| 326 | 1 | if (is_bool($value)) { |
|
| 327 | return $value ? $key : ''; |
||
| 328 | } |
||
| 329 | |||
| 330 | 1 | return $key . '="' . e($value) . '"'; |
|
| 331 | 1 | }) |
|
| 332 | 1 | ->filter() |
|
| 333 | 1 | ->implode(' '); |
|
| 334 | |||
| 335 | 1 | return $attributeString ? ' ' . $attributeString : ''; |
|
| 336 | } |
||
| 337 | |||
| 338 | /** |
||
| 339 | * Check is the current item divider. |
||
| 340 | * |
||
| 341 | * @return bool |
||
| 342 | */ |
||
| 343 | 1 | public function isDivider(): bool |
|
| 344 | { |
||
| 345 | 1 | return $this->is('divider'); |
|
| 346 | } |
||
| 347 | |||
| 348 | /** |
||
| 349 | * Check is the current item divider. |
||
| 350 | * |
||
| 351 | * @return bool |
||
| 352 | */ |
||
| 353 | 1 | public function isHeader(): bool |
|
| 354 | { |
||
| 355 | 1 | return $this->is('header'); |
|
| 356 | } |
||
| 357 | |||
| 358 | /** |
||
| 359 | * Check is the current item divider. |
||
| 360 | * |
||
| 361 | * @param $name |
||
| 362 | * |
||
| 363 | * @return bool |
||
| 364 | */ |
||
| 365 | 2 | public function is($name): bool |
|
| 366 | { |
||
| 367 | 2 | return $this->name == $name; |
|
| 368 | } |
||
| 369 | |||
| 370 | /** |
||
| 371 | * Check is the current item has sub menu . |
||
| 372 | * |
||
| 373 | * @return bool |
||
| 374 | */ |
||
| 375 | 1 | public function hasSubMenu(): bool |
|
| 376 | { |
||
| 377 | 1 | return !empty($this->childs); |
|
| 378 | } |
||
| 379 | |||
| 380 | /** |
||
| 381 | * @deprecated Same with hasSubMenu. |
||
| 382 | * |
||
| 383 | * @return bool |
||
| 384 | */ |
||
| 385 | 1 | public function hasChilds(): bool |
|
| 386 | { |
||
| 387 | 1 | return $this->hasSubMenu(); |
|
| 388 | } |
||
| 389 | |||
| 390 | /** |
||
| 391 | * Check the active state for current menu. |
||
| 392 | * |
||
| 393 | * @return mixed |
||
| 394 | */ |
||
| 395 | public function hasActiveOnChild(): mixed |
||
| 396 | { |
||
| 397 | if ($this->inactive()) { |
||
| 398 | return false; |
||
| 399 | } |
||
| 400 | |||
| 401 | return $this->hasSubMenu() && $this->getActiveStateFromChildren(); |
||
| 402 | } |
||
| 403 | |||
| 404 | public function getActiveStateFromChildren(): bool |
||
| 405 | { |
||
| 406 | foreach ($this->getChildren() as $child) { |
||
| 407 | if ($child->inactive()) { |
||
| 408 | continue; |
||
| 409 | } |
||
| 410 | if ($child->hasChilds()) { |
||
| 411 | if ($child->getActiveStateFromChilds()) { |
||
| 412 | return true; |
||
| 413 | } |
||
| 414 | } elseif ($child->isActive()) { |
||
| 415 | return true; |
||
| 416 | } elseif ($child->hasRoute() && $child->getActiveStateFromRoute()) { |
||
| 417 | return true; |
||
| 418 | } elseif ($child->getActiveStateFromUrl()) { |
||
| 419 | return true; |
||
| 420 | } |
||
| 421 | } |
||
| 422 | |||
| 423 | return false; |
||
| 424 | } |
||
| 425 | |||
| 426 | /** |
||
| 427 | * @deprecated See `getActiveStateFromChildren()` |
||
| 428 | * Get active state from child menu items. |
||
| 429 | * |
||
| 430 | * @return bool |
||
| 431 | */ |
||
| 432 | public function getActiveStateFromChilds(): bool |
||
| 433 | { |
||
| 434 | return $this->getActiveStateFromChildren(); |
||
| 435 | } |
||
| 436 | |||
| 437 | /** |
||
| 438 | * Get inactive state. |
||
| 439 | * |
||
| 440 | * @return bool |
||
| 441 | */ |
||
| 442 | public function inactive(): bool |
||
| 443 | { |
||
| 444 | $inactive = $this->getInactiveAttribute(); |
||
| 445 | |||
| 446 | if ($inactive === '1' || $inactive === '0') { |
||
| 447 | return (bool)$inactive; |
||
| 448 | } |
||
| 449 | if (is_bool($inactive)) { |
||
|
0 ignored issues
–
show
|
|||
| 450 | return $inactive; |
||
| 451 | } |
||
| 452 | |||
| 453 | if ($inactive instanceof \Closure) { |
||
|
0 ignored issues
–
show
|
|||
| 454 | return call_user_func($inactive); |
||
| 455 | } |
||
| 456 | |||
| 457 | return false; |
||
| 458 | } |
||
| 459 | |||
| 460 | /** |
||
| 461 | * Get active attribute. |
||
| 462 | * |
||
| 463 | * @return string |
||
| 464 | */ |
||
| 465 | public function getActiveAttribute(): string |
||
| 466 | { |
||
| 467 | return Arr::get($this->attributes, 'active', ''); |
||
| 468 | } |
||
| 469 | |||
| 470 | /** |
||
| 471 | * Get inactive attribute. |
||
| 472 | * |
||
| 473 | * @return string |
||
| 474 | */ |
||
| 475 | public function getInactiveAttribute(): string |
||
| 476 | { |
||
| 477 | return Arr::get($this->attributes, 'inactive', ''); |
||
| 478 | } |
||
| 479 | |||
| 480 | /** |
||
| 481 | * Get active state for current item. |
||
| 482 | * |
||
| 483 | * @return mixed |
||
| 484 | */ |
||
| 485 | public function isActive(): mixed |
||
| 486 | { |
||
| 487 | if ($this->inactive()) { |
||
| 488 | return false; |
||
| 489 | } |
||
| 490 | |||
| 491 | $active = $this->getActiveAttribute(); |
||
| 492 | |||
| 493 | if ($active === '1' || $active === '0') { |
||
| 494 | return (bool)$active; |
||
| 495 | } |
||
| 496 | if (is_bool($active)) { |
||
|
0 ignored issues
–
show
|
|||
| 497 | return $active; |
||
| 498 | } |
||
| 499 | |||
| 500 | if ($active instanceof \Closure) { |
||
|
0 ignored issues
–
show
|
|||
| 501 | return call_user_func($active); |
||
| 502 | } |
||
| 503 | |||
| 504 | if ($this->hasRoute()) { |
||
| 505 | return $this->getActiveStateFromRoute(); |
||
| 506 | } |
||
| 507 | |||
| 508 | return $this->getActiveStateFromUrl(); |
||
| 509 | } |
||
| 510 | |||
| 511 | /** |
||
| 512 | * Determine the current item using route. |
||
| 513 | * |
||
| 514 | * @return bool |
||
| 515 | */ |
||
| 516 | protected function hasRoute(): bool |
||
| 517 | { |
||
| 518 | return !empty($this->route); |
||
| 519 | } |
||
| 520 | |||
| 521 | /** |
||
| 522 | * Get active status using route. |
||
| 523 | * |
||
| 524 | * @return bool |
||
| 525 | */ |
||
| 526 | protected function getActiveStateFromRoute(): bool |
||
| 527 | { |
||
| 528 | return Request::is(str_replace(url('/') . '/', '', $this->getUrl())); |
||
| 529 | } |
||
| 530 | |||
| 531 | /** |
||
| 532 | * Get active status using request url. |
||
| 533 | * |
||
| 534 | * @return bool |
||
| 535 | */ |
||
| 536 | protected function getActiveStateFromUrl(): bool |
||
| 537 | { |
||
| 538 | return Request::is($this->url); |
||
| 539 | } |
||
| 540 | |||
| 541 | /** |
||
| 542 | * Set order value. |
||
| 543 | * |
||
| 544 | * @param int $order |
||
| 545 | * @return self |
||
| 546 | */ |
||
| 547 | public function order(int $order): self |
||
| 548 | { |
||
| 549 | $this->order = $order; |
||
| 550 | |||
| 551 | return $this; |
||
| 552 | } |
||
| 553 | |||
| 554 | public function hasBadge(): bool |
||
| 555 | { |
||
| 556 | return !empty($this->badge); |
||
| 557 | } |
||
| 558 | } |
||
| 559 |