Completed
Push — rmds ( 34c84e )
by Jeroen De
04:50
created

MapsMapper::getBaseMapJSON()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

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