Completed
Push — master ( f52c87...2cae32 )
by Ventaquil
02:08
created

Path::__get()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
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