|
1
|
|
|
<?php |
|
2
|
|
|
namespace Algorithms\GraphTools; |
|
3
|
|
|
|
|
4
|
|
|
class Point |
|
5
|
|
|
{ |
|
6
|
|
|
protected $id, |
|
|
|
|
|
|
7
|
|
|
$x, |
|
8
|
|
|
$y, |
|
9
|
|
|
$label = null; |
|
10
|
|
|
|
|
11
|
|
|
public function __construct($id, $label = null) |
|
12
|
|
|
{ |
|
13
|
|
|
if (!(filter_var($id, FILTER_VALIDATE_INT) || is_numeric($value))) { |
|
|
|
|
|
|
14
|
|
|
throw new PointException('ID is must be a number'); |
|
15
|
|
|
} |
|
16
|
|
|
|
|
17
|
|
|
$this->id = $id; |
|
18
|
|
|
$this->label = $label; |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
|
|
public static function create($id, $label = null) |
|
22
|
|
|
{ |
|
23
|
|
|
return new self($id, $label); |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
public static function check($point) |
|
27
|
|
|
{ |
|
28
|
|
|
return ($point instanceof self); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
public static function checkOrFail($point) |
|
32
|
|
|
{ |
|
33
|
|
|
if (!Point::check($point)) { |
|
34
|
|
|
throw new PointException('Sent object is not a Point'); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
return true; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
public function __get($name) |
|
41
|
|
|
{ |
|
42
|
|
|
return $this->{$name}; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
public function __set($name, $value) |
|
46
|
|
|
{ |
|
47
|
|
|
if (in_array($name, array('x', 'y'))) { |
|
48
|
|
|
if (!(filter_var($value, FILTER_VALIDATE_INT) || is_numeric($value))) { |
|
49
|
|
|
throw new PointException("Parameter {$value} must be a number"); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
$this->{$name} = $value; |
|
53
|
|
|
} elseif ($name == 'id') { |
|
54
|
|
|
throw new PointException('You can\'t change point ID'); |
|
55
|
|
|
} else { |
|
56
|
|
|
$this->{$name} = $value; |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
public function toArray() |
|
61
|
|
|
{ |
|
62
|
|
|
$point = array( |
|
63
|
|
|
'id' => $this->id, |
|
64
|
|
|
); |
|
65
|
|
|
|
|
66
|
|
|
foreach (array('label', 'x', 'y') as $name) { |
|
67
|
|
|
if (!is_null($this->{$name})) { |
|
68
|
|
|
$point[$name] = $this->{$name}; |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
return $point; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
public function addConnection(Point $point, $distance) |
|
76
|
|
|
{ |
|
77
|
|
|
return new ConnectionsContainer( |
|
78
|
|
|
array( |
|
79
|
|
|
new Connection($this, $point, $distance), |
|
80
|
|
|
) |
|
81
|
|
|
); |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|
Only declaring a single property per statement allows you to later on add doc comments more easily.
It is also recommended by PSR2, so it is a common style that many people expect.