1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @author ventaquil <[email protected]> |
5
|
|
|
* @licence MIT |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace PHPAlgorithms\Dijkstra; |
9
|
|
|
|
10
|
|
|
use PHPAlgorithms\Dijkstra\Exceptions\PathException; |
11
|
|
|
use PHPAlgorithms\GraphTools\Traits\MagicGet; |
12
|
|
|
|
13
|
|
|
class Path { |
14
|
|
|
use MagicGet; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var Point[] $nodes Added points to current path. |
18
|
|
|
* @var integer $distance Distance of the path. |
19
|
|
|
*/ |
20
|
|
|
private $nodes = array(); |
21
|
|
|
private $distance = 0; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Method adds node to end of current path. |
25
|
|
|
* |
26
|
|
|
* @param Point|integer $point Point object or point identifier. |
27
|
|
|
* @param integer $distance Distance between this node and the last one in path. |
28
|
|
|
* @return Path Reference to current object. |
29
|
|
|
* @throws PathException Method throws exception when checkDistance() method throws exception. |
30
|
|
|
*/ |
31
|
|
|
public function addNode($point, $distance = 0) |
32
|
|
|
{ |
33
|
|
|
if ($point instanceof Point) { |
34
|
|
|
$point = $point->id; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
$this->nodes[] = $point; |
38
|
|
|
|
39
|
|
|
$this->checkDistance($distance); |
40
|
|
|
|
41
|
|
|
$this->distance += $distance; |
42
|
|
|
|
43
|
|
|
return $this; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Method checks given $distance value. |
48
|
|
|
* |
49
|
|
|
* @param integer $distance |
50
|
|
|
* @throws PathException Method throws exception when $distance is not numeric value or when $distance is less or equal to 0. |
51
|
|
|
*/ |
52
|
|
|
public function checkDistance($distance) |
53
|
|
|
{ |
54
|
|
|
if (!is_numeric($distance) && ($distance <= 0)) { |
55
|
|
|
throw new PathException('Distance must be numeric value greater than 0'); |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Method copy data of given Path object to current class instance. |
61
|
|
|
* |
62
|
|
|
* @param Path $path Path object whose data we want to copy. |
63
|
|
|
* @return Path $this Reference to current object. |
64
|
|
|
*/ |
65
|
|
|
public function copy(self $path) |
66
|
|
|
{ |
67
|
|
|
$this->nodes = $path->nodes; |
68
|
|
|
$this->distance = $path->distance; |
69
|
|
|
|
70
|
|
|
return $this; |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|