-
Notifications
You must be signed in to change notification settings - Fork 17
/
AbstractPoint.php
410 lines (353 loc) · 12.3 KB
/
AbstractPoint.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
<?php
/**
* This file is part of the doctrine spatial extension.
*
* PHP 8.1 | 8.2 | 8.3
* Doctrine ORM 2.19 | 3.1
*
* Copyright Alexandre Tranchant <alexandre.tranchant@gmail.com> 2017-2024
* Copyright Longitude One 2020-2024
* Copyright 2015 Derek J. Lambert
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
*/
declare(strict_types=1);
namespace LongitudeOne\Spatial\PHP\Types;
use Doctrine\Deprecations\Deprecation;
use LongitudeOne\Geo\String\Exception\RangeException as GeoParserRangeException;
use LongitudeOne\Geo\String\Exception\UnexpectedValueException;
use LongitudeOne\Geo\String\Parser;
use LongitudeOne\Spatial\Exception\InvalidValueException;
use LongitudeOne\Spatial\Exception\RangeException;
/**
* Abstract point object for POINT spatial types.
*
* @see https://stackoverflow.com/questions/7309121/preferred-order-of-writing-latitude-longitude-tuples
* @see https://docs.geotools.org/latest/userguide/library/referencing/order.html
*/
abstract class AbstractPoint extends AbstractGeometry implements PointInterface
{
private const LINK = 'https://github.com/longitude-one/doctrine-spatial/issues/81';
/**
* The X coordinate or the longitude.
*/
protected float|int $x;
/**
* The Y coordinate or the latitude.
*/
protected float|int $y;
/**
* AbstractPoint constructor.
*
* @throws InvalidValueException when point is invalid
*/
public function __construct()
{
$argv = $this->validateArguments(func_get_args(), '__construct');
call_user_func_array([$this, 'construct'], $argv);
}
/**
* This method triggers deprecation messages when developers don't use the next gen constructors.
*
* @param mixed[] $argv arguments passed to the constructor or the calling method
* @param string $caller the calling method
*/
private static function triggerEventualDeprecations(array $argv, string $caller): void
{
$argc = count($argv);
// Array cases
if (1 === $argc && is_array($argv[0])) {
Deprecation::trigger(
'longitude-one/doctrine-spatial',
self::LINK,
'Passing an array of coordinates on %s::%s is deprecated since 5.0.2. Please use two arguments instead.',
static::class,
$caller
);
return;
}
if (2 === $argc && is_array($argv[0]) && is_numeric($argv[1])) {
Deprecation::trigger(
'longitude-one/doctrine-spatial',
self::LINK,
'Passing an array of coordinates and a SRID on %s::%s is deprecated since 5.0.2. Please use three arguments instead.',
static::class,
$caller
);
}
}
/**
* Latitude getter.
*/
public function getLatitude(): float|int
{
return $this->getY();
}
/**
* Longitude getter.
*/
public function getLongitude(): float|int
{
return $this->getX();
}
/**
* Type getter.
*/
public function getType(): string
{
return self::POINT;
}
/**
* X getter. (Longitude getter).
*/
public function getX(): float|int
{
return $this->x;
}
/**
* Y getter. Latitude getter.
*/
public function getY(): float|int
{
return $this->y;
}
/**
* Latitude fluent setter.
*
* @param float|int|string $latitude the new latitude of point
*
* @throws InvalidValueException when latitude is not valid
*/
public function setLatitude(float|int|string $latitude): static
{
try {
$geodesicCoordinate = $this->setGeodesicCoordinate($latitude, -90, 90);
} catch (RangeException $e) {
throw new InvalidValueException(sprintf(InvalidValueException::OUT_OF_RANGE_LATITUDE, $latitude), $e->getCode(), $e);
}
$this->y = $geodesicCoordinate;
return $this;
}
/**
* Longitude setter.
*
* @param float|int|string $longitude the new longitude
*
* @throws InvalidValueException when longitude is not valid
*/
public function setLongitude(float|int|string $longitude): static
{
try {
$geodesicCoordinate = $this->setGeodesicCoordinate($longitude, -180, 180);
} catch (RangeException $e) {
throw new InvalidValueException(sprintf(InvalidValueException::OUT_OF_RANGE_LONGITUDE, $longitude), $e->getCode(), $e);
}
$this->x = $geodesicCoordinate;
return $this;
}
/**
* X setter. (Latitude setter).
*
* @param float|int|string $x the new X
*
* @throws InvalidValueException when x is not valid
*/
public function setX(float|int|string $x): static
{
$this->x = $this->setCartesianCoordinate($x);
return $this;
}
/**
* Y setter. Longitude Setter.
*
* @param float|int|string $y the new Y value
*
* @throws InvalidValueException when Y is invalid, not in valid range
*/
public function setY(float|int|string $y): static
{
$this->y = $this->setCartesianCoordinate($y);
return $this;
}
/**
* Convert point into an array X, Y.
* Latitude, longitude.
*
* @return array{0 : float|int, 1 : float|int}
*/
public function toArray(): array
{
return [$this->x, $this->y];
}
/**
* Validate arguments.
*
* @param mixed[] $argv list of arguments
* @param string $caller the calling method
*
* @return (float|int|string)[]
*
* @throws InvalidValueException when an argument is not valid
*/
protected function validateArguments(array $argv, string $caller): array
{
self::triggerEventualDeprecations($argv, $caller);
$argc = count($argv);
if (1 == $argc && is_array($argv[0])) {
$count = count($argv[0]);
if ($count < 2 || $count > 3) {
throw $this->createException($argv[0], $caller, true);
}
foreach ($argv[0] as $value) {
if (is_numeric($value) || is_string($value)) {
continue;
}
throw $this->createException($argv[0], $caller, true);
}
return $argv[0];
}
if (2 == $argc) {
if (is_array($argv[0]) && (is_numeric($argv[1]) || null === $argv[1] || is_string($argv[1]))) {
$argv[0][] = $argv[1];
return $argv[0];
}
if ((is_numeric($argv[0]) || is_string($argv[0])) && (is_numeric($argv[1]) || is_string($argv[1]))) {
return $argv;
}
}
if (3 == $argc) {
if ((is_numeric($argv[0]) || is_string($argv[0]))
&& (is_numeric($argv[1]) || is_string($argv[1]))
&& (is_numeric($argv[2]) || null === $argv[2] || is_string($argv[2]))
) {
return $argv;
}
}
throw $this->createException($argv, $caller);
}
/**
* Check the range of a coordinate.
*
* @param float|int $coordinate the coordinate to check
* @param int $min the minimum accepted value
* @param int $max the maximum accepted value
*
* @return float|int $coordinate or throw a RangeException
*
* @throws RangeException when coordinate is out of range fixed by min and max
*/
private function checkRange(float|int $coordinate, int $min, int $max): float|int
{
if ($coordinate < $min || $coordinate > $max) {
throw new RangeException(sprintf('Coordinate must be comprised between %d and %d, got "%s".', $min, $max, $coordinate));
}
return $coordinate;
}
/**
* Create a fluent message for InvalidException.
*
* @param mixed[] $argv the arguments
* @param string $caller the method calling the method calling exception :)
* @param bool $subArray when the first argument was a subarray converted into an array
*/
private function createException(array $argv, string $caller, bool $subArray = false): InvalidValueException
{
array_walk($argv, function (&$value) {
if (is_numeric($value) || is_string($value)) {
return;
}
$value = gettype($value);
});
$message = 'Invalid parameters passed to %s::%s: %s';
if ($subArray) {
$message = 'Invalid parameters passed to %s::%s: array(%s)';
}
return new InvalidValueException(sprintf(
$message,
static::class,
$caller,
implode(', ', $argv)
));
}
/**
* Use the longitude-one/geo-parser to parse a coordinate.
*
* @param string $coordinate the coordinate to parse
*
* @throws InvalidValueException when coordinate is invalid
*/
private function geoParse(string $coordinate): float|int
{
try {
$parser = new Parser($coordinate);
$parsedCoordinate = $parser->parse();
} catch (GeoParserRangeException $e) {
$message = match ($e->getCode()) {
GeoParserRangeException::LATITUDE_OUT_OF_RANGE => sprintf(InvalidValueException::OUT_OF_RANGE_LATITUDE, $coordinate),
GeoParserRangeException::LONGITUDE_OUT_OF_RANGE => sprintf(InvalidValueException::OUT_OF_RANGE_LONGITUDE, $coordinate),
GeoParserRangeException::MINUTES_OUT_OF_RANGE => sprintf(InvalidValueException::OUT_OF_RANGE_MINUTE, $coordinate),
GeoParserRangeException::SECONDS_OUT_OF_RANGE => sprintf(InvalidValueException::OUT_OF_RANGE_SECOND, $coordinate),
default => $e->getMessage(),
};
throw new InvalidValueException($message, $e->getCode(), $e);
} catch (UnexpectedValueException $e) {
throw new InvalidValueException(sprintf('Invalid coordinate value, got "%s".', $coordinate), $e->getCode(), $e);
}
if (is_array($parsedCoordinate)) {
throw new InvalidValueException('Invalid coordinate value, coordinate cannot be an array.');
}
return $parsedCoordinate;
}
/**
* Set a cartesian coordinate.
* Abscissa or ordinate.
*
* @param float|int|string $coordinate the coordinate to set
*
* @throws InvalidValueException when coordinate is invalid, RangeException is never thrown
*/
private function setCartesianCoordinate(float|int|string $coordinate): float|int
{
if (is_integer($coordinate) || is_float($coordinate)) {
// We don't check the range of the value.
return $coordinate;
}
// $y is a string, let's use the geo-parser.
return $this->geoParse($coordinate);
}
/**
* Set a geodesic coordinate.
* Latitude or longitude.
*
* @param float|int|string $coordinate the coordinate to set
* @param int $min the minimum value
* @param int $max the maximum value
*
* @throws InvalidValueException|RangeException when coordinate is invalid or out of range
*/
private function setGeodesicCoordinate(float|int|string $coordinate, int $min, int $max): float|int
{
if (is_integer($coordinate) || is_float($coordinate)) {
// We check the range of the value.
return $this->checkRange($coordinate, $min, $max);
}
// $y is a string, let's use the geo-parser.
$parsedCoordinate = $this->geoParse($coordinate);
if ($parsedCoordinate < $min || $parsedCoordinate > $max) {
throw new RangeException(sprintf('Coordinate must be comprised between %d and %d, got "%s".', $min, $max, $coordinate));
}
return $parsedCoordinate;
}
/**
* Abstract point internal constructor.
*
* @param string $x X, longitude
* @param string $y Y, latitude
* @param null|int $srid Spatial Reference System Identifier
*
* @throws InvalidValueException if x or y are invalid
*/
abstract protected function construct(string $x, string $y, ?int $srid = null): void;
}