Completed
Push — mv ( 03c11a...584528 )
by Jeroen De
03:24 queued 44s
created

MapsMappingService::addHtmlDependencies()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 3
cts 4
cp 0.75
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2.0625
1
<?php
2
3
/**
4
 * Base class for mapping services. Deriving classes hold mapping service specific
5
 * information and functionality, which can be used by any mapping feature.
6
 *
7
 * @since 0.6.3
8
 *
9
 * @licence GNU GPL v2+
10
 * @author Jeroen De Dauw < [email protected] >
11
 */
12
abstract class MapsMappingService {
13
14
	/**
15
	 * The internal name of the service.
16
	 *
17
	 * @var string
18
	 */
19
	protected $serviceName;
20
21
	/**
22
	 * A list of aliases for the internal name.
23
	 *
24
	 * @var array
25
	 */
26
	protected $aliases;
27
28
	/**
29
	 * A list of features that support the service, used for validation and defaulting.
30
	 *
31
	 * @var array
32
	 */
33
	protected $features;
34
35
	/**
36
	 * A list of names of resource modules to add.
37
	 *
38
	 * @var array
39
	 */
40
	protected $resourceModules = [];
41
42
	/**
43
	 * A list of dependencies (header items) that have been added.
44
	 *
45
	 * @var array
46
	 */
47
	private $addedDependencies = [];
48
49
	/**
50
	 * A list of dependencies (header items) that need to be added.
51
	 *
52
	 * @var array
53
	 */
54
	private $dependencies = [];
55
56
	/**
57
	 * @param string $serviceName
58
	 * @param array $aliases
59
	 */
60
	public function __construct( $serviceName, array $aliases = [] ) {
61
		$this->serviceName = $serviceName;
62
		$this->aliases = $aliases;
63
	}
64
65
	/**
66
	 * @since 0.7
67
	 *
68
	 * @param $parameterInfo array of IParam
69
	 */
70
	public function addParameterInfo( array &$parameterInfo ) {
71
	}
72
73
	/**
74
	 * @since 0.6.3
75
	 */
76
	public function addFeature( $featureName, $handlingClass ) {
77
		$this->features[$featureName] = $handlingClass;
78
	}
79
80
	/**
81
	 * @since 5.2.0
82
	 * @param ParserOutput $parserOutput
83
	 */
84 19
	public final function addDependencies( ParserOutput $parserOutput ) {
85 19
		$dependencies = $this->getDependencyHtml();
86
87
		// Only add a head item when there are dependencies.
88 19
		if ( $dependencies ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $dependencies of type string|false is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
89 2
			$parserOutput->addHeadItem( $dependencies );
90
		}
91
92 19
		$parserOutput->addModules( $this->getResourceModules() );
93 19
	}
94
95
	/**
96
	 * @since 0.6.3
97
	 */
98 19
	public final function getDependencyHtml() {
99 19
		$allDependencies = array_merge( $this->getDependencies(), $this->dependencies );
100 19
		$dependencies = [];
101
102
		// Only add dependencies that have not yet been added.
103 19
		foreach ( $allDependencies as $dependency ) {
104 19
			if ( !in_array( $dependency, $this->addedDependencies ) ) {
105 2
				$dependencies[] = $dependency;
106 19
				$this->addedDependencies[] = $dependency;
107
			}
108
		}
109
110
		// If there are dependencies, put them all together in a string, otherwise return false.
111 19
		return $dependencies !== [] ? implode( '', $dependencies ) : false;
112
	}
113
114
	/**
115
	 * Returns a list of html fragments, such as script includes, the current service depends on.
116
	 *
117
	 * @since 0.6.3
118
	 *
119
	 * @return array
120
	 */
121
	protected function getDependencies() {
122
		return [];
123
	}
124
125
	/**
126
	 * Returns the resource modules that need to be loaded to use this mapping service.
127
	 *
128
	 * @since 0.7.3
129
	 *
130
	 * @return array of string
131
	 */
132 19
	public function getResourceModules() {
133 19
		return $this->resourceModules;
134
	}
135
136
	/**
137
	 * @since 0.6.3
138
	 */
139 19
	public function getName() {
140 19
		return $this->serviceName;
141
	}
142
143
	/**
144
	 * @since 0.6.6
145
	 */
146
	public function getFeatureInstance( $featureName ) {
147
		$className = $this->getFeature( $featureName );
148
149
		if ( $className === false || !class_exists( $className ) ) {
150
			throw new MWException( 'Could not create a mapping feature class instance' );
151
		}
152
153
		return new $className( $this );
154
	}
155
156
	/**
157
	 * @since 0.6.3
158
	 */
159
	public function getFeature( $featureName ) {
160
		return array_key_exists( $featureName, $this->features ) ? $this->features[$featureName] : false;
161
	}
162
163
	/**
164
	 * @since 0.6.3
165
	 */
166 19
	public function getAliases() {
167 19
		return $this->aliases;
168
	}
169
170
	/**
171
	 * @since 0.6.3
172
	 */
173 7
	public function hasAlias( $alias ) {
174 7
		return in_array( $alias, $this->aliases );
175
	}
176
177
	/**
178
	 * Add one or more names of resource modules that should be loaded.
179
	 *
180
	 * @since 0.7.3
181
	 *
182
	 * @param mixed $modules Array of string or string
183
	 */
184
	public function addResourceModules( $modules ) {
185
		$this->resourceModules = array_merge( $this->resourceModules, (array)$modules );
186
	}
187
188
	/**
189
	 * @param array $dependencies
190
	 */
191 19
	public function addHtmlDependencies( array $dependencies ) {
192 19
		foreach ( $dependencies as $dependency ) {
193
			$this->addHtmlDependency( $dependency );
194
		}
195 19
	}
196
197
	/**
198
	 * @since 0.6.3
199
	 *
200
	 * @param $dependencyHtml
201
	 */
202
	public final function addHtmlDependency( $dependencyHtml ) {
203
		$this->dependencies[] = $dependencyHtml;
204
	}
205
206
	/**
207
	 * @since 1.0
208
	 */
209
	public function getEarthZoom() {
210
		return 1;
211
	}
212
213
	public abstract function getMapId( $increment = true );
214
215
}
216