Menu::setActive()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 9
rs 9.6666
cc 2
eloc 4
nc 2
nop 1
1
<?php
2
3
namespace Roboc\Menu;
4
5
use Roboc\Support\Interfaces\MenuItemInterface;
6
7
/**
8
 * Class Menu
9
 * @package Roboc\Menu
10
 */
11
class Menu
12
{
13
    /**
14
     * @var string
15
     */
16
    protected $identifier;
17
18
    /**
19
     * @var array
20
     */
21
    protected $items = [];
22
23
    /**
24
     * @var \Roboc\Support\Interfaces\MenuItemInterface
25
     */
26
    protected $parent;
27
28
    /**
29
     * Menu constructor.
30
     * @param string $identifier
31
     */
32
    public function __construct( $identifier )
33
    {
34
        $this->identifier = $identifier;
35
    }
36
37
    /**
38
     * @param MenuItemInterface $item
39
     * @return \Roboc\Support\Interfaces\MenuItemInterface
40
     */
41
    public function add( MenuItemInterface $item )
42
    {
43
        $this->items[] = $item;
44
45
        return $item;
46
    }
47
48
    /**
49
     * @param string $title
50
     * @return Item
51
     */
52
    public function item( $title )
53
    {
54
        return $this->add( new Item( $this ) )->title( $title );
55
    }
56
57
    /**
58
     * @return MenuItemInterface[]
59
     */
60
    public function items()
61
    {
62
        return $this->items;
63
    }
64
65
    /**
66
     * @return MenuItemInterface
67
     */
68
    public function parent()
69
    {
70
        return $this->parent;
71
    }
72
73
    /**
74
     * @return bool
75
     */
76
    public function hasParent()
77
    {
78
        return $this->parent !== null;
79
    }
80
81
    /**
82
     * @param $link
83
     * @return MenuItemInterface
0 ignored issues
show
Documentation introduced by
Should the return type not be MenuItemInterface|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
84
     */
85
    public function getItemByLink( $link )
86
    {
87
        foreach( $this->items() as $item )
88
        {
89
            if( $item->getLink() === $link )
90
            {
91
                return $item;
92
            }
93
94
            if( $item->hasSubMenu() )
95
            {
96
                 $item = $item->getSubMenu()->getItemByLink( $link );
97
98
                if( $item )
99
                {
100
                    return $item;
101
                }
102
            }
103
        }
104
105
        return null;
106
    }
107
108
    /**
109
     * @param $link
110
     */
111
    public function setActive( $link )
112
    {
113
        $item = $this->getItemByLink( $link );
114
115
        if( $item )
116
        {
117
            $item->active( true );
118
        }
119
    }
120
121
    /**
122
     * @param MenuItemInterface $item
123
     */
124
    public function setParent( MenuItemInterface $item )
125
    {
126
        $this->parent = $item;
127
    }
128
129
    /**
130
     * @return bool
131
     */
132
    public function hasItems()
133
    {
134
        return (bool) count( $this->items );
135
    }
136
}
137