Completed
Push — master ( f6ec55...abffbf )
by Cheren
03:26
created

Nav::clear()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 8
rs 9.4285
cc 2
eloc 5
nc 2
nop 1
1
<?php
2
/**
3
 * CakeCMS Core
4
 *
5
 * This file is part of the of the simple cms based on CakePHP 3.
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 *
9
 * @package   Core
10
 * @license   MIT
11
 * @copyright MIT License http://www.opensource.org/licenses/mit-license.php
12
 * @link      https://github.com/CakeCMS/Core".
13
 * @author    Sergey Kalistratov <[email protected]>
14
 */
15
16
namespace Core;
17
use Cake\Log\Log;
18
use Cake\Utility\Hash;
19
use Cake\Utility\Inflector;
20
21
/**
22
 * Class Nav
23
 *
24
 * @package Core
25
 */
26
class Nav
27
{
28
29
    /**
30
     * Current active menu.
31
     *
32
     * @see CroogoNav::activeMenu()
33
     */
34
    protected static $_activeMenu = 'sidebar';
35
36
    /**
37
     * Menu items.
38
     *
39
     * @var array
40
     */
41
    protected static $_items = ['sidebar' => []];
42
43
    /**
44
     * Default params.
45
     *
46
     * @var array
47
     */
48
    protected static $_defaults = [
49
        'icon'           => '',
50
        'title'          => false,
51
        'url'            => '#',
52
        'weight'         => 9999,
53
        'before'         => false,
54
        'after'          => false,
55
        'access'         => [],
56
        'children'       => [],
57
        'htmlAttributes' => [],
58
    ];
59
60
    /**
61
     * Getter/setter for activeMenu
62
     *
63
     * @param null $menu
64
     * @return null|string
65
     */
66
    public static function activeMenu($menu = null)
67
    {
68
        if ($menu === null) {
69
            $activeMenu = self::$_activeMenu;
70
        } else {
71
            $activeMenu = $menu;
72
        }
73
74
        if (!array_key_exists($activeMenu, self::$_items)) {
75
            self::$_items[$activeMenu] = [];
76
        }
77
78
        self::$_activeMenu = $activeMenu;
79
        return $activeMenu;
80
    }
81
82
    /**
83
     * Add a menu item.
84
     *
85
     * @param $menu
86
     * @param $path
87
     * @param array $options
88
     */
89
    public static function add($menu, $path, $options = [])
90
    {
91
        // Juggle argument for backward compatibility
92
        if (is_array($path)) {
93
            $options = $path;
94
            $path    = $menu;
95
            $menu    = self::activeMenu();
96
        } else {
97
            self::activeMenu($menu);
98
        }
99
100
        $pathE  = explode('.', $path);
101
        $pathE  = array_splice($pathE, 0, count($pathE) - 2);
102
        $parent = join('.', $pathE);
103
104
        if (!empty($parent) && !Hash::check(self::$_items[$menu], $parent)) {
105
            $title = Inflector::humanize(end($pathE));
106
            $opt = ['title' => $title];
107
            self::_setupOptions($opt);
108
            self::add($parent, $opt);
109
        }
110
111
        self::_setupOptions($options);
112
        $current = Hash::extract(self::$_items[$menu], $path);
113
114
        if (!empty($current)) {
115
            self::_replace(self::$_items[$menu], $path, $options);
116
        } else {
117
            self::$_items[$menu] = Hash::insert(self::$_items[$menu], $path, $options);
118
        }
119
    }
120
121
    /**
122
     * Clear all menus.
123
     *
124
     * @param string $menu
125
     * @return void
126
     */
127
    public static function clear($menu = 'sidebar')
128
    {
129
        if ($menu) {
130
            self::_clear($menu);
131
        } else {
132
            self::$_items = [];
133
        }
134
    }
135
136
    /**
137
     * Gets default settings for menu items.
138
     *
139
     * @return array
140
     */
141
    public static function getDefaults()
142
    {
143
        return self::$_defaults;
144
    }
145
146
    /**
147
     * Sets or returns menu data in array.
148
     *
149
     * @param string $menu
150
     * @param null $items
151
     * @return array
152
     */
153
    public static function items($menu = 'sidebar', $items = null)
154
    {
155
        if (!is_string($menu)) {
156
            throw new \UnexpectedValueException('Menu id is not a string');
157
        }
158
159
        if (!empty($items)) {
160
            self::$_items[$menu] = $items;
161
        }
162
163
        if (!array_key_exists($menu, self::$_items)) {
164
            Log::error('Invalid menu: ' . $menu);
165
            return [];
166
        }
167
168
        return self::$_items[$menu];
169
    }
170
171
    /**
172
     * Get menus.
173
     *
174
     * @return array
175
     */
176
    public static function menus()
177
    {
178
        return array_keys(self::$_items);
179
    }
180
181
    /**
182
     * Remove a menu item.
183
     *
184
     * @param string $path dot separated path in the array.
185
     * @return void
186
     */
187
    public static function remove($path)
188
    {
189
        self::$_items = Hash::remove(self::$_items, $path);
0 ignored issues
show
Documentation Bug introduced by
It seems like \Cake\Utility\Hash::remove(self::$_items, $path) can be null. However, the property $_items is declared as array. Maybe change the type of the property to array|null or add a type check?

Our type inference engine has found an assignment of a scalar value (like a string, an integer or null) to a property which is an array.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property.

To type hint that a parameter can be either an array or null, you can set a type hint of array and a default value of null. The PHP interpreter will then accept both an array or null for that parameter.

function aContainsB(array $needle = null, array  $haystack) {
    if (!$needle) {
        return false;
    }

    return array_intersect($haystack, $needle) == $haystack;
}

The function can be called with either null or an array for the parameter $needle but will only accept an array as $haystack.

Loading history...
190
    }
191
192
    /**
193
     * Clear menu items.
194
     *
195
     * @param $menu
196
     * @throws \UnexpectedValueException
197
     */
198
    protected static function _clear($menu)
199
    {
200
        if (array_key_exists($menu, self::$_items)) {
201
            self::$_items[$menu] = [];
202
        } else {
203
            throw new \UnexpectedValueException('Invalid menu: ' . $menu);
204
        }
205
    }
206
207
    /**
208
     * Merge $firstArray with $secondArray.
209
     *
210
     * Similar to Hash::merge, except duplicates are removed
211
     * @param array $firstArray
212
     * @param array $secondArray
213
     * @return array
214
     */
215
    protected static function _merge($firstArray, $secondArray)
216
    {
217
        $merged = Hash::merge($firstArray, $secondArray);
218
        foreach ($merged as $key => $val) {
219
            if (is_array($val) && is_int(key($val))) {
220
                $merged[$key] = array_unique($val);
221
            }
222
        }
223
        return $merged;
224
    }
225
226
    /**
227
     * Replace a menu element.
228
     *
229
     * @param array $target pointer to start of array
230
     * @param string $path path to search for in dot separated format
231
     * @param array $options data to replace with
232
     * @return void
233
     */
234
    protected static function _replace(&$target, $path, $options)
235
    {
236
        $pathE    = explode('.', $path);
237
        $path     = array_shift($pathE);
238
        $fragment = join('.', $pathE);
239
240
        if (!empty($pathE)) {
241
            self::_replace($target[$path], $fragment, $options);
242
        } else {
243
            $target[$path] = self::_merge($target[$path], $options);
244
        }
245
    }
246
247
    /**
248
     * Setup options.
249
     *
250
     * @param array $options
251
     * @return void
252
     */
253
    protected static function _setupOptions(&$options)
254
    {
255
        $options = self::_merge(self::$_defaults, $options);
256
        foreach ($options['children'] as &$child) {
257
            self::_setupOptions($child);
258
        }
259
    }
260
}
261