1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace DataValues\Geo\PackagePrivate; |
4
|
|
|
|
5
|
|
|
use DataValues\Geo\Parsers\DdCoordinateParser; |
6
|
|
|
use DataValues\Geo\Parsers\DmCoordinateParser; |
7
|
|
|
use DataValues\Geo\Parsers\DmsCoordinateParser; |
8
|
|
|
use DataValues\Geo\Parsers\FloatCoordinateParser; |
9
|
|
|
use ValueParsers\ParseException; |
10
|
|
|
use ValueParsers\ParserOptions; |
11
|
|
|
|
12
|
|
|
class LatLongPrecisionParser { |
13
|
|
|
|
14
|
|
|
private $options; |
15
|
|
|
private $parsers; |
16
|
|
|
|
17
|
|
|
public function __construct( ParserOptions $options = null ) { |
18
|
|
|
$this->options = $options; |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
public function parse( string $coordinate ): LatLongPrecision { |
22
|
|
|
foreach ( $this->getParsers() as $parser ) { |
23
|
|
|
try { |
24
|
|
|
$latLongPrecision = $parser->parse( $coordinate ); |
25
|
|
|
} catch ( ParseException $parseException ) { |
26
|
|
|
continue; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
return $latLongPrecision; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
throw new ParseException( |
33
|
|
|
'The format of the coordinate could not be determined.', |
34
|
|
|
$coordinate |
35
|
|
|
); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @return PrecisionParser[] |
40
|
|
|
*/ |
41
|
|
|
private function getParsers(): iterable { |
42
|
|
|
if ( $this->parsers === null ) { |
43
|
|
|
$this->parsers = new \CachingIterator( $this->getNewParsers(), \CachingIterator::FULL_CACHE ); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
return $this->parsers; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
private function getNewParsers(): \Generator { |
50
|
|
|
yield new PrecisionParser( new FloatCoordinateParser( $this->options ), new FloatPrecisionDetector() ); |
51
|
|
|
yield new PrecisionParser( new DmsCoordinateParser( $this->options ), new DmsPrecisionDetector() ); |
52
|
|
|
yield new PrecisionParser( new DmCoordinateParser( $this->options ), new DmPrecisionDetector() ); |
53
|
|
|
yield new PrecisionParser( new DdCoordinateParser( $this->options ), new FloatPrecisionDetector() ); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
} |
57
|
|
|
|