Completed
Pull Request — master (#180)
by Peter
10:11
created

MapsGoogleGeocoder::parseResponse()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 12
ccs 0
cts 6
cp 0
rs 9.4285
cc 3
eloc 7
nc 2
nop 1
crap 12
1
<?php
2
3
/**
4
 * Class for geocoding requests with the Google Geocoding Service (v3).
5
 * 
6
 * Webservice documentation: http://code.google.com/apis/maps/documentation/geocoding/
7
 *
8
 * @licence GNU GPL v2+
9
 * @author Jeroen De Dauw < [email protected] >
10
 * @author Sergey Chernyshev
11
 * @author Desiree Gennaro
12
 */
13
final class MapsGoogleGeocoder extends \Maps\Geocoder {
14
	
15
	/**
16
	 * Registers the geocoder.
17
	 * 
18
	 * No LSB in pre-5.3 PHP *sigh*.
19
	 * This is to be refactored as soon as php >=5.3 becomes acceptable.
20
	 * 
21
	 * @since 0.7
22
	 */
23
	public static function register() {
24
		\Maps\Geocoders::registerGeocoder( 'google', __CLASS__ );
25
		return true;
26
	}		
27
	
28
	/**
29
	 * @see \Maps\Geocoder::getRequestUrl
30
	 * 
31
	 * @since 0.7
32
	 * 
33
	 * @param string $address
34
	 * 
35
	 * @return string
36
	 */	
37
	protected function getRequestUrl( $address ) {
38
		$urlArgs = [
39
			'address' => urlencode( $address ),
40
			'sensor' => false
41
		];
42
		if ( !empty( $GLOBALS['egMapsGMaps3ApiKey'] ) ) {
43
			$urlArgs['key'] = $GLOBALS['egMapsGMaps3ApiKey']
44
		}
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected '}'
Loading history...
45
46
		return 'http://maps.googleapis.com/maps/api/geocode/xml?' . wfArrayToCgi($urlArgs);
47
	}
48
	
49
	/**
50
	 * @see \Maps\Geocoder::parseResponse
51
	 * 
52
	 * @since 0.7
53
	 * 
54
	 * @param string $response
55
	 * 
56
	 * @return array
57
	 */		
58
	protected function parseResponse( $response ) {
59
		$lon = self::getXmlElementValue( $response, 'lng' );
60
		$lat = self::getXmlElementValue( $response, 'lat' );
61
62
		// In case on of the values is not found, return false.
63
		if ( !$lon || !$lat ) return false;
64
65
		return [
66
			'lat' => (float)$lat,
67
			'lon' => (float)$lon
68
		];
69
	}
70
	
71
	/**
72
	 * @see \Maps\Geocoder::getOverrides
73
	 * 
74
	 * @since 0.7
75
	 * 
76
	 * @return array
77
	 */
78
	public static function getOverrides() {
79
		return [ 'googlemaps3' ];
80
	}
81
	
82
}
83