|
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
|
40 |
|
public function __construct() |
|
39
|
|
|
{ |
|
40
|
40 |
|
$this->hash = uniqid('', true); |
|
41
|
40 |
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Check if two edges are equal |
|
45
|
|
|
* |
|
46
|
|
|
* @param EdgeInterface $other - other edge to be compared |
|
47
|
|
|
*/ |
|
48
|
8 |
|
public function equals(?EdgeInterface $other = null): bool |
|
49
|
|
|
{ |
|
50
|
8 |
|
return !is_null($other) && $this->hash == $other->getHash(); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Get the edge hash |
|
55
|
|
|
* |
|
56
|
|
|
* @return string |
|
57
|
|
|
*/ |
|
58
|
9 |
|
public function getHash(): string |
|
59
|
|
|
{ |
|
60
|
9 |
|
return $this->hash; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Get the edge source vertex |
|
65
|
|
|
* |
|
66
|
|
|
* @return VertexInterface |
|
67
|
|
|
*/ |
|
68
|
30 |
|
public function getSource(): VertexInterface |
|
69
|
|
|
{ |
|
70
|
30 |
|
return $this->sourceVertex; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Get the edge target vertex |
|
75
|
|
|
* |
|
76
|
|
|
* @return VertexInterface |
|
77
|
|
|
*/ |
|
78
|
30 |
|
public function getTarget(): VertexInterface |
|
79
|
|
|
{ |
|
80
|
30 |
|
return $this->targetVertex; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Set the edge source vertex |
|
85
|
|
|
* |
|
86
|
|
|
* @param VertexInterface $vertex - the source vertex |
|
87
|
|
|
*/ |
|
88
|
31 |
|
public function setSource(VertexInterface $vertex): void |
|
89
|
|
|
{ |
|
90
|
31 |
|
$this->sourceVertex = $vertex; |
|
91
|
31 |
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Set the edge target vertex |
|
95
|
|
|
* |
|
96
|
|
|
* @param VertexInterface $vertex - the target vertex |
|
97
|
|
|
*/ |
|
98
|
31 |
|
public function setTarget(VertexInterface $vertex): void |
|
99
|
|
|
{ |
|
100
|
31 |
|
$this->targetVertex = $vertex; |
|
101
|
31 |
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Get the edge string representation |
|
105
|
|
|
* |
|
106
|
|
|
* @return string |
|
107
|
|
|
*/ |
|
108
|
4 |
|
public function __toString(): string |
|
109
|
|
|
{ |
|
110
|
4 |
|
return "(" . (string) $this->sourceVertex . " : " . (string) $this->targetVertex . ")"; |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|