Completed
Push — address-as-title ( 9b6eeb...601934 )
by Peter
11:07
created

Geocoder::getAliases()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Maps;
4
5
/**
6
 * Base geocoder class to be inherited by classes with a specific geocoding implementation. 
7
 * 
8
 * @since 0.7
9
 * 
10
 * @licence GNU GPL v2+
11
 * @author Jeroen De Dauw < [email protected] >
12
 */
13
abstract class Geocoder {
14
15
	/**
16
	 * The internal name of the geocoder.
17
	 * 
18
	 * @since 0.7
19
	 * 
20
	 * @var string
21
	 */
22
	protected static $name;
23
	
24
	/**
25
	 * A list of aliases for the internal name.
26
	 * 
27
	 * @since 0.7
28
	 * 
29
	 * @var array
30
	 */
31
	protected $aliases;	
32
	
33
	/**
34
	 * Returns the url to which to make the geocoding request.
35
	 * 
36
	 * @since 0.7
37
	 * 
38
	 * @param string $address
39
	 * 
40
	 * @return string
41
	 */
42
	protected abstract function getRequestUrl( $address );
43
	
44
	/**
45
	 * Parses the response and returns it as an array with lat and lon keys.
46
	 * 
47
	 * @since 0.7
48
	 * 
49
	 * @param string $response
50
	 * 
51
	 * @return array
52
	 */	
53
	protected abstract function parseResponse( $response );
54
	
55
	/**
56
	 * Constructor.
57
	 * 
58
	 * @since 0.7
59
	 * 
60
	 * @param string $identifier
61
	 */
62
	public function __construct( $identifier ) {
63
		self::$name = $identifier;
64
	}
65
	
66
	/**
67
	 * Returns the geocoders identifier.
68
	 * 
69
	 * @since 0.7
70
	 * 
71
	 * @return string
72
	 */	
73
	public static function getName() {
74
		return self::$name;
75
	}
76
	
77
	/**
78
	 * Returns the geocoders aliases.
79
	 * 
80
	 * @since 0.7
81
	 * 
82
	 * @return array
83
	 */
84
	public function getAliases() {
85
		return $this->aliases;
86
	}
87
	
88
	/**
89
	 * Returns if the geocoder has a certain alias.
90
	 * 
91
	 * @since 0.7
92
	 *
93
	 * @param string $alias
94
	 *
95
	 * @return boolean
96
	 */
97
	public function hasAlias( $alias ) {
98
		return in_array( $alias, $this->aliases );
99
	}	
100
	
101
	/**
102
	 * Returns an array containing the geocoded latitude (lat) and
103
	 * longitude (lon) of the provided address, or false in case the
104
	 * geocoding fails.
105
	 *
106
	 * @since 0.2
107
	 *
108
	 * @param $address String: the address to be geocoded
109
	 * 
110
	 * @return array or false
111
	 */
112
	public function geocode( $address ) {
113
		$response = \Http::get( $this->getRequestUrl( $address ) );
114
		
115
		if ( $response === false ) {
116
			return false;
117
		}
118
		else {
119
			return $this->parseResponse( $response );
120
		}
121
	}
122
	
123
	/**
124
	 * Gets the contents of the first XML tag with the provided name,
125
	 * returns false when no matching element is found.
126
	 *
127
	 * @param string $xml
128
	 * @param string $tagName
129
	 * 
130
	 * @return string or false
131
	 */
132
	protected static function getXmlElementValue( $xml, $tagName ) {
133
		$match = [];
134
		preg_match( "/<$tagName>(.*?)<\/$tagName>/", $xml, $match );
135
		return count( $match ) > 1 ? $match[1] : false;
136
	}
137
	
138
	/**
139
	 * Returns the mapping service overrides for this geocoder, allowing it to be used
140
	 * instead of the default geocoder when none is provided for certain mapping services.
141
	 * 
142
	 * Returns an empty array by default. Override to add overrides.
143
	 * 
144
	 * @since 0.7
145
	 * 
146
	 * @return array
147
	 */
148
	public static function getOverrides() {
149
		return [];
150
	}
151
	
152
	/**
153
	 * Returns if the global geocoder cache should be used or not.
154
	 * By default it should be, but overriding this function allows
155
	 * for making a geocoder ignore it and implement it's own solution.
156
	 * 
157
	 * @since 0.7
158
	 * 
159
	 * @return boolean
160
	 */
161
	public function hasGlobalCacheSupport() {
162
		return true;
163
	}
164
	
165
}
166