Completed
Push — master ( 4d0076...81538e )
by Jeroen De
26s queued 11s
created

GoogleMapsService::processingResultToMapParams()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 17
ccs 10
cts 10
cp 1
rs 9.3888
c 0
b 0
f 0
cc 5
nc 4
nop 1
crap 5
1
<?php
2
3
declare( strict_types = 1 );
4
5
namespace Maps;
6
7
use Html;
8
use Maps\Map\MapData;
9
use ParamProcessor\ProcessedParam;
10
use ParamProcessor\ProcessingResult;
11
12
/**
13
 * @licence GNU GPL v2+
14
 * @author Jeroen De Dauw < [email protected] >
15
 * @author Peter Grassberger < [email protected] >
16
 */
17
class GoogleMapsService implements MappingService {
18
19
	/**
20
	 * Maps user input map types to the Google Maps names for the map types.
21
	 */
22
	private const MAP_TYPES = [
23
		'normal' => 'ROADMAP',
24
		'roadmap' => 'ROADMAP',
25
		'satellite' => 'SATELLITE',
26
		'hybrid' => 'HYBRID',
27
		'terrain' => 'TERRAIN',
28
		'physical' => 'TERRAIN',
29
		'earth' => 'earth'
30
	];
31
32
	private const TYPE_CONTROL_STYLES = [
33
		'default' => 'DEFAULT',
34
		'horizontal' => 'HORIZONTAL_BAR',
35
		'dropdown' => 'DROPDOWN_MENU'
36
	];
37
38
	private $addedDependencies = [];
39
40 27
	public function getName(): string {
41 27
		return 'googlemaps3';
42
	}
43
44 27
	public function getAliases(): array {
45 27
		return [ 'googlemaps', 'google' ];
46
	}
47
48 8
	public function getParameterInfo(): array {
49 8
		global $egMapsGMaps3Type, $egMapsGMaps3Types, $egMapsGMaps3Controls, $egMapsGMaps3Layers;
50 8
		global $egMapsGMaps3DefTypeStyle, $egMapsGMaps3DefZoomStyle, $egMapsGMaps3AutoInfoWindows;
51 8
		global $egMapsResizableByDefault;
52
53 8
		$params = MapsFunctions::getCommonParameters();
54
55 8
		$params['visitedicon'] = [
56
			'default' => '',
57
			'message' => 'maps-displaymap-par-visitedicon',
58
		];
59
60 8
		$params['wmsoverlay'] = [
61
			'type' => 'wmsoverlay',
62
			'default' => false,
63
			'delimiter' => ' ',
64
			'message' => 'maps-displaymap-par-wmsoverlay',
65
		];
66
67 8
		$params['zoom'] = [
68 8
			'type' => 'integer',
69
			'range' => [ 0, 20 ],
70 8
			'default' => $GLOBALS['egMapsGMaps3Zoom'],
71 8
			'message' => 'maps-par-zoom',
72
		];
73
74 8
		$params['type'] = [
75 8
			'default' => $egMapsGMaps3Type,
76 8
			'values' => self::getTypeNames(),
77 8
			'message' => 'maps-googlemaps3-par-type',
78 8
			'post-format' => function ( $value ) {
79 8
				return GoogleMapsService::MAP_TYPES[strtolower( $value )];
80 8
			},
81
		];
82
83 8
		$params['types'] = [
84 8
			'dependencies' => 'type',
85 8
			'default' => $egMapsGMaps3Types,
86 8
			'values' => self::getTypeNames(),
87 8
			'message' => 'maps-googlemaps3-par-types',
88
			'islist' => true,
89 8
			'post-format' => function ( array $value ) {
90 8
				foreach ( $value as &$part ) {
91 8
					$part = self::MAP_TYPES[strtolower( $part )];
92
				}
93
94 8
				return $value;
95 8
			},
96
		];
97
98 8
		$params['layers'] = [
99 8
			'default' => $egMapsGMaps3Layers,
100
			'values' => [
101
				'traffic',
102
				'bicycling',
103
				'transit'
104
			],
105 8
			'message' => 'maps-googlemaps3-par-layers',
106
			'islist' => true,
107
		];
108
109 8
		$params['controls'] = [
110 8
			'default' => $egMapsGMaps3Controls,
111
			'values' => [
112
				'pan',
113
				'zoom',
114
				'type',
115
				'scale',
116
				'streetview',
117
				'rotate'
118
			],
119 8
			'message' => 'maps-googlemaps3-par-controls',
120
			'islist' => true,
121 8
			'post-format' => function ( $value ) {
122 8
				return array_map( 'strtolower', $value );
123 8
			},
124
		];
125
126 8
		$params['zoomstyle'] = [
127 8
			'default' => $egMapsGMaps3DefZoomStyle,
128
			'values' => [ 'default', 'small', 'large' ],
129 8
			'message' => 'maps-googlemaps3-par-zoomstyle',
130 8
			'post-format' => 'strtoupper',
131
		];
132
133 8
		$params['typestyle'] = [
134 8
			'default' => $egMapsGMaps3DefTypeStyle,
135 8
			'values' => array_keys( self::TYPE_CONTROL_STYLES ),
136 8
			'message' => 'maps-googlemaps3-par-typestyle',
137 8
			'post-format' => function ( $value ) {
138 8
				return self::TYPE_CONTROL_STYLES[strtolower( $value )];
139 8
			},
140
		];
141
142 8
		$params['autoinfowindows'] = [
143 8
			'type' => 'boolean',
144 8
			'default' => $egMapsGMaps3AutoInfoWindows,
145 8
			'message' => 'maps-googlemaps3-par-autoinfowindows',
146
		];
147
148 8
		$params['resizable'] = [
149 8
			'type' => 'boolean',
150 8
			'default' => $egMapsResizableByDefault,
151 8
			'message' => 'maps-par-resizable',
152
		];
153
154 8
		$params['kmlrezoom'] = [
155 8
			'type' => 'boolean',
156 8
			'default' => $GLOBALS['egMapsRezoomForKML'],
157 8
			'message' => 'maps-googlemaps3-par-kmlrezoom',
158
		];
159
160 8
		$params['poi'] = [
161 8
			'type' => 'boolean',
162 8
			'default' => $GLOBALS['egMapsShowPOI'],
163 8
			'message' => 'maps-googlemaps3-par-poi',
164
		];
165
166 8
		$params['cluster'] = [
167
			'aliases' => [ 'markercluster' ],
168
			'type' => 'boolean',
169
			'default' => false,
170
			'message' => 'maps-par-markercluster',
171
		];
172
173 8
		$params['clustergridsize'] = [
174
			'type' => 'integer',
175
			'default' => 60,
176
			'message' => 'maps-googlemaps3-par-clustergridsize',
177
		];
178
179 8
		$params['clustermaxzoom'] = [
180
			'type' => 'integer',
181
			'default' => 20,
182
			'message' => 'maps-par-clustermaxzoom',
183
		];
184
185 8
		$params['clusterzoomonclick'] = [
186
			'type' => 'boolean',
187
			'default' => true,
188
			'message' => 'maps-par-clusterzoomonclick',
189
		];
190
191 8
		$params['clusteraveragecenter'] = [
192
			'type' => 'boolean',
193
			'default' => true,
194
			'message' => 'maps-googlemaps3-par-clusteraveragecenter',
195
		];
196
197 8
		$params['clusterminsize'] = [
198
			'type' => 'integer',
199
			'default' => 2,
200
			'message' => 'maps-googlemaps3-par-clusterminsize',
201
		];
202
203 8
		$params['imageoverlays'] = [
204
			'type' => 'mapsimageoverlay',
205
			'default' => [],
206
			'delimiter' => ';',
207
			'islist' => true,
208
			'message' => 'maps-googlemaps3-par-imageoverlays',
209
		];
210
211 8
		$params['kml'] = [
212 8
			'default' => [],
213 8
			'message' => 'maps-par-kml',
214
			'islist' => true,
215 8
			'post-format' => function( array $kmlFileNames ) {
216 8
				return array_values(
217 8
					array_filter(
218 8
						array_map(
219 8
							function( string $fileName ) {
220 1
								return wfExpandUrl( MapsFunctions::getFileUrl( $fileName ) );
0 ignored issues
show
Deprecated Code introduced by
The method Maps\MapsFunctions::getFileUrl() has been deprecated.

This method has been deprecated.

Loading history...
221 8
							},
222
							$kmlFileNames
223
						),
224 8
						function( string $fileName ) {
225 1
							return $fileName !== '';
226 8
						}
227
					)
228
				);
229 8
			}
230
		];
231
232 8
		$params['gkml'] = [
233
			'default' => [],
234
			'message' => 'maps-googlemaps3-par-gkml',
235
			'islist' => true,
236
		];
237
238 8
		$params['searchmarkers'] = [
239
			'default' => '',
240
			'message' => 'maps-par-searchmarkers',
241
			// new CriterionSearchMarkers() FIXME
242
		];
243
244 8
		$params['fullscreen'] = [
245
			'aliases' => [ 'enablefullscreen' ],
246
			'type' => 'boolean',
247
			'default' => false,
248
			'message' => 'maps-par-enable-fullscreen',
249
		];
250
251 8
		$params['scrollwheelzoom'] = [
252
			'aliases' => [ 'scrollzoom' ],
253
			'type' => 'boolean',
254
			'default' => false,
255
			'message' => 'maps-par-scrollwheelzoom',
256
		];
257
258 8
		return $params;
259
	}
260
261
	/**
262
	 * Returns the names of all supported map types.
263
	 */
264 8
	private function getTypeNames(): array {
265 8
		return array_keys( self::MAP_TYPES );
266
	}
267
268 8
	public function newMapId(): string {
269 8
		static $mapsOnThisPage = 0;
270
271 8
		$mapsOnThisPage++;
272
273 8
		return 'map_google3_' . $mapsOnThisPage;
274
	}
275
276 8
	public function getResourceModules( array $params ): array {
277 8
		return [ 'ext.maps.googlemaps3', 'ext.maps.googlemaps3ajax' ];
278
	}
279
280 8
	public static function getApiScript( $langCode, array $urlArgs = [] ) {
281 8
		$urlArgs = array_merge(
282
			[
283 8
				'language' => self::getMappedLanguageCode( $langCode )
284
			],
285
			$urlArgs
286
		);
287 8
		if ( $GLOBALS['egMapsGMaps3ApiKey'] !== '' ) {
288
			$urlArgs['key'] = $GLOBALS['egMapsGMaps3ApiKey'];
289
		}
290 8
		if ( $GLOBALS['egMapsGMaps3ApiVersion'] !== '' ) {
291
			$urlArgs['v'] = $GLOBALS['egMapsGMaps3ApiVersion'];
292
		}
293
294 8
		return Html::linkedScript( '//maps.googleapis.com/maps/api/js?' . wfArrayToCgi( $urlArgs ) );
295
	}
296
297
	/**
298
	 * Maps language codes to Google Maps API v3 compatible values.
299
	 */
300 8
	private static function getMappedLanguageCode( string $code ): string {
301
		$mappings = [
302 8
			'en_gb' => 'en-gb',// v3 supports en_gb - but wants us to call it en-gb
303
			'he' => 'iw',      // iw is googlish for hebrew
304
			'fj' => 'fil',     // google does not support Fijian - use Filipino as close(?) supported relative
305
		];
306
307 8
		if ( array_key_exists( $code, $mappings ) ) {
308
			return $mappings[$code];
309
		}
310
311 8
		return $code;
312
	}
313
314 8
	public function getDependencyHtml( array $params ): string {
315 8
		$dependencies = [];
316
317
		// Only add dependencies that have not yet been added.
318 8
		foreach ( $this->getDependencies() as $dependency ) {
319 8
			if ( !in_array( $dependency, $this->addedDependencies ) ) {
320 1
				$dependencies[] = $dependency;
321 1
				$this->addedDependencies[] = $dependency;
322
			}
323
		}
324
325 8
		return implode( '', $dependencies );
326
	}
327
328 8
	private function getDependencies(): array {
329
		return [
330 8
			self::getApiScript(
331 8
				is_string( $GLOBALS['egMapsGMaps3Language'] ) ?
332 8
					$GLOBALS['egMapsGMaps3Language'] : $GLOBALS['egMapsGMaps3Language']->getCode()
333
			)
334
		];
335
	}
336
337 8
	public function newMapDataFromProcessingResult( ProcessingResult $processingResult ): MapData {
338 8
		$parameters = $processingResult->getParameters();
339
340 8
		if ( array_key_exists( 'zoom', $parameters ) && $parameters['zoom']->wasSetToDefault() && count(
341 7
				$parameters['coordinates']->getValue()
342 8
			) > 1 ) {
343 1
			$parameters['zoom'] = $this->getParameterWithValue( $parameters['zoom'], false );
344
		}
345
346 8
		$mapParams = [];
347
348 8
		foreach ( $parameters as $parameter ) {
349 8
			$mapParams[$parameter->getName()] = $parameter->getValue();
350
		}
351
352 8
		return $this->newMapDataFromParameters( $mapParams );
353
	}
354
355
356 1
	private function getParameterWithValue( ProcessedParam $param, $value ) {
357 1
		return new ProcessedParam(
358 1
			$param->getName(),
359
			$value,
360 1
			$param->wasSetToDefault(),
361 1
			$param->getOriginalName(),
362 1
			$param->getOriginalValue()
363
		);
364
	}
365
366 8
	public function newMapDataFromParameters( array $params ): MapData {
367 8
		return new MapData( $params );
368
	}
369
370
}
371