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

Builder::addBefore()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 1
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
    {
100
        if (is_array($childPositionOld)) {
101
            $childPositions = $childPositionOld;
102
        } else {
103
            $childPositions = [];
104
            if ($childPositionOld) {
105
                $childPositions[] = $childPositionOld;
106
            }
107
        }
108
        foreach ($items as $key => $item) {
109
            if (isset($item['key']) && $item['key'] == $itemKey) {
110
                if ($childPositionOld) {
111
                    $childPositions[] = $key;
112
113
                    return $childPositions;
114
                }
115
116
                return $key;
117
            }
118
        }
119
        foreach ($items as $key => $item) {
120
            if (isset($item['submenu'])) {
121
                if ($childPositionOld) {
122
                    $childPositions[] = $key;
123
                    $childPosition = $this->findItem($itemKey, $item['submenu'], $childPositions);
124
                    $childPositions[] = $childPosition;
125
126
                    if (is_array($childPosition)) {
127
                        $childPositions = $childPosition;
128
                    }
129
130
                    return $childPositions;
131
                }
132
133
                return $this->findItem($itemKey, $item['submenu'], $key);
134
            }
135
        }
136
    }
137
138
    protected function applyFilters($item)
139
    {
140
        if (is_string($item)) {
141
            return $item;
142
        }
143
144
        foreach ($this->filters as $filter) {
145
            $item = $filter->transform($item, $this);
146
        }
147
148
        return $item;
149
    }
150
}
151