MenuHelper   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 42
dl 0
loc 83
rs 10
c 0
b 0
f 0
wmc 11

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __toString() 0 25 5
A help() 0 4 1
A setCurrentUrl() 0 4 1
A setAlias() 0 4 1
A addCssClass() 0 4 1
A __construct() 0 4 1
A setHasLinks() 0 4 1
1
<?php
2
3
namespace ntentan\honam\engines\php\helpers;
4
5
use ntentan\honam\engines\php\Helper;
6
use ntentan\honam\TemplateRenderer;
7
use ntentan\utils\Input;
8
9
/**
10
 * A helper class for generating HTML menus.
11
 */
12
class MenuHelper extends Helper
13
{
14
    /**
15
     * All items in the menu.
16
     * @var array
17
     */
18
    private $items = array();
19
    private $cssClasses = [
20
        'menu' => [],
21
        'item' => [],
22
        'selected' => [],
23
        'matched' => []
24
    ];
25
    private $currentUrl;
26
    private $alias;
27
    private $hasLinks = true;
28
29
    const MENU = 'menu';
30
    const ITEM = 'item';
31
    const SELECTED_ITEM = 'selected';
32
    const MATCHED_ITEM = 'matched';
33
34
    public function __construct(TemplateRenderer $templateRenderer)
35
    {
36
        parent::__construct($templateRenderer);
37
        $this->setCurrentUrl(filter_var($_SERVER["REQUEST_URI"], FILTER_SANITIZE_URL));
38
    }
39
40
    public function help($items = null)
41
    {
42
        $this->items = $items;
43
        return $this;
44
    }
45
46
    public function addCssClass($class, $section = self::MENU)
47
    {
48
        $this->cssClasses[$section][] = $class;
49
        return $this;
50
    }
51
52
    public function setAlias($alias)
53
    {
54
        $this->alias = $alias;
55
        return $this;
56
    }
57
58
    public function setCurrentUrl($currentUrl)
59
    {
60
        $this->currentUrl = $currentUrl;
61
        return $this;
62
    }
63
64
    public function setHasLinks($hasLinks)
65
    {
66
        $this->hasLinks = $hasLinks;
67
        return $this;
68
    }
69
70
    public function __toString()
71
    {
72
        $menuItems = array();
73
74
        foreach ($this->items as $index => $item) {
75
            if (is_string($item) || is_numeric($item)) {
76
                $item = [
77
                    'label' => $item,
78
                    'url' => is_string($index) ? $index : $this->makeFullUrl(strtolower(str_replace(' ', '_', $item))),
79
                    'default' => null
80
                ];
81
            }
82
83
            $item['selected'] = $item['url'] == substr($this->currentUrl, 0, strlen($item['url']));
84
            $item['fully_matched'] = $item['url'] == $this->currentUrl;
85
            $menuItems[$index] = $item;
86
        }
87
88
        return $this->templateRenderer->render(
89
            "{$this->alias}_menu.tpl.php",
90
            [
91
                'items' => $menuItems,
92
                'css_classes' => $this->cssClasses,
93
                'has_links' => $this->hasLinks,
94
                'alias' => $this->alias
95
            ]
96
        );
97
    }
98
}
99