1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace DataValues\Geo\Parsers; |
4
|
|
|
|
5
|
|
|
use DataValues\Geo\Values\LatLongValue; |
6
|
|
|
use ValueParsers\ParseException; |
7
|
|
|
use ValueParsers\StringValueParser; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* ValueParser that parses the string representation of a geographical coordinate. |
11
|
|
|
* |
12
|
|
|
* The resulting objects are of type @see LatLongValue. |
13
|
|
|
* |
14
|
|
|
* Supports the following notations: |
15
|
|
|
* - Degree minute second |
16
|
|
|
* - Decimal degrees |
17
|
|
|
* - Decimal minutes |
18
|
|
|
* - Float |
19
|
|
|
* |
20
|
|
|
* And for all these notations direction can be indicated either with |
21
|
|
|
* + and - or with N/E/S/W, the later depending on the set options. |
22
|
|
|
* |
23
|
|
|
* The delimiter between latitude and longitude can be set in the options. |
24
|
|
|
* So can the symbols used for degrees, minutes and seconds. |
25
|
|
|
* |
26
|
|
|
* Some code in this class has been borrowed from the |
27
|
|
|
* MapsCoordinateParser class of the Maps extension for MediaWiki. |
28
|
|
|
* |
29
|
|
|
* @since 0.1, name changed in 2.0 |
30
|
|
|
* |
31
|
|
|
* @license GPL-2.0+ |
32
|
|
|
* @author Jeroen De Dauw < [email protected] > |
33
|
|
|
*/ |
34
|
|
|
class LatLongParser extends StringValueParser { |
35
|
|
|
|
36
|
|
|
const TYPE_FLOAT = 'float'; |
37
|
|
|
const TYPE_DMS = 'dms'; |
38
|
|
|
const TYPE_DM = 'dm'; |
39
|
|
|
const TYPE_DD = 'dd'; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* The symbols representing the different directions for usage in directional notation. |
43
|
|
|
*/ |
44
|
|
|
const OPT_NORTH_SYMBOL = 'north'; |
45
|
|
|
const OPT_EAST_SYMBOL = 'east'; |
46
|
|
|
const OPT_SOUTH_SYMBOL = 'south'; |
47
|
|
|
const OPT_WEST_SYMBOL = 'west'; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* The symbols representing degrees, minutes and seconds. |
51
|
|
|
*/ |
52
|
|
|
const OPT_DEGREE_SYMBOL = 'degree'; |
53
|
|
|
const OPT_MINUTE_SYMBOL = 'minute'; |
54
|
|
|
const OPT_SECOND_SYMBOL = 'second'; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* The symbol to use as separator between latitude and longitude. |
58
|
|
|
*/ |
59
|
|
|
const OPT_SEPARATOR_SYMBOL = 'separator'; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @see StringValueParser::stringParse |
63
|
|
|
* |
64
|
|
|
* @param string $value |
65
|
|
|
* |
66
|
|
|
* @return LatLongValue |
67
|
|
|
* @throws ParseException |
68
|
|
|
*/ |
69
|
|
|
protected function stringParse( $value ) { |
70
|
|
|
foreach ( $this->getParsers() as $parser ) { |
71
|
|
|
try { |
72
|
|
|
return $parser->parse( $value ); |
73
|
|
|
} catch ( ParseException $ex ) { |
74
|
50 |
|
continue; |
75
|
50 |
|
} |
76
|
|
|
} |
77
|
50 |
|
|
78
|
37 |
|
throw new ParseException( 'The format of the coordinate could not be determined. Parsing failed.' ); |
79
|
37 |
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @return StringValueParser[] |
83
|
2 |
|
*/ |
84
|
|
|
protected function getParsers() { |
85
|
|
|
$parsers = array(); |
86
|
|
|
|
87
|
|
|
$parsers[] = new FloatCoordinateParser( $this->options ); |
88
|
|
|
$parsers[] = new DmsCoordinateParser( $this->options ); |
89
|
50 |
|
$parsers[] = new DmCoordinateParser( $this->options ); |
90
|
50 |
|
$parsers[] = new DdCoordinateParser( $this->options ); |
91
|
|
|
|
92
|
50 |
|
return $parsers; |
93
|
50 |
|
} |
94
|
50 |
|
|
95
|
50 |
|
/** |
96
|
|
|
* Convenience function for determining if something is a valid coordinate string. |
97
|
50 |
|
* Analogous to creating an instance of the parser, parsing the string and checking isValid on the result. |
98
|
|
|
* |
99
|
|
|
* @deprecated since 2.0, please instantiate and call isValid() instead |
100
|
|
|
* |
101
|
|
|
* @param string $string |
102
|
|
|
* |
103
|
|
|
* @return boolean |
104
|
|
|
*/ |
105
|
|
|
public static function areCoordinates( $string ) { |
106
|
|
|
static $parser = null; |
107
|
|
|
|
108
|
|
|
if ( $parser === null ) { |
109
|
|
|
$parser = new self(); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
return $parser->parse( $string )->isValid(); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
} |
116
|
|
|
|