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