1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace graphp\edge; |
4
|
|
|
|
5
|
|
|
use graphp\vertex\VertexInterface; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class DefaultEdge |
9
|
|
|
* |
10
|
|
|
* @package graphp\edge |
11
|
|
|
*/ |
12
|
|
|
class DefaultEdge implements EdgeInterface |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* The edge unique hash |
16
|
|
|
* |
17
|
|
|
* @var string |
18
|
|
|
*/ |
19
|
|
|
private $hash; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* The edge source vertex |
23
|
|
|
* |
24
|
|
|
* @var VertexInterface |
25
|
|
|
*/ |
26
|
|
|
protected $sourceVertex; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* The edge target vertex |
30
|
|
|
* |
31
|
|
|
* @var VertexInterface |
32
|
|
|
*/ |
33
|
|
|
protected $targetVertex; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Construct a new edge |
37
|
|
|
*/ |
38
|
11 |
|
public function __construct() |
39
|
|
|
{ |
40
|
11 |
|
$this->hash = uniqid('', true); |
41
|
11 |
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Check if two edges are equal |
45
|
|
|
* |
46
|
|
|
* @param EdgeInterface $other - other edge to be compared |
47
|
|
|
*/ |
48
|
5 |
|
public function equals(EdgeInterface $other): bool |
49
|
|
|
{ |
50
|
5 |
|
return $this->hash == $other->getHash(); |
|
|
|
|
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Get the edge hash |
55
|
|
|
* |
56
|
|
|
* @return string |
57
|
|
|
*/ |
58
|
6 |
|
public function getHash(): string |
59
|
|
|
{ |
60
|
6 |
|
return $this->hash; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Get the edge source vertex |
65
|
|
|
* |
66
|
|
|
* @return VertexInterface |
67
|
|
|
*/ |
68
|
2 |
|
public function getSource(): VertexInterface |
69
|
|
|
{ |
70
|
2 |
|
return $this->sourceVertex; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Get the edge target vertex |
75
|
|
|
* |
76
|
|
|
* @return VertexInterface |
77
|
|
|
*/ |
78
|
2 |
|
public function getTarget(): VertexInterface |
79
|
|
|
{ |
80
|
2 |
|
return $this->targetVertex; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Set the edge source vertex |
85
|
|
|
* |
86
|
|
|
* @param VertexInterface $vertex - the source vertex |
87
|
|
|
*/ |
88
|
2 |
|
public function setSource(VertexInterface $vertex): void |
89
|
|
|
{ |
90
|
2 |
|
$this->sourceVertex = $vertex; |
91
|
2 |
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Set the edge target vertex |
95
|
|
|
* |
96
|
|
|
* @param VertexInterface $vertex - the target vertex |
97
|
|
|
*/ |
98
|
2 |
|
public function setTarget(VertexInterface $vertex): void |
99
|
|
|
{ |
100
|
2 |
|
$this->targetVertex = $vertex; |
101
|
2 |
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Get the edge string representation |
105
|
|
|
* |
106
|
|
|
* @return string |
107
|
|
|
*/ |
108
|
|
|
public function __toString(): string |
109
|
|
|
{ |
110
|
|
|
return "(" . $this->sourceVertex + " : " + $this->targetVertex + ")"; |
|
|
|
|
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|