Skip to content

Commit bd43762

Browse files
Version 2.0
1 parent cff9340 commit bd43762

File tree

11 files changed

+435
-58
lines changed

11 files changed

+435
-58
lines changed

src/DynamicMenu/Collections/ArrayCollection.php

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use DynamicMenu\Collections\Collection;
66
use DynamicMenu\Contracts\CollectionContract;
7+
use DynamicMenu\Supports\DefaultConfig;
78

89
/**
910
* Dynamic Menu
@@ -17,6 +18,13 @@
1718
*/
1819
class ArrayCollection extends Collection implements CollectionContract
1920
{
21+
/**
22+
* Instance of class DefaultConfig.
23+
*
24+
* @var \DynamicMenu\Supports\DefaultConfig
25+
*/
26+
private $config;
27+
2028
/**
2129
* Remaining items to organize
2230
*
@@ -28,10 +36,12 @@ class ArrayCollection extends Collection implements CollectionContract
2836
* New instance of class.
2937
*
3038
* @param array $items
39+
* @param \DynamicMenu\Supports\DefaultConfig $config
3140
* @return void
3241
*/
33-
public function __construct($items = [])
42+
public function __construct($items = [], DefaultConfig $config)
3443
{
44+
$this->config = $config;
3545
$this->setItems($items);
3646
}
3747

@@ -78,11 +88,13 @@ public function generateChildress(array $menus)
7888
public function filterParent(array $items, bool $parents = false)
7989
{
8090
return array_filter($items, function($item) use ($parents){
91+
$parent_key = $this->config->getParentKey();
92+
8193
if ($parents) {
82-
return ! $item['menu_id'];
94+
return ! $item[$parent_key];
8395
}
8496

85-
return ($item['menu_id']);
97+
return ($item[$parent_key]);
8698
});
8799
}
88100

@@ -97,11 +109,14 @@ public function filterParent(array $items, bool $parents = false)
97109
public function filterChildress(array $items, $menu, bool $parents = false)
98110
{
99111
return array_filter($items, function($item) use ($parents, $menu){
112+
$primary_key = $this->config->getPrimaryKey();
113+
$parent_key = $this->config->getParentKey();
114+
100115
if ($parents) {
101-
return ((int) $item['menu_id'] === (int) $menu['id']);
116+
return ((int) $item[$parent_key] === (int) $menu[$primary_key]);
102117
}
103118

104-
return ((int) $item['menu_id'] !== (int) $menu['id']);
119+
return ((int) $item[$parent_key] !== (int) $menu[$primary_key]);
105120
});
106121
}
107122

@@ -113,10 +128,12 @@ public function filterChildress(array $items, $menu, bool $parents = false)
113128
*/
114129
public function sortBy(array $items)
115130
{
131+
$order = $this->config->getOrder();
132+
116133
$results = [];
117134

118135
foreach ($items as $key => $value) {
119-
$results[$key] = $value['order'];
136+
$results[$key] = $value[$order];
120137
}
121138

122139
asort($results, SORT_REGULAR);

src/DynamicMenu/Collections/ObjectCollection.php

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use DynamicMenu\Collections\Collection;
66
use DynamicMenu\Contracts\CollectionContract;
7+
use DynamicMenu\Supports\DefaultConfig;
78

89
/**
910
* Dynamic Menu
@@ -17,6 +18,13 @@
1718
*/
1819
class ObjectCollection extends Collection implements CollectionContract
1920
{
21+
/**
22+
* Instance of class DefaultConfig.
23+
*
24+
* @var \DynamicMenu\Supports\DefaultConfig
25+
*/
26+
private $config;
27+
2028
/**
2129
* Remaining items to organize
2230
*
@@ -28,10 +36,12 @@ class ObjectCollection extends Collection implements CollectionContract
2836
* New instance of class.
2937
*
3038
* @param object $items
39+
* @param \DynamicMenu\Supports\DefaultConfig $config
3140
* @return void
3241
*/
33-
public function __construct($items = [])
42+
public function __construct($items = [], DefaultConfig $config)
3443
{
44+
$this->config = $config;
3545
$this->setItems($items);
3646
}
3747

@@ -78,11 +88,13 @@ public function generateChildress(array $menus)
7888
public function filterParent(array $items, bool $parents = false)
7989
{
8090
return array_filter($items, function($item) use ($parents){
91+
$parent_key = $this->config->getParentKey();
92+
8193
if ($parents) {
82-
return ! $item->menu_id;
94+
return ! $item->$parent_key;
8395
}
8496

85-
return ($item->menu_id);
97+
return ($item->$parent_key);
8698
});
8799
}
88100

@@ -97,11 +109,14 @@ public function filterParent(array $items, bool $parents = false)
97109
public function filterChildress(array $items, $menu, bool $parents = false)
98110
{
99111
return array_filter($items, function($item) use ($parents, $menu){
112+
$primary_key = $this->config->getPrimaryKey();
113+
$parent_key = $this->config->getParentKey();
114+
100115
if ($parents) {
101-
return ((int) $item->menu_id === (int) $menu->id);
116+
return ((int) $item->$parent_key === (int) $menu->$primary_key);
102117
}
103118

104-
return ((int) $item->menu_id !== (int) $menu->id);
119+
return ((int) $item->$parent_key !== (int) $menu->$primary_key);
105120
});
106121
}
107122

@@ -113,10 +128,12 @@ public function filterChildress(array $items, $menu, bool $parents = false)
113128
*/
114129
public function sortBy(array $items)
115130
{
131+
$order = $this->config->getOrder();
132+
116133
$results = [];
117134

118135
foreach ($items as $key => $value) {
119-
$results[$key] = $value->order;
136+
$results[$key] = $value->$order;
120137
}
121138

122139
asort($results, SORT_REGULAR);

src/DynamicMenu/Contracts/CollectionContract.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace DynamicMenu\Contracts;
44

5+
use DynamicMenu\Supports\DefaultConfig;
6+
57
/**
68
* Dynamic Menu
79
*
@@ -18,9 +20,10 @@ interface CollectionContract
1820
* New instance of class.
1921
*
2022
* @param array $items
23+
* @param \DynamicMenu\Supports\DefaultConfig $config
2124
* @return void
2225
*/
23-
public function __construct($items = []);
26+
public function __construct($items = [], DefaultConfig $config);
2427

2528
/**
2629
* Generate parent dynamic menu

src/DynamicMenu/Customizations/Attributes.php

Lines changed: 69 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ trait Attributes
2222
* @var array
2323
*/
2424
private $ul_dropdown = [
25-
'class' => '',
25+
'class' => 'menu',
2626
'attributes' => '',
2727
];
2828

@@ -32,7 +32,7 @@ trait Attributes
3232
* @var array
3333
*/
3434
private $li_dropdown = [
35-
'class' => '',
35+
'class' => 'dropdown',
3636
'attributes' => '',
3737
];
3838

@@ -84,20 +84,78 @@ public function get(string $attribute)
8484
}
8585

8686
/**
87-
* Set style
87+
* Set ul_dropdown
8888
*
89-
* @param string $attribute
90-
* @param array $style
91-
* @return void
89+
* @param array $ul_dropdown
90+
* @return $this
9291
*/
93-
public function setStyle(string $attribute, array $style)
92+
public function setUlDropdown(array $ul_dropdown)
9493
{
95-
$this->handlerProperty($attribute);
94+
foreach ($ul_dropdown as $key => $value) {
95+
$this->handlerKey('ul_dropdown', $key);
96+
}
9697

97-
foreach ($style as $key => $value) {
98-
$this->handlerKey($attribute, $key);
99-
$this->$attribute[$key] = $value;
98+
return $this;
99+
}
100+
101+
/**
102+
* Set li_dropdown
103+
*
104+
* @param array $li_dropdown
105+
* @return $this
106+
*/
107+
public function setLiDropdown(array $li_dropdown)
108+
{
109+
foreach ($li_dropdown as $key => $value) {
110+
$this->handlerKey('li_dropdown', $key);
100111
}
112+
113+
return $this;
114+
}
115+
116+
/**
117+
* Set li
118+
*
119+
* @param array $li
120+
* @return $this
121+
*/
122+
public function setLi(array $li)
123+
{
124+
foreach ($li as $key => $value) {
125+
$this->handlerKey('li', $key);
126+
}
127+
128+
return $this;
129+
}
130+
131+
/**
132+
* Set a_dropdown
133+
*
134+
* @param array $a_dropdown
135+
* @return $this
136+
*/
137+
public function setADropdown(array $a_dropdown)
138+
{
139+
foreach ($a_dropdown as $key => $value) {
140+
$this->handlerKey('a_dropdown', $key);
141+
}
142+
143+
return $this;
144+
}
145+
146+
/**
147+
* Set a
148+
*
149+
* @param array $a
150+
* @return $this
151+
*/
152+
public function setA(array $a)
153+
{
154+
foreach ($a as $key => $value) {
155+
$this->handlerKey('a', $key);
156+
}
157+
158+
return $this;
101159
}
102160

103161
/**

src/DynamicMenu/Customizations/Customization.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,11 @@ public function getInitDropdown()
4141
$a_dropdown = $this->get('a_dropdown');
4242

4343
$generated = '<li class="'.$li_dropdown['class'].'" '.$li_dropdown['attributes'].'>';
44-
$generated .= '<a href="#href#" class="'.$a_dropdown['class'].'" '.$a_dropdown['attributes'].' title="#title#">';
44+
$generated .= '<a href="#href#" class="'.$a_dropdown['class'].'" '.$a_dropdown['attributes'].' title="#title#">#icon-before#';
4545
$generated .= $a_dropdown['before'];
4646
$generated .= '#name#';
4747
$generated .= $a_dropdown['after'];
48-
$generated .= '</a>';
48+
$generated .= '#icon-after#</a>';
4949

5050
return $generated;
5151
}
@@ -71,11 +71,11 @@ public function getDefault()
7171
$a = $this->get('a');
7272

7373
$generated = '<li class="'.$li['class'].'" '.$li['attributes'].'>';
74-
$generated .= '<a href="#href#" class="'.$a['class'].'" '.$a['attributes'].' title="#title#">';
74+
$generated .= '<a href="#href#" class="'.$a['class'].'" '.$a['attributes'].' title="#title#"> #icon-before# ';
7575
$generated .= $a['before'];
7676
$generated .= '#name#';
7777
$generated .= $a['after'];
78-
$generated .= '</a>';
78+
$generated .= ' #icon-after# </a>';
7979
$generated .= '</li>';
8080

8181
return $generated;

0 commit comments

Comments
 (0)