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