1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare( strict_types = 1 ); |
4
|
|
|
|
5
|
|
|
namespace DataValues\Geo\Parsers; |
6
|
|
|
|
7
|
|
|
use DataValues\Geo\PackagePrivate\LatLongPrecisionParser; |
8
|
|
|
use DataValues\Geo\PackagePrivate\Precision; |
9
|
|
|
use DataValues\Geo\Values\GlobeCoordinateValue; |
10
|
|
|
use ValueParsers\ParseException; |
11
|
|
|
use ValueParsers\ParserOptions; |
12
|
|
|
use ValueParsers\ValueParser; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Extends the LatLongParser by adding precision detection support. |
16
|
|
|
* |
17
|
|
|
* The object that gets constructed is a GlobeCoordinateValue rather then a LatLongValue. |
18
|
|
|
* |
19
|
|
|
* @since 0.1 |
20
|
|
|
* |
21
|
|
|
* @license GPL-2.0-or-later |
22
|
|
|
* @author Jeroen De Dauw < [email protected] > |
23
|
|
|
* @author H. Snater < [email protected] > |
24
|
|
|
* @author Thiemo Kreuz |
25
|
|
|
*/ |
26
|
|
|
class GlobeCoordinateParser implements ValueParser { |
27
|
|
|
|
28
|
|
|
public const FORMAT_NAME = 'globe-coordinate'; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Option specifying the globe. Should be a string containing a Wikidata concept URI. |
32
|
|
|
* Defaults to Earth. |
33
|
|
|
*/ |
34
|
|
|
public const OPT_GLOBE = 'globe'; |
35
|
|
|
|
36
|
|
|
private $options; |
37
|
|
|
private $latLongPrecisionParser; |
38
|
|
|
|
39
|
119 |
|
public function __construct( ParserOptions $options = null ) { |
40
|
119 |
|
$this->options = $options ?: new ParserOptions(); |
41
|
|
|
|
42
|
119 |
|
$this->options->defaultOption( ValueParser::OPT_LANG, 'en' ); |
43
|
119 |
|
$this->options->defaultOption( self::OPT_GLOBE, 'http://www.wikidata.org/entity/Q2' ); |
44
|
119 |
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @see StringValueParser::stringParse |
48
|
|
|
* |
49
|
|
|
* @param string $value |
50
|
|
|
* |
51
|
|
|
* @throws ParseException |
52
|
|
|
* @return GlobeCoordinateValue |
53
|
|
|
*/ |
54
|
119 |
|
public function parse( $value ): GlobeCoordinateValue { |
55
|
119 |
|
$parser = $this->getParser(); |
56
|
|
|
|
57
|
|
|
try { |
58
|
119 |
|
$latLongPrecision = $parser->parse( $value ); |
59
|
2 |
|
} catch ( \Exception $ex ) { |
60
|
2 |
|
throw new ParseException( |
61
|
2 |
|
'The format of the coordinate could not be determined.', |
62
|
|
|
$value, |
63
|
2 |
|
self::FORMAT_NAME |
64
|
|
|
); |
65
|
|
|
} |
66
|
|
|
|
67
|
117 |
|
return new GlobeCoordinateValue( |
68
|
117 |
|
$latLongPrecision->getLatLong(), |
69
|
117 |
|
$this->getPrecision( $latLongPrecision->getPrecision() ), |
70
|
117 |
|
$this->options->getOption( self::OPT_GLOBE ) |
71
|
|
|
); |
72
|
|
|
} |
73
|
|
|
|
74
|
119 |
|
private function getParser() { |
75
|
119 |
|
if ( $this->latLongPrecisionParser === null ) { |
76
|
119 |
|
$this->latLongPrecisionParser = new LatLongPrecisionParser( $this->options ); |
77
|
|
|
} |
78
|
|
|
|
79
|
119 |
|
return $this->latLongPrecisionParser; |
80
|
|
|
} |
81
|
|
|
|
82
|
117 |
|
private function getPrecision( Precision $detectedPrecision ): float { |
83
|
117 |
|
if ( $this->options->hasOption( 'precision' ) ) { |
84
|
|
|
return $this->options->getOption( 'precision' ); |
85
|
|
|
} |
86
|
|
|
|
87
|
117 |
|
return $detectedPrecision->toFloat(); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
} |
91
|
|
|
|