|
| 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 | +} |
0 commit comments