Completed
Push — master ( 684184...9a35aa )
by Jeroen De
08:07
created

includes/Maps_Mapper.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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
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
	 * @deprecated
71
	 *
72
	 * @return array
73
	 */
74
	public static function getCommonParameters() {
75
		global $egMapsAvailableGeoServices, $egMapsDefaultGeoService, $egMapsMapWidth, $egMapsMapHeight, $egMapsDefaultService;
76
77
		$params = [];
78
79
		$params['mappingservice'] = [
80
			'type' => 'mappingservice',
81
			'aliases' => 'service',
82
			'default' => $egMapsDefaultService,
83
		];
84
85
		$params['geoservice'] = [
86
			'default' => $egMapsDefaultGeoService,
87
			'values' => $egMapsAvailableGeoServices,
88
			'dependencies' => 'mappingservice',
89
			// TODO 'manipulations' => new MapsParamGeoService( 'mappingservice' ),
90
		];
91
92
		$params['width'] = [
93
			'type' => 'dimension',
94
			'allowauto' => true,
95
			'units' => [ 'px', 'ex', 'em', '%', '' ],
96
			'default' => $egMapsMapWidth,
97
		];
98
99
		$params['height'] = [
100
			'type' => 'dimension',
101
			'units' => [ 'px', 'ex', 'em', '' ],
102
			'default' => $egMapsMapHeight,
103
		];
104
105
		// TODO$manipulation = new MapsParamLocation();
106
		// TODO$manipulation->toJSONObj = true;
107
108
		$params['centre'] = [
109
			'type' => 'mapslocation',
110
			'aliases' => [ 'center' ],
111
			'default' => false,
112
			'manipulatedefault' => false,
113
		];
114
115
		// Give grep a chance to find the usages:
116
		// maps-par-mappingservice, maps-par-geoservice, maps-par-width,
117
		// maps-par-height, maps-par-centre
118
		foreach ( $params as $name => &$data ) {
119
			$data['name'] = $name;
120
			$data['message'] = 'maps-par-' . $name;
121
		}
122
123
		return $params;
124
	}
125
	
126
	/**
127
	 * Resolves the url of images provided as wiki page; leaves others alone.
128
	 * 
129
	 * @since 1.0
130
	 * @deprecated
131
	 * 
132
	 * @param string $file
133
	 * 
134
	 * @return string
135
	 */
136
	public static function getFileUrl( $file ) {
137
		$title = Title::makeTitle( NS_FILE, $file );
138
139
		if( $title !==  null && $title->exists() ) {
140
			$imagePage = new ImagePage( $title );
141
			return $imagePage->getDisplayedFile()->getURL();
142
		}
143
		return $file;
144
	}
145
146
	/**
147
	 * Returns JS to init the vars to hold the map data when they are not there already.
148
	 * 
149
	 * @since 1.0
150
	 * @deprecated
151
	 * 
152
	 * @param string $serviceName
153
	 *
154
	 * @return string
155
	 */
156
	public static function getBaseMapJSON( $serviceName ) {
157
		static $baseInit = false;
158
		static $serviceInit = [];
159
160
		$json = '';
161
		
162
		if ( !$baseInit ) {
163
			$baseInit = true;
164
			$json .= 'var mwmaps={};';
165
		}
166
		
167
		if ( !in_array( $serviceName, $serviceInit ) ) {
168
			$serviceInit[] = $serviceName;
169
			$json .= "mwmaps.$serviceName={};";
170
		}
171
		
172
		return $json;
173
	}
174
	
175
}
176