Completed
Push — master ( 4fc6a7...099af8 )
by Ventaquil
03:19
created

Point::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 0 Features 0
Metric Value
c 5
b 0
f 0
dl 0
loc 9
rs 9.6667
cc 2
eloc 6
nc 2
nop 2
1
<?php
2
namespace Algorithms\Dijkstra;
3
4
class Point
5
{
6
    private $id;
7
    private $distances = array();
8
    private $label = null;
9
10
    public function __construct($point_id, $label = null)
11
    {
12
        if (is_int($point_id)) {
13
            $this->id = $point_id;
14
            $this->label = $label;
15
        } else {
16
            throw new PointException('Wrong data sent');
17
        }
18
    }
19
20
    public static function create($point_id, $label = null)
21
    {
22
        return new self($point_id, $label);
23
    }
24
25
    public static function checkPoint($point)
26
    {
27
        if ($point instanceof self) {
28
            return TRUE;
29
        } else {
30
            return FALSE;
31
        }
32
    }
33
34
    public static function validate($point)
35
    {
36
        if (is_int($point)) {
37
            return $point;
38
        } elseif (self::checkPoint($point)) {
39
            return $point->getID();
40
        } else {
41
            throw new PointException('Wrong data sent');
42
        }
43
    }
44
45
    public function addRelation($point, $distance)
46
    {
47
        $point = $this::validate($point);
48
49
        $this->distances[$point] = $distance;
50
51
        return $this;
52
    }
53
54
    public function getDinstances()
55
    {
56
        $distances = array();
57
58
        foreach ($this->distances as $point_id => $distance) {
59
            $distances[] = [$point_id, $distance];
60
        }
61
62
        return $distances;
63
    }
64
65
    public function distanceTo($point)
66
    {
67
        $point = $this::validate($point);
68
69
        if (isset($this->distances[$point])) {
70
            return $this->distances[$point];
71
        } else {
72
            return FALSE;
73
        }
74
    }
75
76
    public function getID()
77
    {
78
        return $this->id;
79
    }
80
81
    public function getLabel()
82
    {
83
        return $this->label;
84
    }
85
}
86