Skip to content

Commit 468b943

Browse files
authored
Merge pull request #4 from nurmanhabib/feature/factory
Added NavCollectionFactory and NavFactory
2 parents 8da663c + d725d2e commit 468b943

File tree

6 files changed

+267
-4
lines changed

6 files changed

+267
-4
lines changed

README.md

Lines changed: 85 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,76 @@ $menu->addLink('Logout', '/logout');
4444

4545
$navigator = new Navigator($menu);
4646

47+
echo $navigator->render();
48+
```
49+
50+
Anda juga bisa membuat menggunakan data Array
51+
52+
```php
53+
$data = [
54+
[
55+
'text' => 'Home',
56+
'url' => '/'
57+
],
58+
[
59+
'text' => 'Berita',
60+
'url' => 'berita',
61+
'match' => '/berita*'
62+
],
63+
[
64+
'type' => 'separator'
65+
],
66+
[
67+
'text' => 'Kategori',
68+
'child' => [
69+
[
70+
'text' => 'Teknologi',
71+
'url' => 'kategori/teknologi'
72+
],
73+
[
74+
'text' => 'Otomotif',
75+
'url' => 'kategori/otomotif'
76+
],
77+
[
78+
'text' => 'Lifestyle',
79+
'child' => [
80+
[
81+
'text' => 'Pria',
82+
'url' => 'lifestyle-pria'
83+
],
84+
[
85+
'text' => 'Wanita',
86+
'url' => 'lifestyle-wanita'
87+
],
88+
]
89+
],
90+
]
91+
],
92+
[
93+
'type' => 'heading',
94+
'text' => 'Configuration'
95+
],
96+
[
97+
'text' => 'Account',
98+
'child' => [
99+
[
100+
'text' => 'Change Password',
101+
'url' => 'change-password'
102+
],
103+
[
104+
'text' => 'Logout',
105+
'url' => 'logout'
106+
],
107+
]
108+
],
109+
];
110+
111+
$factory = new ArrayNavCollectionFactory($data);
112+
113+
$menu = $factory->createNavCollection();
114+
115+
$navigator = new Navigator($menu);
116+
47117
echo $navigator->render();
48118
```
49119

@@ -68,13 +138,24 @@ echo $navigator->render();
68138

69139
Anda bisa menambahkan data pada item menu untuk digunakan pada saat custom render.
70140

71-
3. hasData
141+
3. hasData
72142
```php
73143
$item->hasData('key');
74144
```
75-
76-
4. getData
145+
146+
4. getData
77147
```php
78148
echo $item->getData('key');
79149
```
80-
150+
151+
5. getType
152+
```php
153+
echo $item->getType();
154+
```
155+
156+
Type yang didukung saat ini :
157+
158+
- link
159+
- home
160+
- heading
161+
- separator
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace Nurmanhabib\Navigator\Exceptions;
4+
5+
use Throwable;
6+
7+
class InvalidNavTypeException extends \Exception
8+
{
9+
public function __construct($type)
10+
{
11+
$message = 'Invalid Nav Type [' . $type . ']';
12+
13+
parent::__construct($message);
14+
}
15+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace Nurmanhabib\Navigator\Factories;
4+
5+
use Nurmanhabib\Navigator\NavCollection;
6+
7+
class ArrayNavCollectionFactory implements NavCollectionFactory
8+
{
9+
/**
10+
* @var array
11+
*/
12+
protected $items;
13+
14+
/**
15+
* ArrayNavCollectionFactory constructor.
16+
*
17+
* @param array $items
18+
*/
19+
public function __construct(array $items)
20+
{
21+
$this->items = $items;
22+
}
23+
24+
/**
25+
* @return NavCollection
26+
*/
27+
public function createNavCollection()
28+
{
29+
$items = array_map(function ($item) {
30+
return (new ArrayNavFactory($item))->createNav();
31+
}, $this->items);
32+
33+
return new NavCollection($items);
34+
}
35+
}

src/Factories/ArrayNavFactory.php

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
<?php
2+
3+
namespace Nurmanhabib\Navigator\Factories;
4+
5+
use Nurmanhabib\Navigator\Exceptions\InvalidNavTypeException;
6+
use Nurmanhabib\Navigator\Items\Nav;
7+
use Nurmanhabib\Navigator\Items\NavHeading;
8+
use Nurmanhabib\Navigator\Items\NavHome;
9+
use Nurmanhabib\Navigator\Items\NavLink;
10+
use Nurmanhabib\Navigator\Items\NavParent;
11+
use Nurmanhabib\Navigator\Items\NavSeparator;
12+
13+
class ArrayNavFactory implements NavFactory
14+
{
15+
/**
16+
* @var array
17+
*/
18+
protected $data;
19+
20+
/**
21+
* ArrayNavFactory constructor.
22+
*
23+
* @param array $data
24+
*/
25+
public function __construct(array $data)
26+
{
27+
$this->data = $data;
28+
}
29+
30+
/**
31+
* @return Nav
32+
*
33+
* @throws InvalidNavTypeException
34+
*/
35+
public function createNav()
36+
{
37+
switch ($this->getValue('type', 'link')) {
38+
case 'heading':
39+
$nav = new NavHeading($this->getValue('text'));
40+
break;
41+
42+
case 'separator':
43+
$nav = new NavSeparator;
44+
break;
45+
46+
case 'home':
47+
$nav = new NavHome($this->getValue('text'), $this->getValue('url'), $this->getValue('icon'));
48+
break;
49+
50+
case 'link':
51+
$nav = $this->makeNavLinkOrParent();
52+
break;
53+
54+
default:
55+
throw new InvalidNavTypeException($this->getValue('type'));
56+
}
57+
58+
if ($this->hasKey('data')) {
59+
$nav->setData($this->getValue('data', []));
60+
}
61+
62+
if ($this->hasKey('is_active')) {
63+
$nav->setActive((bool)$this->getValue('is_active'));
64+
}
65+
66+
if ($this->hasKey('is_visible')) {
67+
$nav->setVisible((bool)$this->getValue('is_visible'));
68+
}
69+
70+
if ($this->hasKey('match')) {
71+
$nav->match($this->getValue('match'));
72+
}
73+
74+
return $nav;
75+
}
76+
77+
protected function makeNavLinkOrParent()
78+
{
79+
$children = $this->getValue('child', []);
80+
81+
if (!empty($children)) {
82+
$navCollectionFactory = new ArrayNavCollectionFactory($children);
83+
84+
$nav = new NavParent($this->getValue('text'), $this->getValue('url'), $this->getValue('icon'));
85+
$nav->setChild($navCollectionFactory->createNavCollection());
86+
87+
return $nav;
88+
}
89+
90+
return new NavLink($this->getValue('text'), $this->getValue('url'), $this->getValue('icon'));
91+
}
92+
93+
protected function hasKey($key)
94+
{
95+
return array_key_exists($key, $this->data);
96+
}
97+
98+
protected function getValue($key, $default = null)
99+
{
100+
if (!$this->hasKey($key)) {
101+
return $default;
102+
}
103+
104+
return $this->data[$key];
105+
}
106+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Nurmanhabib\Navigator\Factories;
4+
5+
use Nurmanhabib\Navigator\NavCollection;
6+
7+
interface NavCollectionFactory
8+
{
9+
/**
10+
* @return NavCollection
11+
*/
12+
public function createNavCollection();
13+
}

src/Factories/NavFactory.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Nurmanhabib\Navigator\Factories;
4+
5+
use Nurmanhabib\Navigator\Items\Nav;
6+
7+
interface NavFactory
8+
{
9+
/**
10+
* @return Nav
11+
*/
12+
public function createNav();
13+
}

0 commit comments

Comments
 (0)