|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace anlutro\Menu; |
| 4 | + |
| 5 | +use Illuminate\Support\Str; |
| 6 | +use Illuminate\Support\Collection as BaseCollection; |
| 7 | + |
| 8 | +class Collection// extends BaseCollection |
| 9 | +{ |
| 10 | + const DIVIDER = 'divider'; |
| 11 | + |
| 12 | + protected $items = []; |
| 13 | + protected $ids; |
| 14 | + |
| 15 | + public function __construct(array $attributes = array()) |
| 16 | + { |
| 17 | + $this->attributes = $this->parseAttributes($attributes); |
| 18 | + } |
| 19 | + |
| 20 | + protected function parseAttributes(array $in) |
| 21 | + { |
| 22 | + $out = $in; |
| 23 | + if (isset($in['id']) && !Str::startsWith('menu-', $in['id'])) { |
| 24 | + $out['id'] = 'menu-' . $in['id']; |
| 25 | + } |
| 26 | + $out['class'] = isset($in['class']) ? explode(' ', $in['class']) : []; |
| 27 | + return $out; |
| 28 | + } |
| 29 | + |
| 30 | + public function renderAttributes() |
| 31 | + { |
| 32 | + $attributes = $this->attributes; |
| 33 | + $attributes['class'] = implode(' ', $this->attributes['class']); |
| 34 | + $strings = []; |
| 35 | + foreach ($attributes as $key => $value) { |
| 36 | + if (!empty($value)) $strings[] = "$key=\"$value\""; |
| 37 | + } |
| 38 | + return implode(' ', $strings); |
| 39 | + } |
| 40 | + |
| 41 | + public function addItemInstance(ItemInterface $item, $priority = null) |
| 42 | + { |
| 43 | + $priority = (int) $priority; |
| 44 | + $this->items[$priority][] = $item; |
| 45 | + $this->ids[$item->getId()] = $item; |
| 46 | + return $item; |
| 47 | + } |
| 48 | + |
| 49 | + public function addItem($title, $url, array $attributes = array(), $priority = null) |
| 50 | + { |
| 51 | + return $this->addItemInstance($this->makeItem($title, $url, $attributes), $priority); |
| 52 | + } |
| 53 | + |
| 54 | + public function makeItem($title, $url, array $attributes = array()) |
| 55 | + { |
| 56 | + return new Item($title, $url, $attributes); |
| 57 | + } |
| 58 | + |
| 59 | + public function addSubmenu($title, array $attributes = array(), $priority = null) |
| 60 | + { |
| 61 | + return $this->addItemInstance($this->makeSubmenu($title), $priority); |
| 62 | + } |
| 63 | + |
| 64 | + public function makeSubmenu($title) |
| 65 | + { |
| 66 | + return new SubmenuItem($title); |
| 67 | + } |
| 68 | + |
| 69 | + public function addDivider() |
| 70 | + { |
| 71 | + $this->items[] = 'divider'; |
| 72 | + } |
| 73 | + |
| 74 | + public function getItem($id) |
| 75 | + { |
| 76 | + return array_key_exists($id, $this->ids) ? $this->ids[$id] : null; |
| 77 | + } |
| 78 | + |
| 79 | + public function render() |
| 80 | + { |
| 81 | + $items = ''; |
| 82 | + $sorted = $this->items; |
| 83 | + ksort($sorted); |
| 84 | + foreach (array_flatten($sorted) as $item) { |
| 85 | + if ($item === static::DIVIDER) $items .= '<li class="divider"></li>'; |
| 86 | + else $items .= '<li>'.$item->render().'</li>'; |
| 87 | + } |
| 88 | + return '<ul '.$this->renderAttributes().'>'.$items.'</ul>'; |
| 89 | + } |
| 90 | +} |
0 commit comments