Item   A
last analyzed

Complexity

Total Complexity 24

Size/Duplication

Total Lines 123
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 123
rs 10
c 0
b 0
f 0
wmc 24
lcom 1
cbo 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 14 3
A make() 0 8 2
B makeLiClass() 0 21 7
A makeLiAttributesStr() 0 8 3
A makeAAttributesStr() 0 8 3
A setActiveItems() 0 4 1
A setChildren() 0 4 1
A render() 0 20 3
A __toString() 0 4 1
1
<?php
2
/**
3
 * @copyright Copyright (c) 2011 - 2015 Oleksandr Torosh (http://wezoom.net)
4
 * @author Oleksandr Torosh <[email protected]>
5
 */
6
7
namespace Menu;
8
9
class Item
10
{
11
12
    public $title;
13
    public $id;
14
    public $url;
15
    public $params;
16
    public $children = [];
17
18
    private $href;
19
    private $li_attributes = [];
20
    private $li_attributes_str;
21
    private $a_attributes = [];
22
    private $a_attributes_str;
23
    private $active_items = [];
24
25
    /**
26
     * @param $title
27
     * @param integer $id
28
     * @param string $url
29
     * @param array $params
30
     */
31
    public function __construct($title, $id = null, $url = null, array $params = [])
32
    {
33
        $this->title = $title;
34
        $this->id = $id;
35
        $this->url = $url;
36
        $this->params = $params;
37
38
        if (isset($this->params['li'])) {
39
            $this->li_attributes = $this->params['li'];
40
        }
41
        if (isset($this->params['a'])) {
42
            $this->a_attributes = $this->params['a'];
43
        }
44
    }
45
46
    public function make()
47
    {
48
        $this->makeLiClass();
49
        $this->makeLiAttributesStr();
50
        $this->makeAAttributesStr();
51
52
        $this->href = ($this->url) ? $this->url : "javascript:void(0);";
53
    }
54
55
    private function makeLiClass()
56
    {
57
        $class = [];
58
        if (!empty($this->children)) {
59
            $class[] = 'parent';
60
        }
61
        if (isset($this->li_attributes['class'])) {
62
            $class[] = $this->li_attributes['class'];
63
        }
64
        if (in_array($this->id, $this->active_items)) {
65
            $class[] = 'active';
66
        } elseif (!empty($this->children)) {
67
            foreach ($this->children as $child) {
68
                if (in_array($child->id, $this->active_items)) {
69
                    $class[] = 'active';
70
                }
71
            }
72
        }
73
74
        $this->li_attributes['class'] = implode(' ', $class);
75
    }
76
77
    private function makeLiAttributesStr()
78
    {
79
        if (!empty($this->li_attributes)) {
80
            foreach ($this->li_attributes as $key => $value) {
81
                $this->li_attributes_str .= ' ' . $key . '="' . $value . '"';
82
            }
83
        }
84
    }
85
86
    private function makeAAttributesStr()
87
    {
88
        if (!empty($this->a_attributes)) {
89
            foreach ($this->a_attributes as $key => $value) {
90
                $this->a_attributes_str .= ' ' . $key . '="' . $value . '"';
91
            }
92
        }
93
    }
94
95
    public function setActiveItems($active_items)
96
    {
97
        $this->active_items = $active_items;
98
    }
99
100
    public function setChildren(array $children = [])
101
    {
102
        $this->children = $children;
103
    }
104
105
    public function render()
106
    {
107
        $html = "<li{$this->li_attributes_str}>
108
            <a href=\"{$this->href}\"{$this->a_attributes_str}>{$this->title}</a>\n";
109
110
        if (!empty($this->children)) {
111
            $html .= "<ul>";
112
            foreach ($this->children as $child) {
113
                $childItem = new Item($child->title, $child->id, $child->url, $child->params);
114
                $childItem->setActiveItems($this->active_items);
115
                $childItem->make();
116
                $html .= $childItem->render();
117
            }
118
            $html .= "</ul>";
119
        }
120
121
        $html .= "</li>\n";
122
123
        return $html;
124
    }
125
126
    public function __toString()
127
    {
128
        return $this->render();
129
    }
130
131
}