88trait HasItems
99{
1010 /**
11- * The items collection.
11+ * The items collection
1212 *
1313 * @var Collection
1414 */
1515 protected $ items ;
1616
1717 /**
18- * Magic method to manipulate items Collection with ease.
18+ * Add new item
1919 *
20- * @param string $method_name
21- * @param array $args
20+ * @param array $attributes
21+ *
22+ * @return Item
2223 */
23- public function __call ( $ method_name , $ args )
24+ public function add ( array $ attributes = []): Item
2425 {
25- if (!method_exists ($ this , $ method_name )) {
26- return call_user_func_array ([$ this ->items , $ method_name ], $ args );
26+ $ item = new Item ($ attributes , $ this );
27+
28+ if (!array_key_exists ('order ' , $ attributes )) {
29+ $ item ->order = count ($ this ->items );
2730 }
28- }
2931
30- /**
31- * Add new item.
32- */
33- public function add (array $ attributes = []): Item
34- {
35- $ this ->items ->push ($ item = new Item ($ attributes , $ this ));
32+ $ this ->items ->push ($ item );
3633
3734 return $ item ;
3835 }
3936
4037 /**
41- * Add new divider menu item.
38+ * Add new divider menu item
39+ *
40+ * @param array $attributes
41+ *
42+ * @return Item
4243 */
4344 public function divider (array $ attributes = []): Item
4445 {
4546 return $ this ->add (compact ('attributes ' ))->asDivider ();
4647 }
4748
4849 /**
49- * Find item by key and value.
50+ * Find item by key and value
51+ *
52+ * @param string $key
53+ * @param string $value
5054 *
5155 * @return mixed
5256 */
@@ -58,7 +62,10 @@ public function findBy(string $key, string $value): ?Item
5862 }
5963
6064 /**
61- * Find item by given title or add it.
65+ * Find item by given title or add it
66+ *
67+ * @param string $title
68+ * @param array $attributes
6269 *
6370 * @return mixed
6471 */
@@ -72,15 +79,20 @@ public function findByTitleOrAdd(string $title, array $attributes = []): ?Item
7279 }
7380
7481 /**
75- * Add new header menu item.
82+ * Add new header menu item
83+ *
84+ * @param string $title
85+ * @param array $attributes
86+ *
87+ * @return Item
7688 */
7789 public function header (string $ title , array $ attributes = []): Item
7890 {
7991 return $ this ->add (compact ('title ' , 'attributes ' ))->asHeader ();
8092 }
8193
8294 /**
83- * Get items.
95+ * Get items
8496 *
8597 * @return Collection
8698 */
@@ -92,20 +104,45 @@ public function items()
92104 }
93105
94106 /**
95- * Register new menu item using registered route.
107+ * Register new menu item using registered route
96108 *
97109 * @param mixed $route
110+ * @param string $title
111+ * @param array $attributes
112+ *
113+ * @return Item
98114 */
99115 public function route ($ route , string $ title , array $ attributes = []): Item
100116 {
101117 return $ this ->add (compact ('route ' , 'title ' , 'attributes ' ));
102118 }
103119
104120 /**
105- * Register new menu item using url.
121+ * Register new menu item using url
122+ *
123+ * @param string $url
124+ * @param string $title
125+ * @param array $attributes
126+ *
127+ * @return Item
106128 */
107129 public function url (string $ url , string $ title , array $ attributes = []): Item
108130 {
109131 return $ this ->add (compact ('url ' , 'title ' , 'attributes ' ));
110132 }
133+
134+ /**
135+ * Magic method to manipulate items Collection with ease
136+ *
137+ * @param string $method_name
138+ * @param array $args
139+ *
140+ * @return void
141+ */
142+ public function __call ($ method_name , $ args )
143+ {
144+ if (!method_exists ($ this , $ method_name )) {
145+ return call_user_func_array ([$ this ->items , $ method_name ], $ args );
146+ }
147+ }
111148}
0 commit comments