Passed
Push — master ( 6e37f8...05a985 )
by Jeroen De
02:43
created

GlobeCoordinateParser::stringParse()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 24
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 3

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 24
ccs 15
cts 15
cp 1
rs 8.9713
cc 3
eloc 16
nc 4
nop 1
crap 3
1
<?php
2
3
namespace DataValues\Geo\Parsers;
4
5
use DataValues\Geo\Values\GlobeCoordinateValue;
6
use DataValues\Geo\Values\LatLongValue;
7
use ValueParsers\ParseException;
8
use ValueParsers\ParserOptions;
9
use ValueParsers\StringValueParser;
10
use ValueParsers\ValueParser;
11
12
/**
13
 * Extends the LatLongParser by adding precision detection support.
14
 *
15
 * The object that gets constructed is a GlobeCoordinateValue rather then a LatLongValue.
16
 *
17
 * @since 0.1
18
 *
19
 * @license GPL-2.0+
20
 * @author Jeroen De Dauw < [email protected] >
21
 * @author H. Snater < [email protected] >
22
 * @author Thiemo Kreuz
23
 */
24
class GlobeCoordinateParser implements ValueParser {
25
26
	const FORMAT_NAME = 'globe-coordinate';
27
28
	/**
29
	 * Option specifying the globe. Should be a string containing a Wikidata concept URI. Defaults
30
	 * to Earth.
31
	 */
32
	const OPT_GLOBE = 'globe';
33
34
	private $options;
35
36 122
	public function __construct( ParserOptions $options = null ) {
37 122
		$this->options = $options ?: new ParserOptions();
38
39 122
		$this->options->defaultOption( ValueParser::OPT_LANG, 'en' );
40 122
		$this->options->defaultOption( self::OPT_GLOBE, 'http://www.wikidata.org/entity/Q2' );
41 122
	}
42
43
	/**
44
	 * @see StringValueParser::stringParse
45
	 *
46
	 * @param string $value
47
	 *
48
	 * @throws ParseException
49
	 * @return GlobeCoordinateValue
50
	 */
51 122
	public function parse( $value ) {
52 122
		foreach ( $this->getParsers() as $precisionDetector => $parser ) {
53
			try {
54 122
				$latLong = $parser->parse( $value );
55
56 117
				return new GlobeCoordinateValue(
57 117
					new LatLongValue(
58 117
						$latLong->getLatitude(),
59 117
						$latLong->getLongitude()
60
					),
61 117
					$this->detectPrecision( $latLong, $precisionDetector ),
62 117
					$this->options->getOption( self::OPT_GLOBE )
63
				);
64 89
			} catch ( ParseException $parseException ) {
65 89
				continue;
66
			}
67
		}
68
69 5
		throw new ParseException(
70 5
			'The format of the coordinate could not be determined.',
71 5
			$value,
72 5
			self::FORMAT_NAME
73
		);
74
	}
75
76
	/**
77
	 * @param LatLongValue $latLong
78
	 * @param string $precisionDetector
79
	 *
80
	 * @return float|int
81
	 */
82 117
	private function detectPrecision( LatLongValue $latLong, $precisionDetector ) {
83 117
		if ( $this->options->hasOption( 'precision' ) ) {
84
			return $this->options->getOption( 'precision' );
85
		}
86
87 117
		return min(
88 117
			call_user_func( [ $this, $precisionDetector ], $latLong->getLatitude() ),
89 117
			call_user_func( [ $this, $precisionDetector ], $latLong->getLongitude() )
90
		);
91
	}
92
93
	/**
94
	 * @return ValueParser[]
95
	 */
96 122
	private function getParsers() {
97 122
		$parsers = [];
98
99 122
		$parsers['detectFloatPrecision'] = new FloatCoordinateParser( $this->options );
100 122
		$parsers['detectDmsPrecision'] = new DmsCoordinateParser( $this->options );
101 122
		$parsers['detectDmPrecision'] = new DmCoordinateParser( $this->options );
102 122
		$parsers['detectDdPrecision'] = new DdCoordinateParser( $this->options );
103
104 122
		return $parsers;
105
	}
106
107
	/**
108
	 * @param float $degree
109
	 *
110
	 * @return float|int
111
	 */
112 29
	private function detectDdPrecision( $degree ) {
113 29
		return $this->detectFloatPrecision( $degree );
114
	}
115
116
	/**
117
	 * @param float $degree
118
	 *
119
	 * @return float|int
120
	 */
121 25
	private function detectDmPrecision( $degree ) {
122 25
		$minutes = $degree * 60;
123 25
		$split = explode( '.', round( $minutes, 6 ) );
124
125 25
		if ( isset( $split[1] ) ) {
126 12
			return $this->detectDmsPrecision( $degree );
127
		}
128
129 13
		return 1 / 60;
130
	}
131
132
	/**
133
	 * @param float $degree
134
	 *
135
	 * @return float|int
136
	 */
137 42
	private function detectDmsPrecision( $degree ) {
138 42
		$seconds = $degree * 3600;
139 42
		$split = explode( '.', round( $seconds, 4 ) );
140
141 42
		if ( isset( $split[1] ) ) {
142 16
			return pow( 10, -strlen( $split[1] ) ) / 3600;
143
		}
144
145 26
		return 1 / 3600;
146
	}
147
148
	/**
149
	 * @param float $degree
150
	 *
151
	 * @return float|int
152
	 */
153 62
	private function detectFloatPrecision( $degree ) {
154 62
		$split = explode( '.', round( $degree, 8 ) );
155
156 62
		if ( isset( $split[1] ) ) {
157 52
			return pow( 10, -strlen( $split[1] ) );
158
		}
159
160 34
		return 1;
161
	}
162
163
}
164