Completed
Pull Request — master (#477)
by
unknown
02:16
created

Builder::remove()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 4
cts 4
cp 1
rs 9.7333
c 0
b 0
f 0
cc 4
nc 2
nop 1
crap 4
1
<?php
2
3
namespace JeroenNoten\LaravelAdminLte\Menu;
4
5
use Illuminate\Support\Arr;
6
7
class Builder
8
{
9
    public $menu = [];
10
11
    /**
12
     * @var array
13
     */
14 21
    private $filters;
15
16 21
    public function __construct(array $filters = [])
17 21
    {
18
        $this->filters = $filters;
19 21
    }
20
21 21
    public function add()
22
    {
23 21
        $items = $this->transformItems(func_get_args());
24 21
25
        foreach ($items as $item) {
26 21
            array_push($this->menu, $item);
27
        }
28 21
    }
29
30 21
    public function addAfter($itemKey, ...$newItems)
31
    {
32
        $this->addItem($itemKey, 'after', ...$newItems);
33 21
    }
34
35 21
    public function addBefore($itemKey, ...$newItems)
36 1
    {
37
        $this->addItem($itemKey, 'before', ...$newItems);
38
    }
39 20
40 19
    public function remove($itemKey)
41
    {
42
        $completeArrayPath = '';
43 20
        $itemPath = $this->findItem($itemKey, $this->menu);
44 2
        if (is_array($itemPath)) {
45
            foreach ($itemPath as $key => $value) {
46
                if ($completeArrayPath === '') {
47 20
                    $completeArrayPath .= "$value";
48
                } else {
49
                    $completeArrayPath .= ".submenu.$value";
50
                }
51
            }
52
            $itemPath = $completeArrayPath;
53
        }
54
        Arr::forget($this->menu, $itemPath);
55
    }
56
57
    public function itemKeyExists($itemKey)
58
    {
59
        if ($this->findItem($itemKey, $this->menu)) {
60
            return true;
61
        }
62
63
        return false;
64
    }
65
66
    public function transformItems($items)
67
    {
68
        return array_filter(array_map([$this, 'applyFilters'], $items));
69
    }
70
71
    protected function addItem($itemKey, $direction, ...$newItems)
72
    {
73
        $items = $this->transformItems($newItems);
74
75
        $position = $this->findItem($itemKey, $this->menu);
76
        if ($position !== null) {
77
            if (!is_array($position)) {
78
                array_splice($this->menu, $position+($direction == 'after' ? 1 : 0), 0, $items);
79
            } else {
80
                $completeArrayPath = $lastKey = '';
81
                foreach ($position as $key => $value) {
82
                    if ($completeArrayPath === '') {
83
                        $completeArrayPath .= "$value";
84
                    } else {
85
                        $completeArrayPath .= ".submenu.$value";
86
                        $lastKey = $value;
87
                    }
88
                }
89
90
                $arrayPath = substr($completeArrayPath, 0, -(strlen(".$lastKey")));
91
                $menuItems = Arr::get($this->menu, $arrayPath);
92
                array_splice($menuItems, $lastKey+($direction == 'after' ? 1 : 0), 0, $items);
93
                Arr::set($this->menu, $arrayPath, $menuItems);
94
            }
95
        }
96
    }
97
98
    protected function findItem($itemKey, $items, $childPositionOld = null) {
99
        if (is_array($childPositionOld)) {
100
            $childPositions = $childPositionOld;
101
        } else {
102
            $childPositions = [];
103
            if ($childPositionOld) {
104
                $childPositions[] = $childPositionOld;
105
            }
106
        }
107
        foreach ($items as $key => $item) {
108
            if (isset($item['key']) && $item['key'] == $itemKey) {
109
                if ($childPositionOld) {
110
                    $childPositions[] = $key;
111
112
                    return $childPositions;
113
                }
114
115
                return $key;
116
            }
117
        }
118
        foreach ($items as $key => $item) {
119
            if (isset($item['submenu'])) {
120
                if ($childPositionOld) {
121
                    $childPositions[] = $key;
122
                    $childPosition = $this->findItem($itemKey, $item['submenu'], $childPositions);
123
                    $childPositions[] = $childPosition;
124
125
                    if (is_array($childPosition)) {
126
                        $childPositions = $childPosition;
127
                    }
128
129
                    return $childPositions;
130
                }
131
132
                return $this->findItem($itemKey, $item['submenu'], $key);
133
            }
134
        }
135
    }
136
137
    protected function applyFilters($item)
138
    {
139
        if (is_string($item)) {
140
            return $item;
141
        }
142
143
        foreach ($this->filters as $filter) {
144
            $item = $filter->transform($item, $this);
145
        }
146
147
        return $item;
148
    }
149
}
150