1 | <?php |
||
23 | class GlobeCoordinateParser extends StringValueParser { |
||
24 | |||
25 | const FORMAT_NAME = 'globe-coordinate'; |
||
26 | |||
27 | /** |
||
28 | * Option specifying the globe. Should be a string containing a Wikidata concept URI. Defaults |
||
29 | * to Earth. |
||
30 | */ |
||
31 | const OPT_GLOBE = 'globe'; |
||
32 | |||
33 | /** |
||
34 | * @param ParserOptions|null $options |
||
35 | */ |
||
36 | 126 | public function __construct( ParserOptions $options = null ) { |
|
41 | |||
42 | /** |
||
43 | * @see StringValueParser::stringParse |
||
44 | * |
||
45 | * @param string $value |
||
46 | * |
||
47 | * @return GlobeCoordinateValue |
||
48 | * @throws ParseException |
||
49 | */ |
||
50 | 119 | protected function stringParse( $value ) { |
|
51 | 119 | foreach ( $this->getParsers() as $precisionDetector => $parser ) { |
|
52 | try { |
||
53 | 119 | $latLong = $parser->parse( $value ); |
|
54 | |||
55 | 117 | return new GlobeCoordinateValue( |
|
56 | 117 | new LatLongValue( |
|
57 | 117 | $latLong->getLatitude(), |
|
58 | 117 | $latLong->getLongitude() |
|
59 | ), |
||
60 | 117 | $this->detectPrecision( $latLong, $precisionDetector ), |
|
61 | 117 | $this->getOption( self::OPT_GLOBE ) |
|
62 | ); |
||
63 | 86 | } catch ( ParseException $parseException ) { |
|
64 | 86 | continue; |
|
65 | } |
||
66 | } |
||
67 | |||
68 | 2 | throw new ParseException( |
|
69 | 2 | 'The format of the coordinate could not be determined.', |
|
70 | 2 | $value, |
|
71 | 2 | self::FORMAT_NAME |
|
72 | ); |
||
73 | } |
||
74 | |||
75 | /** |
||
76 | * @param LatLongValue $latLong |
||
77 | * @param string $precisionDetector |
||
78 | * |
||
79 | * @return float|int |
||
80 | */ |
||
81 | 117 | private function detectPrecision( LatLongValue $latLong, $precisionDetector ) { |
|
91 | |||
92 | /** |
||
93 | * @return StringValueParser[] |
||
94 | */ |
||
95 | 119 | private function getParsers() { |
|
105 | |||
106 | /** |
||
107 | * @param float $degree |
||
108 | * |
||
109 | * @return float|int |
||
110 | */ |
||
111 | 29 | protected function detectDdPrecision( $degree ) { |
|
114 | |||
115 | /** |
||
116 | * @param float $degree |
||
117 | * |
||
118 | * @return float|int |
||
119 | */ |
||
120 | 25 | protected function detectDmPrecision( $degree ) { |
|
130 | |||
131 | /** |
||
132 | * @param float $degree |
||
133 | * |
||
134 | * @return float|int |
||
135 | */ |
||
136 | 42 | protected function detectDmsPrecision( $degree ) { |
|
146 | |||
147 | /** |
||
148 | * @param float $degree |
||
149 | * |
||
150 | * @return float|int |
||
151 | */ |
||
152 | 62 | protected function detectFloatPrecision( $degree ) { |
|
161 | |||
162 | } |
||
163 |