Completed
Push — inline ( 44f4bc )
by Jeroen De
05:02
created

MapsGoogleMaps3::getLayerNames()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
/**
4
 * Class holding information and functionality specific to Google Maps v3.
5
 * This information and features can be used by any mapping feature.
6
 *
7
 * @since 0.7
8
 *
9
 * @licence GNU GPL v2+
10
 * @author Jeroen De Dauw < [email protected] >
11
 * @author Peter Grassberger < [email protected] >
12
 */
13
14
class MapsGoogleMaps3 extends MapsMappingService {
15
16
	/**
17
	 * Maps user input map types to the Google Maps names for the map types.
18
	 */
19
	private static $mapTypes = [
20
		'normal' => 'ROADMAP',
21
		'roadmap' => 'ROADMAP',
22
		'satellite' => 'SATELLITE',
23
		'hybrid' => 'HYBRID',
24
		'terrain' => 'TERRAIN',
25
		'physical' => 'TERRAIN',
26
		'earth' => 'earth'
27
	];
28
29
	private static $typeControlStyles = [
30
		'default' => 'DEFAULT',
31
		'horizontal' => 'HORIZONTAL_BAR',
32
		'dropdown' => 'DROPDOWN_MENU'
33
	];
34
35
	public function __construct( $serviceName ) {
36
		parent::__construct(
37
			$serviceName,
38
			[ 'googlemaps', 'google' ]
39
		);
40
	}
41
42
	/**
43
	 * @see MapsMappingService::addParameterInfo
44
	 *
45
	 * @since 0.7
46
	 */
47 7
	public function addParameterInfo( array &$params ) {
0 ignored issues
show
Coding Style introduced by
addParameterInfo uses the super-global variable $GLOBALS which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
48 7
		global $egMapsGMaps3Type, $egMapsGMaps3Types, $egMapsGMaps3Controls, $egMapsGMaps3Layers;
49 7
		global $egMapsGMaps3DefTypeStyle, $egMapsGMaps3DefZoomStyle, $egMapsGMaps3AutoInfoWindows;
50 7
		global $egMapsResizableByDefault, $egMapsGMaps3DefaultTilt;
51
52 7
		$params['zoom'] = [
53 7
			'type' => 'integer',
54
			'range' => [ 0, 20 ],
55 7
			'default' => self::getDefaultZoom(),
56 7
			'message' => 'maps-par-zoom',
57
		];
58
59 7
		$params['type'] = [
60 7
			'default' => $egMapsGMaps3Type,
61 7
			'values' => self::getTypeNames(),
62 7
			'message' => 'maps-googlemaps3-par-type',
63 7
			'post-format' => function ( $value ) {
64 7
				return MapsGoogleMaps3::$mapTypes[strtolower( $value )];
65 7
			},
66
		];
67
68 7
		$params['types'] = [
69 7
			'dependencies' => 'type',
70 7
			'default' => $egMapsGMaps3Types,
71 7
			'values' => self::getTypeNames(),
72 7
			'message' => 'maps-googlemaps3-par-types',
73
			'islist' => true,
74 7
			'post-format' => function ( array $value ) {
75 7
				foreach ( $value as &$part ) {
76 7
					$part = self::$mapTypes[strtolower( $part )];
77
				}
78
79 7
				return $value;
80 7
			},
81
		];
82
83 7
		$params['layers'] = [
84 7
			'default' => $egMapsGMaps3Layers,
85
			'values' => [
86
				'traffic',
87
				'bicycling'
88
			],
89 7
			'message' => 'maps-googlemaps3-par-layers',
90
			'islist' => true,
91
		];
92
93 7
		$params['controls'] = [
94 7
			'default' => $egMapsGMaps3Controls,
95
			'values' => [
96
				'pan',
97
				'zoom',
98
				'type',
99
				'scale',
100
				'streetview',
101
				'rotate'
102
			],
103 7
			'message' => 'maps-googlemaps3-par-controls',
104
			'islist' => true,
105 7
			'post-format' => function ( $value ) {
106 7
				return array_map( 'strtolower', $value );
107 7
			},
108
		];
109
110 7
		$params['zoomstyle'] = [
111 7
			'default' => $egMapsGMaps3DefZoomStyle,
112
			'values' => [ 'default', 'small', 'large' ],
113 7
			'message' => 'maps-googlemaps3-par-zoomstyle',
114 7
			'post-format' => 'strtoupper',
115
		];
116
117 7
		$params['typestyle'] = [
118 7
			'default' => $egMapsGMaps3DefTypeStyle,
119 7
			'values' => array_keys( self::$typeControlStyles ),
120 7
			'message' => 'maps-googlemaps3-par-typestyle',
121 7
			'post-format' => function ( $value ) {
122 7
				return self::$typeControlStyles[strtolower( $value )];
123 7
			},
124
		];
125
126 7
		$params['autoinfowindows'] = [
127 7
			'type' => 'boolean',
128 7
			'default' => $egMapsGMaps3AutoInfoWindows,
129 7
			'message' => 'maps-googlemaps3-par-autoinfowindows',
130
		];
131
132 7
		$params['resizable'] = [
133 7
			'type' => 'boolean',
134 7
			'default' => $egMapsResizableByDefault,
135 7
			'message' => 'maps-par-resizable',
136
		];
137
138 7
		$params['kmlrezoom'] = [
139 7
			'type' => 'boolean',
140 7
			'default' => $GLOBALS['egMapsRezoomForKML'],
141 7
			'message' => 'maps-googlemaps3-par-kmlrezoom',
142
		];
143
144 7
		$params['poi'] = [
145 7
			'type' => 'boolean',
146 7
			'default' => $GLOBALS['egMapsShowPOI'],
147 7
			'message' => 'maps-googlemaps3-par-poi',
148
		];
149
150 7
		$params['markercluster'] = [
151
			'type' => 'boolean',
152
			'default' => false,
153
			'message' => 'maps-par-markercluster',
154
		];
155
156 7
		$params['clustergridsize'] = [
157
			'type' => 'integer',
158
			'default' => 60,
159
			'message' => 'maps-googlemaps3-par-clustergridsize',
160
		];
161
162 7
		$params['clustermaxzoom'] = [
163
			'type' => 'integer',
164
			'default' => 20,
165
			'message' => 'maps-par-clustermaxzoom',
166
		];
167
168 7
		$params['clusterzoomonclick'] = [
169
			'type' => 'boolean',
170
			'default' => true,
171
			'message' => 'maps-par-clusterzoomonclick',
172
		];
173
174 7
		$params['clusteraveragecenter'] = [
175
			'type' => 'boolean',
176
			'default' => true,
177
			'message' => 'maps-googlemaps3-par-clusteraveragecenter',
178
		];
179
180 7
		$params['clusterminsize'] = [
181
			'type' => 'integer',
182
			'default' => 2,
183
			'message' => 'maps-googlemaps3-par-clusterminsize',
184
		];
185
186 7
		$params['tilt'] = [
187 7
			'type' => 'integer',
188 7
			'default' => $egMapsGMaps3DefaultTilt,
189 7
			'message' => 'maps-googlemaps3-par-tilt',
190
		];
191
192 7
		$params['imageoverlays'] = [
193
			'type' => 'mapsimageoverlay',
194
			'default' => [],
195
			'delimiter' => ';',
196
			'islist' => true,
197
			'message' => 'maps-googlemaps3-par-imageoverlays',
198
		];
199
200 7
		$params['kml'] = [
201
			'default' => [],
202
			'message' => 'maps-par-kml',
203
			'islist' => true,
204
			// new MapsParamFile() FIXME
205
		];
206
207 7
		$params['gkml'] = [
208
			'default' => [],
209
			'message' => 'maps-googlemaps3-par-gkml',
210
			'islist' => true,
211
		];
212
213 7
		$params['fusiontables'] = [
214
			'default' => [],
215
			'message' => 'maps-googlemaps3-par-fusiontables',
216
			'islist' => true,
217
		];
218
219 7
		$params['searchmarkers'] = [
220
			'default' => '',
221
			'message' => 'maps-par-searchmarkers',
222
			// new CriterionSearchMarkers() FIXME
223
		];
224
225 7
		$params['enablefullscreen'] = [
226
			'type' => 'boolean',
227
			'default' => false,
228
			'message' => 'maps-par-enable-fullscreen',
229
		];
230
231 7
		$params['scrollwheelzoom'] = [
232
			'type' => 'boolean',
233
			'default' => false,
234
			'message' => 'maps-par-scrollwheelzoom',
235
		];
236 7
	}
237
238
	/**
239
	 * @since 0.6.5
240
	 */
241 7
	public function getDefaultZoom() {
242 7
		global $egMapsGMaps3Zoom;
243 7
		return $egMapsGMaps3Zoom;
244
	}
245
246
	/**
247
	 * Returns the names of all supported map types.
248
	 *
249
	 * @return array
250
	 */
251 7
	public static function getTypeNames() {
252 7
		return array_keys( self::$mapTypes );
253
	}
254
255
	/**
256
	 * @see MapsMappingService::getMapId
257
	 *
258
	 * @since 0.6.5
259
	 */
260 7
	public function getMapId( $increment = true ) {
261 7
		static $mapsOnThisPage = 0;
262
263 7
		if ( $increment ) {
264 7
			$mapsOnThisPage++;
265
		}
266
267 7
		return 'map_google3_' . $mapsOnThisPage;
268
	}
269
270
	/**
271
	 * @see MapsMappingService::getResourceModules
272
	 *
273
	 * @since 1.0
274
	 *
275
	 * @return array of string
276
	 */
277 7
	public function getResourceModules() {
278 7
		return array_merge(
279 7
			parent::getResourceModules(),
280 7
			[ 'ext.maps.googlemaps3' ]
281
		);
282
	}
283
284
	/**
285
	 * @see MapsMappingService::getDependencies
286
	 *
287
	 * @return array
288
	 */
289 7
	protected function getDependencies() {
0 ignored issues
show
Coding Style introduced by
getDependencies uses the super-global variable $GLOBALS which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
290
		return [
291 7
			self::getApiScript(
292 7
				is_string( $GLOBALS['egMapsGMaps3Language'] ) ?
293 7
					$GLOBALS['egMapsGMaps3Language'] : $GLOBALS['egMapsGMaps3Language']->getCode()
294
			)
295
		];
296
	}
297
298 7
	public static function getApiScript( $langCode, array $urlArgs = [] ) {
0 ignored issues
show
Coding Style introduced by
getApiScript uses the super-global variable $GLOBALS which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
299 7
		$urlArgs = array_merge(
300
			[
301 7
				'language' => self::getMappedLanguageCode( $langCode )
302
			],
303 7
			$urlArgs
304
		);
305 7
		if ( $GLOBALS['egMapsGMaps3ApiKey'] !== '' ) {
306
			$urlArgs['key'] = $GLOBALS['egMapsGMaps3ApiKey'];
307
		}
308 7
		if ( $GLOBALS['egMapsGMaps3ApiVersion'] !== '' ) {
309
			$urlArgs['v'] = $GLOBALS['egMapsGMaps3ApiVersion'];
310
		}
311
312 7
		return Html::linkedScript( '//maps.googleapis.com/maps/api/js?' . wfArrayToCgi( $urlArgs ) );
313
	}
314
315
	/**
316
	 * Maps language codes to Google Maps API v3 compatible values.
317
	 *
318
	 * @param string $code
319
	 *
320
	 * @return string The mapped code
321
	 */
322 7
	protected static function getMappedLanguageCode( $code ) {
323
		$mappings = [
324 7
			'en_gb' => 'en-gb',// v3 supports en_gb - but wants us to call it en-gb
325
			'he' => 'iw',      // iw is googlish for hebrew
326
			'fj' => 'fil',     // google does not support Fijian - use Filipino as close(?) supported relative
327
		];
328
329 7
		if ( array_key_exists( $code, $mappings ) ) {
330
			$code = $mappings[$code];
331
		}
332
333 7
		return $code;
334
	}
335
}
336