1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @author ventaquil <[email protected]> |
5
|
|
|
* @licence MIT |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace PHPAlgorithms\Dijkstra; |
9
|
|
|
|
10
|
|
|
use PHPAlgorithms\Dijkstra\Exceptions\PointException; |
11
|
|
|
use PHPAlgorithms\GraphTools\AbstractPoint; |
12
|
|
|
use PHPAlgorithms\GraphTools\Traits\MagicGet; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class Point |
16
|
|
|
* @package PHPAlgorithms\Dijkstra |
17
|
|
|
* @property string $label |
18
|
|
|
* @property Relation[] $relations |
19
|
|
|
*/ |
20
|
|
|
class Point extends AbstractPoint |
21
|
|
|
{ |
22
|
|
|
use MagicGet; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var integer|null $id Point identification number. |
26
|
|
|
* @var string $label Point label, defaults empty string. |
27
|
|
|
* @var Relation[] $relations Relations array, default is empty. |
28
|
|
|
*/ |
29
|
|
|
public $id; |
30
|
|
|
private $label = ''; |
31
|
|
|
private $relations = array(); |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Point constructor. |
35
|
|
|
* |
36
|
|
|
* @param string|null $label Point's label. Defaults null. |
37
|
|
|
*/ |
38
|
|
|
public function __construct($label = null) |
39
|
|
|
{ |
40
|
|
|
if (!is_null($label)) { |
41
|
|
|
$this->setLabel($label); |
42
|
|
|
} |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Magic method isset. |
47
|
|
|
* |
48
|
|
|
* @param string $name Name of parameter. |
49
|
|
|
* @return boolean Method returns true if parameter exists or false otherwise. |
50
|
|
|
*/ |
51
|
|
|
public function __isset($name) |
52
|
|
|
{ |
53
|
|
|
return isset($this->{$name}); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param Relation|Point Relation or Point object. |
58
|
|
|
* @param null|integer Distance to point, when first argument is a Point object. |
59
|
|
|
* @return Point $this Reference to the same object. |
60
|
|
|
* @throws PointException Throws exception when arguments aren't match. |
61
|
|
|
*/ |
62
|
|
|
public function addDoubleRelation() |
63
|
|
|
{ |
64
|
|
|
switch (func_num_args()) { |
65
|
|
|
case 1: |
66
|
|
|
$arg = func_get_arg(0); |
67
|
|
|
|
68
|
|
|
$this->addRelation($arg); |
69
|
|
|
if ($arg->from === $this) { |
70
|
|
|
$arg->to |
71
|
|
|
->addRelation($arg); |
72
|
|
|
} else { |
73
|
|
|
$arg->from |
74
|
|
|
->addRelation($arg); |
75
|
|
|
} |
76
|
|
|
break; |
77
|
|
|
case 2: |
78
|
|
|
$this->addRelation(func_get_arg(0), func_get_arg(1)); |
79
|
|
|
func_get_arg(0)->addRelation($this, func_get_arg(1)); |
80
|
|
|
break; |
81
|
|
|
default: |
82
|
|
|
throw new PointException('Wrong arguments sent to addRelation() method'); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return $this; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param Relation|Point Relation or Point object. |
90
|
|
|
* @param null|integer Distance to point, when first argument is a Point object. |
91
|
|
|
* @return Point $this Reference to the same object. |
92
|
|
|
* @throws PointException Throws exception when arguments aren't match. |
93
|
|
|
*/ |
94
|
|
|
public function addRelation() |
95
|
|
|
{ |
96
|
|
|
switch (func_num_args()) { |
97
|
|
|
case 1: |
98
|
|
|
$this->addRelationObject(func_get_arg(0)); |
99
|
|
|
break; |
100
|
|
|
case 2: |
101
|
|
|
$this->addRelationPointDistance(func_get_arg(0), func_get_arg(1)); |
102
|
|
|
break; |
103
|
|
|
default: |
104
|
|
|
throw new PointException('Wrong arguments sent to addRelation() method'); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
return $this; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @param Relation $relation |
112
|
|
|
* @throws PointException |
113
|
|
|
*/ |
114
|
|
|
private function addRelationObject(Relation $relation) |
115
|
|
|
{ |
116
|
|
|
if (($this !== $relation->from) && ($this !== $relation->to)) { |
|
|
|
|
117
|
|
|
throw new PointException('Sent relation is not match to this point'); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
$this->relations[] = $relation; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @param Point $point Another Point object to create a bidirectional relationship. |
125
|
|
|
* @param integer $distance Integer value of distance between points. Must be greater than 0. |
126
|
|
|
* @throws PointException |
127
|
|
|
*/ |
128
|
|
|
private function addRelationPointDistance(Point $point, $distance) |
129
|
|
|
{ |
130
|
|
|
$this->addRelationObject(new Relation($this, $point, $distance)); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @param string $label New Point's label. |
135
|
|
|
* @throws PointException Throws exception when $label is not a string. |
136
|
|
|
*/ |
137
|
|
|
public function setLabel($label) |
138
|
|
|
{ |
139
|
|
|
if (!is_string($label)) { |
140
|
|
|
throw new PointException('Label is not a string'); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
$this->label = $label; |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|
Since your code implements the magic setter
_set
, this function will be called for any write access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.Since the property has write access only, you can use the @property-write annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.