Passed
Push — master ( 735275...20e747 )
by Ivan
02:20
created

NavigationItem::__construct1()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 1
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace Everlution\Navigation;
6
7
/**
8
 * Class NavigationItem.
9
 * @author Ivan Barlog <[email protected]>
10
 */
11
class NavigationItem implements Item
12
{
13
    /** @var string */
14
    private $uri;
15
    /** @var string */
16
    private $label;
17
    /** @var Item */
18
    private $parent;
19
    /** @var NavigationItem[] */
20
    private $children = [];
21
22
    public function __construct(string $uri, string $label, Item $parent = null, array $children = [])
23
    {
24
        $this->uri = $uri;
25
        $this->label = $label;
26
        $this->parent = $parent;
27
28
        foreach ($children as $child) {
29
            $this->addChild($child);
30
        }
31
    }
32
33
    /**
34
     * @param Item $item
35
     * @return NavigationItem
36
     */
37
    public function addChild(Item $item): NavigationItem
38
    {
39
        if (!$item instanceof NavigationItem) {
40
            throw new \InvalidArgumentException(
41
                sprintf("Item must be instance of %s'", NavigationItem::class)
42
            );
43
        }
44
45
        $this->children[] = $item->setParent($this);
46
47
        return $this;
48
    }
49
50
    /**
51
     * @return NavigationItem[]
52
     */
53
    public function getChildren(): array
54
    {
55
        return $this->children;
56
    }
57
58
    /**
59
     * @return string
60
     */
61
    public function getUri(): string
62
    {
63
        return $this->uri;
64
    }
65
66
    /**
67
     * @return string
68
     */
69
    public function getLabel(): string
70
    {
71
        return $this->label;
72
    }
73
74
    /**
75
     * @param $parent
76
     * @return NavigationItem
77
     */
78
    public function setParent($parent): NavigationItem
79
    {
80
        $this->parent = $parent;
81
82
        return $this;
83
    }
84
85
    /**
86
     * @return Item
87
     */
88
    public function getParent(): Item
89
    {
90
        return $this->parent;
91
    }
92
}
93