Completed
Push — leaflet-attribution ( 0121c0 )
by Peter
04:35
created

MapsMapper::getCommonParameters()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 41
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 41
rs 8.8571
c 0
b 0
f 0
cc 2
eloc 25
nc 2
nop 0
1
<?php
2
3
/**
4
 * A class that holds static helper functions for generic mapping-related functions.
5
 *
6
 * @since 0.1
7
 *
8
 * @deprecated
9
 *
10
 * @licence GNU GPL v2+
11
 * @author Jeroen De Dauw < [email protected] >
12
 */
13
final class MapsMapper {
14
15
	/**
16
	 * Encode a variable of unknown type to JavaScript.
17
	 * Arrays are converted to JS arrays, objects are converted to JS associative
18
	 * arrays (objects). So cast your PHP associative arrays to objects before
19
	 * passing them to here.
20
	 *
21
	 * This is a copy of
22
	 *
23
	 * @see Xml::encodeJsVar
24
	 * which fixes incorrect behaviour with floats.
25
	 *
26
	 * @since 0.7.1
27
	 *
28
	 * @param mixed $value
29
	 *
30
	 * @return string
31
	 */
32
	public static function encodeJsVar( $value ) {
33
		if ( is_bool( $value ) ) {
34
			$s = $value ? 'true' : 'false';
35
		} elseif ( is_null( $value ) ) {
36
			$s = 'null';
37
		} elseif ( is_int( $value ) || is_float( $value ) ) {
38
			$s = $value;
39
		} elseif ( is_array( $value ) && // Make sure it's not associative.
40
			array_keys( $value ) === range( 0, count( $value ) - 1 ) ||
41
			count( $value ) == 0
42
		) {
43
			$s = '[';
44
			foreach ( $value as $elt ) {
0 ignored issues
show
Bug introduced by
The expression $value of type object|string|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
45
				if ( $s != '[' ) {
46
					$s .= ', ';
47
				}
48
				$s .= self::encodeJsVar( $elt );
49
			}
50
			$s .= ']';
51
		} elseif ( is_object( $value ) || is_array( $value ) ) {
52
			// Objects and associative arrays
53
			$s = '{';
54
			foreach ( (array)$value as $name => $elt ) {
55
				if ( $s != '{' ) {
56
					$s .= ', ';
57
				}
58
				$s .= '"' . Xml::encodeJsVar( $name ) . '": ' .
59
					self::encodeJsVar( $elt );
60
			}
61
			$s .= '}';
62
		} else {
63
			$s = '"' . Xml::encodeJsVar( $value ) . '"';
64
		}
65
		return $s;
66
	}
67
68
	/**
69
	 * This function returns the definitions for the parameters used by every map feature.
70
	 *
71
	 * @return array
72
	 */
73
	public static function getCommonParameters() {
74
		global $egMapsMapWidth, $egMapsMapHeight, $egMapsDefaultService;
75
76
		$params = [];
77
78
		$params['mappingservice'] = [
79
			'type' => 'mappingservice',
80
			'aliases' => 'service',
81
			'default' => $egMapsDefaultService,
82
		];
83
84
		$params['width'] = [
85
			'type' => 'dimension',
86
			'allowauto' => true,
87
			'units' => [ 'px', 'ex', 'em', '%', '' ],
88
			'default' => $egMapsMapWidth,
89
		];
90
91
		$params['height'] = [
92
			'type' => 'dimension',
93
			'units' => [ 'px', 'ex', 'em', '' ],
94
			'default' => $egMapsMapHeight,
95
		];
96
97
		$params['centre'] = [
98
			'type' => 'string',
99
			'aliases' => [ 'center' ],
100
			'default' => false,
101
			'manipulatedefault' => false,
102
		];
103
104
		// Give grep a chance to find the usages:
105
		// maps-par-mappingservice, maps-par-geoservice, maps-par-width,
106
		// maps-par-height, maps-par-centre
107
		foreach ( $params as $name => &$data ) {
108
			$data['name'] = $name;
109
			$data['message'] = 'maps-par-' . $name;
110
		}
111
112
		return array_merge( $params, self::getEvenMawrCommonParameters() );
113
	}
114
115
	private static function getEvenMawrCommonParameters() {
116
		global $egMapsDefaultTitle, $egMapsDefaultLabel;
117
118
		$params = [];
119
120
		$params['title'] = [
121
			'name' => 'title',
122
			'default' => $egMapsDefaultTitle,
123
		];
124
125
		$params['label'] = [
126
			'default' => $egMapsDefaultLabel,
127
			'aliases' => 'text',
128
		];
129
130
		$params['icon'] = [
131
			'default' => '', // TODO: image param
132
		];
133
134
		$params['visitedicon'] = [
135
			'default' => '', //TODO: image param
136
		];
137
138
		$params['lines'] = [
139
			'type' => 'mapsline',
140
			'default' => [],
141
			'delimiter' => ';',
142
			'islist' => true,
143
		];
144
145
		$params['polygons'] = [
146
			'type' => 'mapspolygon',
147
			'default' => [],
148
			'delimiter' => ';',
149
			'islist' => true,
150
		];
151
152
		$params['circles'] = [
153
			'type' => 'mapscircle',
154
			'default' => [],
155
			'delimiter' => ';',
156
			'islist' => true,
157
		];
158
159
		$params['rectangles'] = [
160
			'type' => 'mapsrectangle',
161
			'default' => [],
162
			'delimiter' => ';',
163
			'islist' => true,
164
		];
165
166
		$params['wmsoverlay'] = [
167
			'type' => 'wmsoverlay',
168
			'default' => false,
169
			'delimiter' => ' ',
170
		];
171
172
		$params['maxzoom'] = [
173
			'type' => 'integer',
174
			'default' => false,
175
			'manipulatedefault' => false,
176
			'dependencies' => 'minzoom',
177
		];
178
179
		$params['minzoom'] = [
180
			'type' => 'integer',
181
			'default' => false,
182
			'manipulatedefault' => false,
183
			'lowerbound' => 0,
184
		];
185
186
		$params['copycoords'] = [
187
			'type' => 'boolean',
188
			'default' => false,
189
		];
190
191
		$params['static'] = [
192
			'type' => 'boolean',
193
			'default' => false,
194
		];
195
196
		// Give grep a chance to find the usages:
197
		// maps-displaymap-par-title, maps-displaymap-par-label, maps-displaymap-par-icon,
198
		// maps-displaymap-par-visitedicon, aps-displaymap-par-lines, maps-displaymap-par-polygons,
199
		// maps-displaymap-par-circles, maps-displaymap-par-rectangles, maps-displaymap-par-wmsoverlay,
200
		// maps-displaymap-par-maxzoom, maps-displaymap-par-minzoom, maps-displaymap-par-copycoords,
201
		// maps-displaymap-par-static
202
		foreach ( $params as $name => &$param ) {
203
			if ( !array_key_exists( 'message', $param ) ) {
204
				$param['message'] = 'maps-displaymap-par-' . $name;
205
			}
206
		}
207
208
		return $params;
209
	}
210
211
	/**
212
	 * Resolves the url of images provided as wiki page; leaves others alone.
213
	 *
214
	 * @since 1.0
215
	 * @deprecated
216
	 *
217
	 * @param string $file
218
	 *
219
	 * @return string
220
	 */
221
	public static function getFileUrl( $file ) {
222
		$title = Title::makeTitle( NS_FILE, $file );
223
224
		if ( $title !== null && $title->exists() ) {
225
			$imagePage = new ImagePage( $title );
226
			return $imagePage->getDisplayedFile()->getURL();
227
		}
228
		return $file;
229
	}
230
231
	/**
232
	 * Returns JS to init the vars to hold the map data when they are not there already.
233
	 *
234
	 * @since 1.0
235
	 * @deprecated
236
	 *
237
	 * @param string $serviceName
238
	 *
239
	 * @return string
240
	 */
241
	public static function getBaseMapJSON( $serviceName ) {
242
		static $baseInit = false;
243
		static $serviceInit = [];
244
245
		$json = '';
246
247
		if ( !$baseInit ) {
248
			$baseInit = true;
249
			$json .= 'var mwmaps={};';
250
		}
251
252
		if ( !in_array( $serviceName, $serviceInit ) ) {
253
			$serviceInit[] = $serviceName;
254
			$json .= "mwmaps.$serviceName={};";
255
		}
256
257
		return $json;
258
	}
259
260
}
261