Completed
Push — master ( 8528dc...77ea3d )
by Ventaquil
07:05
created

Point::toArray()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 20
rs 9.2
cc 4
eloc 10
nc 8
nop 0
1
<?php
2
namespace Algorithms\GraphTools;
3
4
class Point
5
{
6
    private $_id;
7
    private $distances = array();
8
    private $label = null;
9
    private $x;
10
    private $y;
11
12
    public function __construct($pointId, $label = null)
13
    {
14
        if (filter_var($pointId, FILTER_VALIDATE_INT) !== false) {
15
            $this->_id = $pointId;
16
            $this->label = $label;
17
        } else {
18
            throw new PointException('Wrong data sent');
19
        }
20
    }
21
22
    public static function create($pointId, $label = null)
23
    {
24
        return new self($pointId, $label);
25
    }
26
27
    public static function checkPoint($point)
28
    {
29
        return ($point instanceof self);
30
    }
31
32
    public static function validate($point)
33
    {
34
        if (filter_var($point, FILTER_VALIDATE_INT) !== false) {
35
            return $point;
36
        } elseif (self::checkPoint($point)) {
37
            return $point->getID();
38
        } else {
39
            throw new PointException('Wrong data sent');
40
        }
41
    }
42
43
    public function addRelation($point, $distance)
44
    {
45
        $point = $this::validate($point);
46
47
        $this->distances[$point] = $distance;
48
49
        return $this;
50
    }
51
52
    public function getDinstances()
53
    {
54
        $distances = array();
55
56
        foreach ($this->distances as $pointId => $distance) {
57
            $distances[] = [$pointId, $distance];
58
        }
59
60
        return $distances;
61
    }
62
63
    public function distanceTo($point)
64
    {
65
        $point = $this::validate($point);
66
67
        return (isset($this->distances[$point])) ? $this->distances[$point] : false;
68
    }
69
70
    public function getID()
71
    {
72
        return $this->_id;
73
    }
74
75
    public function getLabel()
76
    {
77
        return $this->label;
78
    }
79
80
    public function setX($x)
81
    {
82
        $this->x = intval($x);
83
        return $this;
84
    }
85
86
    public function setY($y)
87
    {
88
        $this->y = intval($y);
89
        return $this;
90
    }
91
92
    public function getX()
93
    {
94
        return $this->x;
95
    }
96
97
    public function getY()
98
    {
99
        return $this->y;
100
    }
101
102
    public function toArray()
103
    {
104
        $array = array(
105
            'id' => $this->_id
106
        );
107
108
        if (!empty($this->label)) {
109
            $array['label'] = $this->label;
110
        }
111
112
        if (!empty($this->x)) {
113
            $array['x'] = $this->x;
114
        }
115
116
        if (!empty($this->y)) {
117
            $array['y'] = $this->y;
118
        }
119
120
        return $array;
121
    }
122
}
123