Passed
Pull Request — master (#53)
by Wilmer
02:51
created

Normalize   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 121
Duplicated Lines 0 %

Importance

Changes 1
Bugs 1 Features 1
Metric Value
eloc 41
c 1
b 1
f 1
dl 0
loc 121
rs 10
wmc 18

4 Methods

Rating   Name   Duplication   Size   Complexity  
A dropdown() 0 33 5
A isItemActive() 0 3 2
A menu() 0 39 6
A label() 0 18 5
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Yii\Widgets\Helper;
6
7
use InvalidArgumentException;
8
use Yiisoft\Html\Html;
9
10
final class Normalize
11
{
12
    public static function dropdown(array $items): array
13
    {
14
        /**
15
         * @psalm-var array[] $items
16
         * @psalm-suppress RedundantConditionGivenDocblockType
17
         */
18
        foreach ($items as $i => $child) {
19
            if (is_array($child)) {
20
                $items[$i]['label'] = self::label($child);
21
                /** @var bool */
22
                $items[$i]['active'] = $child['active'] ?? false;
23
                /** @var bool */
24
                $items[$i]['disabled'] = $child['disabled'] ?? false;
25
                /** @var string */
26
                $items[$i]['link'] = $child['link'] ?? '/';
27
                /** @var string */
28
                $items[$i]['icon'] = $child['icon'] ?? '';
29
                /** @var array */
30
                $items[$i]['iconAttributes'] = $child['iconAttributes'] ?? [];
31
                /** @var string */
32
                $items[$i]['iconClass'] = $child['iconClass'] ?? '';
33
                /** @var array */
34
                $items[$i]['iconContainerAttributes'] = $child['iconContainerAttributes'] ?? [];
35
                /** @var bool */
36
                $items[$i]['visible'] = $child['visible'] ?? true;
37
38
                if (isset($child['items']) && is_array($child['items'])) {
39
                    $items[$i]['items'] = self::dropdown($child['items']);
40
                }
41
            }
42
        }
43
44
        return $items;
45
    }
46
47
    /**
48
     * Normalize the given array of items for the menu.
49
     *
50
     * @param array $items The items to be normalized.
51
     * @param string $currentPath The current path.
52
     * @param bool $activateItems Whether to activate items.
53
     *
54
     * @return array The normalized array of items.
55
     */
56
    public static function menu(array $items, string $currentPath, bool $activateItems): array
57
    {
58
        /**
59
         * @psalm-var array[] $items
60
         * @psalm-suppress RedundantConditionGivenDocblockType
61
         */
62
        foreach ($items as $i => $child) {
63
            if (is_array($child)) {
64
                if (isset($child['items']) && is_array($child['items'])) {
65
                    $items[$i]['items'] = self::menu($child['items'], $currentPath, $activateItems);
66
                } else {
67
                    $items[$i]['label'] = self::label($child);
68
69
                    /** @var string */
70
                    $link = $child['link'] ?? '/';
71
                    /** @var bool */
72
                    $active = $child['active'] ?? false;
73
74
                    if ($active === false) {
75
                        $items[$i]['active'] = self::isItemActive($link, $currentPath, $activateItems);
76
                    }
77
78
                    /** @var bool */
79
                    $items[$i]['disabled'] = $child['disabled'] ?? false;
80
                    /** @var string */
81
                    $items[$i]['icon'] = $child['icon'] ?? '';
82
                    /** @var array */
83
                    $items[$i]['iconAttributes'] = $child['iconAttributes'] ?? [];
84
                    /** @var string */
85
                    $items[$i]['iconClass'] = $child['iconClass'] ?? '';
86
                    /** @var array */
87
                    $items[$i]['iconContainerAttributes'] = $child['iconContainerAttributes'] ?? [];
88
                    /** @var bool */
89
                    $items[$i]['visible'] = $child['visible'] ?? true;
90
                }
91
            }
92
        }
93
94
        return $items;
95
    }
96
97
    /**
98
     * Checks whether a menu item is active.
99
     *
100
     * This is done by checking match that specified in the `url` option of the menu item.
101
     *
102
     * @param string $link The link of the menu item.
103
     * @param string $currentPath The current path.
104
     * @param bool $activateItems Whether to activate items having no link.
105
     *
106
     * @return bool Whether the menu item is active.
107
     */
108
    private static function isItemActive(string $link, string $currentPath, bool $activateItems): bool
109
    {
110
        return ($link === $currentPath) && $activateItems;
111
    }
112
113
    private static function label(array $item): string
114
    {
115
        if (!isset($item['label'])) {
116
            throw new InvalidArgumentException('The "label" option is required.');
117
        }
118
119
        if (!is_string($item['label'])) {
120
            throw new InvalidArgumentException('The "label" option must be a string.');
121
        }
122
123
        if ($item['label'] === '') {
124
            throw new InvalidArgumentException('The "label" cannot be an empty string.');
125
        }
126
127
        /** @var bool */
128
        $encode = $item['encode'] ?? true;
129
130
        return $encode ? Html::encode($item['label']) : $item['label'];
131
    }
132
}
133