Completed
Push — master ( 61ea44...8528dc )
by Ventaquil
10:30
created

Point   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 18
c 4
b 0
f 0
lcom 1
cbo 1
dl 0
loc 96
rs 10

13 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 2
A create() 0 4 1
A checkPoint() 0 4 1
A validate() 0 10 3
A addRelation() 0 8 1
A getDinstances() 0 10 2
A distanceTo() 0 6 2
A getID() 0 4 1
A getLabel() 0 4 1
A setX() 0 4 1
A setY() 0 4 1
A getX() 0 4 1
A getY() 0 4 1
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
    }
84
85
    public function setY($y)
86
    {
87
        $this->y = intval($y);
88
    }
89
90
    public function getX()
91
    {
92
        return $this->x;
93
    }
94
95
    public function getY()
96
    {
97
        return $this->y;
98
    }
99
}
100