1 | <?php |
||
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 | 122 | public function __construct( ParserOptions $options = null ) { |
|
43 | |||
44 | /** |
||
45 | * @see StringValueParser::stringParse |
||
46 | * |
||
47 | * @param string $value |
||
48 | * |
||
49 | * @throws ParseException |
||
50 | * @return GlobeCoordinateValue |
||
51 | */ |
||
52 | 122 | public function parse( $value ): GlobeCoordinateValue { |
|
53 | 122 | foreach ( $this->getParsers() as $precisionDetector => $parser ) { |
|
54 | try { |
||
55 | 122 | $latLong = $parser->parse( $value ); |
|
56 | |||
57 | 117 | return new GlobeCoordinateValue( |
|
58 | 117 | new LatLongValue( |
|
59 | 117 | $latLong->getLatitude(), |
|
60 | 117 | $latLong->getLongitude() |
|
61 | ), |
||
62 | 117 | $this->detectPrecision( $latLong, $precisionDetector ), |
|
63 | 117 | $this->options->getOption( self::OPT_GLOBE ) |
|
64 | ); |
||
65 | 89 | } catch ( ParseException $parseException ) { |
|
66 | 89 | continue; |
|
67 | } |
||
68 | } |
||
69 | |||
70 | 5 | throw new ParseException( |
|
71 | 5 | 'The format of the coordinate could not be determined.', |
|
72 | $value, |
||
73 | 5 | self::FORMAT_NAME |
|
74 | ); |
||
75 | } |
||
76 | |||
77 | 117 | private function detectPrecision( LatLongValue $latLong, string $precisionDetector ): float { |
|
87 | |||
88 | /** |
||
89 | * @return ValueParser[] |
||
90 | */ |
||
91 | 122 | private function getParsers(): array { |
|
101 | |||
102 | 29 | private function detectDdPrecision( float $degree ): float { |
|
105 | |||
106 | 25 | private function detectDmPrecision( float $degree ): float { |
|
116 | |||
117 | 42 | private function detectDmsPrecision( float $degree ): float { |
|
127 | |||
128 | 62 | private function detectFloatPrecision( float $degree ): float { |
|
137 | |||
138 | } |
||
139 |