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