Completed
Push — master ( 607686...1ffe23 )
by Michael
02:03 queued 25s
created

Menu::__toString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Iatstuti\SimpleMenu;
4
5
use Iatstuti\SimpleMenu\Presenters\MenuPresenter;
6
use Iatstuti\SimpleMenu\Presenters\UnorderedListPresenter;
7
use Iatstuti\SimpleMenu\Traits\FetchesWeight;
8
use Iatstuti\SimpleMenu\Traits\ObjectOptions;
9
use Iatstuti\Support\Traits\MethodPropertyAccess;
10
use Illuminate\Support\Collection;
11
12
/**
13
 * POPO object to wrap a menu collection.
14
 *
15
 * @package    Iatstuti\SimpleMenu
16
 * @copyright  2016 IATSTUTI
17
 * @author     Michael Dyrynda <[email protected]>
18
 *
19
 * @property   string $label The menu label
20
 * @property   array $options The menu options
21
 */
22
class Menu
23
{
24
25
    use MethodPropertyAccess, ObjectOptions, FetchesWeight;
26
27
    /**
28
     * Store the menu items.
29
     *
30
     * @var \Illuminate\Support\Collection
31
     */
32
    protected $items;
33
34
    /**
35
     * Store the menu label.
36
     *
37
     * @var null|string
38
     */
39
    protected $label;
40
41
    /**
42
     * @var array
43
     */
44
    private $options;
45
46
47
    /**
48
     * Menu constructor.
49
     *
50
     * @param  string|null $label
51
     * @param  array $options
52
     */
53
    public function __construct($label = null, array $options = [ ])
54
    {
55
        $this->label   = $label;
56
        $this->items   = new Collection();
57
        $this->options = array_merge([ 'weight' => 0, ], $options);
58
    }
59
60
61
    /**
62
     * Return this menu's label.
63
     *
64
     * @return null|string
65
     */
66
    public function label()
67
    {
68
        return $this->label;
69
    }
70
71
72
    /**
73
     * Add a new link to the menu.
74
     *
75
     * @param  string $item
76
     * @param  string $link
77
     * @param  array $options
78
     *
79
     * @return \Iatstuti\SimpleMenu\MenuItem
80
     */
81
    public function link($item, $link, array $options = [ ])
82
    {
83
        $item = new MenuItem($item, $link, $options);
84
85
        $this->items()->push($item);
86
87
        return $item;
88
    }
89
90
91
    /**
92
     * Return this menu's items.
93
     *
94
     * @return \Illuminate\Support\Collection
95
     */
96
    public function items()
97
    {
98
        $this->sortItems();
99
100
        return $this->items;
101
    }
102
103
104
    /**
105
     * Sort the menu items by their weight.
106
     *
107
     * @return void
108
     */
109
    private function sortItems()
110
    {
111
        $this->items = $this->items->sortBy(function ($item) {
112
            return $item->weight;
113
        });
114
    }
115
116
117
    /**
118
     * Add a new sub menu item to the menu.
119
     *
120
     * @param  \Iatstuti\SimpleMenu\Menu $menu
121
     *
122
     * @return \Iatstuti\SimpleMenu\Menu
123
     */
124
    public function subMenu(Menu $menu)
125
    {
126
        $this->items->push($menu);
127
128
        return $menu;
129
    }
130
131
132
    /**
133
     * Render the menu using the given presenter,
134
     * or the default UnorderedListPresenter.
135
     *
136
     * @param  \Iatstuti\SimpleMenu\Presenters\MenuPresenter|null $presenter
137
     *
138
     * @return string
139
     */
140
    public function render(MenuPresenter $presenter = null)
141
    {
142
        $this->sortItems();
143
144
        $presenter = $presenter ?: new UnorderedListPresenter($this);
145
146
        return $presenter->render();
147
    }
148
149
150
    /**
151
     * Overload the __toString method to be able to
152
     * print this object using the render method.
153
     *
154
     * @return string
155
     */
156
    public function __toString()
157
    {
158
        return $this->render();
159
    }
160
}
161