forked from DataValues/Geo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLatLongPrecisionParser.php
More file actions
58 lines (47 loc) · 1.45 KB
/
LatLongPrecisionParser.php
File metadata and controls
58 lines (47 loc) · 1.45 KB
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
<?php
namespace DataValues\Geo\PackagePrivate;
use DataValues\Geo\Parsers\DdCoordinateParser;
use DataValues\Geo\Parsers\DmCoordinateParser;
use DataValues\Geo\Parsers\DmsCoordinateParser;
use DataValues\Geo\Parsers\FloatCoordinateParser;
use ValueParsers\ParseException;
use ValueParsers\ParserOptions;
/**
* @api
*/
class LatLongPrecisionParser {
private ?array $parsers = null;
public function __construct( private ?ParserOptions $options = null ) {
}
public function parse( string $coordinate ): PreciseLatLong {
foreach ( $this->getParsers() as $parser ) {
try {
$latLongPrecision = $parser->parse( $coordinate );
} catch ( ParseException ) {
continue;
}
return $latLongPrecision;
}
throw new ParseException(
'The format of the coordinate could not be determined.',
$coordinate
);
}
/**
* @return PrecisionParser[]
*/
private function getParsers(): iterable {
if ( $this->parsers === null ) {
$this->parsers = $this->getNewParsers();
}
return $this->parsers;
}
private function getNewParsers(): array {
return [
new PrecisionParser( new FloatCoordinateParser( $this->options ), new FloatPrecisionDetector() ),
new PrecisionParser( new DmsCoordinateParser( $this->options ), new DmsPrecisionDetector() ),
new PrecisionParser( new DmCoordinateParser( $this->options ), new DmPrecisionDetector() ),
new PrecisionParser( new DdCoordinateParser( $this->options ), new FloatPrecisionDetector() )
];
}
}