Completed
Push — master ( 994ddf...607686 )
by Michael
02:00
created

Menu::weight()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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