Completed
Pull Request — master (#125)
by no
03:24 queued 48s
created

LatLongParser::areCoordinates()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 0
cts 0
cp 0
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
crap 6
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 50
	protected function stringParse( $value ) {
70 50
		foreach ( $this->getParsers() as $parser ) {
71
			try {
72 50
				return $parser->parse( $value );
73 37
			} catch ( ParseException $ex ) {
74 37
				continue;
75
			}
76
		}
77
78 2
		throw new ParseException( 'The format of the coordinate could not be determined. Parsing failed.' );
79
	}
80
81
	/**
82
	 * @return  StringValueParser[]
83
	 */
84 50
	private function getParsers() {
85 50
		$parsers = [];
86
87 50
		$parsers[] = new FloatCoordinateParser( $this->options );
88 50
		$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
	}
94
95
}
96