Completed
Push — dropOldAliases ( 17840d...e22dca )
by Jeroen De
12:52 queued 07:28
created

LatLongParser   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 6
c 2
b 0
f 0
lcom 1
cbo 6
dl 0
loc 72
ccs 13
cts 13
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A parse() 0 11 3
A __construct() 0 4 2
A getParsers() 0 10 1
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
	public function __construct( ParserOptions $options = null ) {
69 50
		$this->options = $options ?: new ParserOptions();
70 50
		$this->options->defaultOption( ValueParser::OPT_LANG, 'en' );
71
	}
72 50
73 37
	/**
74 37
	 * @see ValueParser::parse
75
	 *
76
	 * @param string $value
77
	 *
78 2
	 * @throws ParseException
79
	 * @return LatLongValue
80
	 */
81
	public function parse( $value ) {
82
		foreach ( $this->getParsers() as $parser ) {
83
			try {
84 50
				return $parser->parse( $value );
85 50
			} catch ( ParseException $ex ) {
86
				continue;
87 50
			}
88 50
		}
89 50
90 50
		throw new ParseException( 'The format of the coordinate could not be determined. Parsing failed.' );
91
	}
92 50
93
	/**
94
	 * @return  StringValueParser[]
95
	 */
96
	private function getParsers() {
97
		$parsers = [];
98
99
		$parsers[] = new FloatCoordinateParser( $this->options );
100
		$parsers[] = new DmsCoordinateParser( $this->options );
101
		$parsers[] = new DmCoordinateParser( $this->options );
102
		$parsers[] = new DdCoordinateParser( $this->options );
103
104
		return $parsers;
105
	}
106
107
}
108