Completed
Pull Request — master (#304)
by Atlas
02:28
created

Node   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 108
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 108
rs 10
wmc 11
lcom 1
cbo 1

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getKey() 0 4 1
A getAdjacents() 0 13 4
A getEdges() 0 4 1
A addEdge() 0 5 1
A getData() 0 4 1
A setData() 0 5 1
A getUniqueId() 0 4 1
1
<?php
2
3
/*
4
 * (c) Jean-François Lépine <https://twitter.com/Halleck45>
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace Hal\Component\Tree;
11
12
class Node
13
{
14
15
    /**
16
     * @var mixed
17
     */
18
    private $data;
19
20
    /**
21
     * @var string
22
     */
23
    private $key;
24
25
    /**
26
     * @var Edge[]
27
     */
28
    private $edges = array();
29
30
    /**
31
     * @var bool
32
     */
33
    public $visited = false;
34
35
    /**
36
     * @var bool
37
     */
38
    public $cyclic = false;
39
40
    /**
41
     * Node constructor.
42
     * @param $key
43
     * @param mixed $data
44
     */
45
    public function __construct($key, $data = null)
46
    {
47
        $this->key = $key;
48
        $this->data = $data;
49
    }
50
51
    /**
52
     * @return string
53
     */
54
    public function getKey()
55
    {
56
        return $this->key;
57
    }
58
59
    /**
60
     * @return array
61
     */
62
    public function getAdjacents()
63
    {
64
        $adjacents = [];
65
        foreach ($this->edges as $edge) {
66
            if ($edge->getFrom()->getKey() != $this->getKey()) {
67
                $adjacents[$edge->getFrom()->getKey()] = $edge->getFrom();
68
            }
69
            if ($edge->getTo()->getKey() != $this->getKey()) {
70
                $adjacents[$edge->getTo()->getKey()] = $edge->getTo();
71
            }
72
        }
73
        return $adjacents;
74
    }
75
76
    /**
77
     * @return Edge[]
78
     */
79
    public function getEdges()
80
    {
81
        return $this->edges;
82
    }
83
84
    /**
85
     * @param Edge $edge
86
     * @return $this
87
     */
88
    public function addEdge(Edge $edge)
89
    {
90
        array_push($this->edges, $edge);
91
        return $this;
92
    }
93
94
    /**
95
     * @return mixed
96
     */
97
    public function getData()
98
    {
99
        return $this->data;
100
    }
101
102
    /**
103
     * @param mixed $data
104
     * @return Node
105
     */
106
    public function setData($data)
107
    {
108
        $this->data = $data;
109
        return $this;
110
    }
111
112
    /**
113
     * @return string Unique id for this node independent of class name or node type
114
     */
115
    public function getUniqueId()
116
    {
117
        return spl_object_hash($this);
118
    }
119
}
120