1 | <?php |
||
22 | final class Geocoders { |
||
23 | |||
24 | /** |
||
25 | * Associative with geoservice identifiers as keys containing instances of |
||
26 | * the geocoder classes. |
||
27 | * |
||
28 | * Note: This list only contains the instances, so is not to be used for |
||
29 | * looping over all available services, as not all of them are guaranteed |
||
30 | * to have an instance already, use $registeredServices for this purpouse. |
||
31 | * |
||
32 | * @since 0.7 |
||
33 | * |
||
34 | * @var Geocoder[] |
||
35 | */ |
||
36 | protected static $geocoders = []; |
||
37 | |||
38 | /** |
||
39 | * Associative with geoservice identifiers as keys containing the class |
||
40 | * name of the geocoders. This is used for registration of a geocoder |
||
41 | * without immediately instantiating it. |
||
42 | * |
||
43 | * @since 0.7 |
||
44 | * |
||
45 | * @var array of string => string |
||
46 | */ |
||
47 | public static $registeredGeocoders = []; |
||
48 | |||
49 | /** |
||
50 | * The global geocoder cache, holding geocoded data when enabled. |
||
51 | * |
||
52 | * @since 0.7 |
||
53 | * |
||
54 | * @var array |
||
55 | */ |
||
56 | private static $globalGeocoderCache = []; |
||
57 | |||
58 | /** |
||
59 | * Can geocoding happen, ie are there any geocoders available. |
||
60 | * |
||
61 | * @since 1.0.3 |
||
62 | * @var boolean |
||
63 | */ |
||
64 | protected static $canGeocode = false; |
||
65 | |||
66 | /** |
||
67 | * Returns if this class can do geocoding operations. |
||
68 | * Ie. if there are any geocoders available. |
||
69 | * |
||
70 | * @since 0.7 |
||
71 | * |
||
72 | * @return boolean |
||
73 | */ |
||
74 | public static function canGeocode() { |
||
78 | |||
79 | /** |
||
80 | * Gets a list of available geocoders. |
||
81 | * |
||
82 | * @since 1.0.3 |
||
83 | * |
||
84 | * @return array |
||
85 | */ |
||
86 | public static function getAvailableGeocoders() { |
||
90 | |||
91 | /** |
||
92 | * Initiate the geocoding functionality. |
||
93 | * |
||
94 | * @since 1.0.3 |
||
95 | * |
||
96 | * @return boolean Indicates if init happened |
||
97 | */ |
||
98 | public static function init() { |
||
99 | static $initiated = false; |
||
100 | |||
101 | if ( $initiated ) { |
||
102 | return false; |
||
103 | } |
||
104 | |||
105 | $initiated = true; |
||
106 | |||
107 | // Register the geocoders. |
||
108 | \Hooks::run( 'GeocoderFirstCallInit' ); |
||
109 | |||
110 | // Determine if there are any geocoders. |
||
111 | self::$canGeocode = count( self::$registeredGeocoders ) > 0; |
||
112 | |||
113 | return true; |
||
114 | } |
||
115 | |||
116 | /** |
||
117 | * This function first determines whether the provided string is a pair or coordinates |
||
118 | * or an address. If it's the later, an attempt to geocode will be made. The function will |
||
119 | * return the coordinates or false, in case a geocoding attempt was made but failed. |
||
120 | * |
||
121 | * @since 0.7 |
||
122 | * |
||
123 | * @param string $coordsOrAddress |
||
124 | * @param string $geoservice |
||
125 | * @param string|false $mappingService |
||
126 | * @param boolean $checkForCoords |
||
127 | * |
||
128 | * @return LatLongValue|false |
||
129 | */ |
||
130 | public static function attemptToGeocode( $coordsOrAddress, $geoservice = '', $mappingService = false, $checkForCoords = true ) { |
||
144 | |||
145 | /** |
||
146 | * Geocodes an address with the provided geocoding service and returns the result |
||
147 | * as an array, or false when the geocoding failed. |
||
148 | * |
||
149 | * @since 0.7 |
||
150 | * |
||
151 | * @param string $address |
||
152 | * @param string $geoService |
||
153 | * @param string|false $mappingService |
||
154 | * |
||
155 | * @return LatLongValue|false |
||
156 | * @throws MWException |
||
157 | */ |
||
158 | public static function geocode( $address, $geoService = '', $mappingService = false ) { |
||
194 | |||
195 | private static function getGeocoded( Geocoder $geocoder, $address ) { |
||
207 | |||
208 | private static function getGeocodedAsArray( Geocoder $geocoder, $address ) { |
||
220 | |||
221 | /** |
||
222 | * Returns already coordinates already known from previous geocoding operations, |
||
223 | * or false if there is no match found in the cache. |
||
224 | * |
||
225 | * @since 0.7 |
||
226 | * |
||
227 | * @param string $address |
||
228 | * |
||
229 | * @return LatLongValue|boolean false |
||
230 | */ |
||
231 | private static function cacheRead( $address ) { |
||
241 | |||
242 | /** |
||
243 | * Writes the geocoded result to the cache if the cache is on. |
||
244 | * |
||
245 | * @since 0.7 |
||
246 | * |
||
247 | * @param string $address |
||
248 | * @param LatLongValue $coordinates |
||
249 | */ |
||
250 | private static function cacheWrite( $address, LatLongValue $coordinates ) { |
||
258 | |||
259 | /** |
||
260 | * Registers a geocoder linked to an identifier. |
||
261 | * |
||
262 | * @since 0.7 |
||
263 | * |
||
264 | * @param string $geocoderIdentifier |
||
265 | * @param string|\Maps\Geocoders\Geocoder $geocoder |
||
266 | */ |
||
267 | public static function registerGeocoder( $geocoderIdentifier, $geocoder ) { |
||
270 | |||
271 | /** |
||
272 | * Returns the instance of the geocoder linked to the provided identifier |
||
273 | * or the default one when it's not valid. False is returned when there |
||
274 | * are no geocoders available. |
||
275 | * |
||
276 | * @since 0.7 |
||
277 | * |
||
278 | * @param string $geocoderIdentifier |
||
279 | * |
||
280 | * @return Geocoder|bool |
||
281 | */ |
||
282 | private static function getValidGeocoderInstance( $geocoderIdentifier ) { |
||
285 | |||
286 | /** |
||
287 | * Returns the instance of a geocoder. This function assumes there is a |
||
288 | * geocoder linked to the identifier you provide - if you are not sure |
||
289 | * it does, use getValidGeocoderInstance instead. |
||
290 | * |
||
291 | * @since 0.7 |
||
292 | * |
||
293 | * @param string $geocoderIdentifier |
||
294 | * |
||
295 | * @return Geocoder|bool |
||
296 | * @throws MWException |
||
297 | */ |
||
298 | private static function getGeocoderInstance( $geocoderIdentifier ) { |
||
324 | |||
325 | /** |
||
326 | * Returns a valid geocoder idenifier. If the given one is a valid main identifier, |
||
327 | * it will simply be returned. If it's an alias, it will be turned into the correponding |
||
328 | * main identifier. If it's not recognized at all (or empty), the default will be used. |
||
329 | * Only call this function when there are geocoders available, else an erro will be thrown. |
||
330 | * |
||
331 | * @since 0.7 |
||
332 | * |
||
333 | * @param string $geocoderIdentifier |
||
334 | * |
||
335 | * @return string|bool |
||
336 | * @throws MWException |
||
337 | */ |
||
338 | private static function getValidGeocoderIdentifier( $geocoderIdentifier ) { |
||
363 | |||
364 | } |
||
365 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.