1 | <?php |
||
14 | class Coordinate implements ValueObjectInterface |
||
15 | { |
||
16 | /** @var Latitude */ |
||
17 | protected $latitude; |
||
18 | |||
19 | /** @var Longitude */ |
||
20 | protected $longitude; |
||
21 | |||
22 | /** @var Ellipsoid */ |
||
23 | protected $ellipsoid; |
||
24 | |||
25 | /** |
||
26 | * Returns a new Coordinate object from native PHP arguments |
||
27 | * |
||
28 | * @return self |
||
29 | * @throws \BadMethodCallException |
||
30 | */ |
||
31 | 2 | public static function fromNative() |
|
48 | |||
49 | /** |
||
50 | * Returns a new Coordinate object |
||
51 | * |
||
52 | * @param Latitude $latitude |
||
53 | * @param Longitude $longitude |
||
54 | * @param Ellipsoid $ellipsoid |
||
55 | */ |
||
56 | 9 | public function __construct(Latitude $latitude, Longitude $longitude, Ellipsoid $ellipsoid = null) |
|
57 | { |
||
58 | 9 | if (null === $ellipsoid) { |
|
59 | 9 | $ellipsoid = Ellipsoid::WGS84(); |
|
60 | } |
||
61 | |||
62 | 9 | $this->latitude = $latitude; |
|
63 | 9 | $this->longitude = $longitude; |
|
64 | 9 | $this->ellipsoid = $ellipsoid; |
|
65 | 9 | } |
|
66 | |||
67 | /** |
||
68 | * Tells whether tow Coordinate objects are equal |
||
69 | * |
||
70 | * @param ValueObjectInterface $coordinate |
||
71 | * @return bool |
||
72 | */ |
||
73 | 2 | public function sameValueAs(ValueObjectInterface $coordinate) |
|
74 | { |
||
75 | 2 | if (false === Util::classEquals($this, $coordinate)) { |
|
76 | 1 | return false; |
|
77 | } |
||
78 | |||
79 | 2 | return $this->getLatitude()->sameValueAs($coordinate->getLatitude()) && |
|
|
|||
80 | 2 | $this->getLongitude()->sameValueAs($coordinate->getLongitude()) && |
|
81 | 2 | $this->getEllipsoid()->sameValueAs($coordinate->getEllipsoid()) |
|
82 | ; |
||
83 | } |
||
84 | |||
85 | /** |
||
86 | * Returns latitude |
||
87 | * |
||
88 | * @return Latitude |
||
89 | */ |
||
90 | 7 | public function getLatitude() |
|
94 | |||
95 | /** |
||
96 | * Returns longitude |
||
97 | * |
||
98 | * @return Longitude |
||
99 | */ |
||
100 | 7 | public function getLongitude() |
|
104 | |||
105 | /** |
||
106 | * Returns ellipsoid |
||
107 | * |
||
108 | * @return Ellipsoid |
||
109 | */ |
||
110 | 7 | public function getEllipsoid() |
|
114 | |||
115 | /** |
||
116 | * Returns a degrees/minutes/seconds representation of the coordinate |
||
117 | * |
||
118 | * @return String |
||
119 | */ |
||
120 | 1 | public function toDegreesMinutesSeconds() |
|
121 | { |
||
122 | 1 | $coordinate = self::getBaseCoordinate($this); |
|
123 | 1 | $convert = new Convert($coordinate); |
|
124 | 1 | $dms = $convert->toDegreesMinutesSeconds(); |
|
125 | |||
126 | 1 | return new StringLiteral($dms); |
|
127 | } |
||
128 | |||
129 | /** |
||
130 | * Returns a decimal minutes representation of the coordinate |
||
131 | * |
||
132 | * @return String |
||
133 | */ |
||
134 | 1 | public function toDecimalMinutes() |
|
135 | { |
||
136 | 1 | $coordinate = self::getBaseCoordinate($this); |
|
137 | 1 | $convert = new Convert($coordinate); |
|
138 | 1 | $dm = $convert->toDecimalMinutes(); |
|
139 | |||
140 | 1 | return new StringLiteral($dm); |
|
141 | } |
||
142 | |||
143 | /** |
||
144 | * Returns a Universal Transverse Mercator projection representation of the coordinate in meters |
||
145 | * |
||
146 | * @return String |
||
147 | */ |
||
148 | 1 | public function toUniversalTransverseMercator() |
|
149 | { |
||
150 | 1 | $coordinate = self::getBaseCoordinate($this); |
|
151 | 1 | $convert = new Convert($coordinate); |
|
152 | 1 | $utm = $convert->toUniversalTransverseMercator(); |
|
153 | |||
154 | 1 | return new StringLiteral($utm); |
|
155 | } |
||
156 | |||
157 | /** |
||
158 | * Calculates the distance between two Coordinate objects |
||
159 | * |
||
160 | * @param Coordinate $coordinate |
||
161 | * @param DistanceUnit $unit |
||
162 | * @param DistanceFormula $formula |
||
163 | * @return Real |
||
164 | */ |
||
165 | 1 | public function distanceFrom(Coordinate $coordinate, DistanceUnit $unit = null, DistanceFormula $formula = null) |
|
166 | { |
||
167 | 1 | if (null === $unit) { |
|
168 | 1 | $unit = DistanceUnit::METER(); |
|
169 | } |
||
170 | |||
171 | 1 | if (null === $formula) { |
|
172 | 1 | $formula = DistanceFormula::FLAT(); |
|
173 | } |
||
174 | |||
175 | 1 | $baseThis = self::getBaseCoordinate($this); |
|
176 | 1 | $baseCoordinate = self::getBaseCoordinate($coordinate); |
|
177 | |||
178 | 1 | $distance = new Distance(); |
|
179 | $distance |
||
180 | 1 | ->setFrom($baseThis) |
|
181 | 1 | ->setTo($baseCoordinate) |
|
182 | 1 | ->in($unit->toNative()) |
|
183 | ; |
||
184 | |||
185 | 1 | $value = \call_user_func(array($distance, $formula->toNative())); |
|
186 | |||
187 | 1 | return new Real($value); |
|
188 | } |
||
189 | |||
190 | /** |
||
191 | * Returns a native string version of the Coordiantes object in format "$latitude,$longitude" |
||
192 | * |
||
193 | * @return string |
||
194 | */ |
||
195 | 1 | public function __toString() |
|
199 | |||
200 | /** |
||
201 | * Returns the underlying Coordinate object |
||
202 | * |
||
203 | * @param self $coordinate |
||
204 | * @return BaseCoordinate |
||
205 | */ |
||
206 | 4 | protected static function getBaseCoordinate(self $coordinate) |
|
215 | |||
216 | function jsonSerialize() |
||
217 | { |
||
218 | return [ |
||
224 | |||
225 | |||
226 | } |
||
227 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: