|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Letournel\PathFinder\Core; |
|
4
|
|
|
|
|
5
|
|
|
use Letournel\PathFinder\Distance; |
|
6
|
|
|
|
|
7
|
|
|
class NodePath implements \Countable, \Iterator |
|
8
|
|
|
{ |
|
9
|
|
|
private |
|
10
|
|
|
$path, |
|
|
|
|
|
|
11
|
|
|
$valid; |
|
12
|
|
|
|
|
13
|
|
|
public function __construct(array $path) |
|
14
|
|
|
{ |
|
15
|
|
|
$this->path = array(); |
|
16
|
|
|
$this->valid = true; |
|
17
|
|
|
foreach($path as $node) |
|
18
|
|
|
{ |
|
19
|
|
|
if($node instanceof Node) |
|
20
|
|
|
{ |
|
21
|
|
|
$this->path[$node->getId()] = $node; |
|
22
|
|
|
} |
|
23
|
|
|
} |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
public function count() |
|
27
|
|
|
{ |
|
28
|
|
|
return count($this->path); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
public function current() |
|
32
|
|
|
{ |
|
33
|
|
|
return current($this->path); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
public function key() |
|
37
|
|
|
{ |
|
38
|
|
|
return key($this->path); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
public function next() |
|
42
|
|
|
{ |
|
43
|
|
|
$this->valid = (next($this->path) !== false); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
public function rewind() |
|
47
|
|
|
{ |
|
48
|
|
|
reset($this->path); |
|
49
|
|
|
$this->valid = true; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
public function valid() |
|
53
|
|
|
{ |
|
54
|
|
|
return $this->valid; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
public function getKey($key) |
|
58
|
|
|
{ |
|
59
|
|
|
return $this->path[$key]; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
public function getKeys() |
|
63
|
|
|
{ |
|
64
|
|
|
return array_keys($this->path); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
View Code Duplication |
public function computeLength(Distance $distance) |
|
|
|
|
|
|
68
|
|
|
{ |
|
69
|
|
|
$length = 0; |
|
70
|
|
|
$prevNode = null; |
|
71
|
|
|
foreach($this->path as $node) |
|
72
|
|
|
{ |
|
73
|
|
|
if($prevNode === null) |
|
74
|
|
|
{ |
|
75
|
|
|
$prevNode = $node; |
|
76
|
|
|
continue; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
$length += $distance->compute($prevNode, $node); |
|
80
|
|
|
$prevNode = $node; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
return $length; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
public function contains(Node $needleNode) |
|
87
|
|
|
{ |
|
88
|
|
|
foreach($this->path as $node) |
|
89
|
|
|
{ |
|
90
|
|
|
if($node->toString() == $needleNode->toString()) |
|
91
|
|
|
{ |
|
92
|
|
|
return true; |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
return false; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
public function getStartNode() |
|
100
|
|
|
{ |
|
101
|
|
|
return reset($this->path); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
public function getEndNode() |
|
105
|
|
|
{ |
|
106
|
|
|
return end($this->path); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
public function toString() |
|
110
|
|
|
{ |
|
111
|
|
|
$nodesString = array(); |
|
112
|
|
|
foreach($this->path as $node) |
|
113
|
|
|
{ |
|
114
|
|
|
$nodesString[] = $node->toString(); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
return implode('->', $nodesString); |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
|
Only declaring a single property per statement allows you to later on add doc comments more easily.
It is also recommended by PSR2, so it is a common style that many people expect.