Completed
Push — oldjunk ( 6d7aaf...37c282 )
by Jeroen De
02:36
created

MapsDisplayMap::extractServiceName()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 10
nc 3
nop 1
1
<?php
2
3
use Maps\ParameterExtractor;
4
use ParamProcessor\ProcessedParam;
5
6
/**
7
 * Class for the 'display_map' parser hooks.
8
 *
9
 * @licence GNU GPL v2+
10
 * @author Jeroen De Dauw < [email protected] >
11
 */
12
class MapsDisplayMap {
13
14
	private $renderer;
15
	private $defaultService;
16
	private $availableServices;
17
18
	public function __construct() {
19
		$this->renderer = new MapsDisplayMapRenderer();
20
21
		// TODO: inject
22
		$this->defaultService = $GLOBALS['egMapsDefaultService'];
23
		$this->availableServices = $GLOBALS['egMapsAvailableServices'];
24
	}
25
26
	/**
27
	 * @param Parser $parser
28
	 * @param string[] $parameters Values of the array can be named parameters ("key=value") or unnamed.
29
	 * They are not normalized, so can be "key =  value "
30
	 *
31
	 * @return string
32
	 * @throws MWException
33
	 */
34
	public function getMapHtmlForKeyValueStrings( Parser $parser, array $parameters ): string {
35
		$processor = new \ParamProcessor\Processor( new \ParamProcessor\Options() );
36
37
		// TODO: do not use global access
38
		$service = MapsMappingServices::getServiceInstance( $this->extractServiceName(
39
			Maps\ParameterExtractor::extractFromKeyValueStrings( $parameters )
40
		) );
41
42
		$parameterDefinitions = self::getHookDefinition( ';' )->getParameters();
43
		$service->addParameterInfo( $parameterDefinitions );
44
		$this->renderer->service = $service;
45
46
		$processor->setFunctionParams(
47
			$parameters,
48
			$parameterDefinitions,
49
			self::getHookDefinition( ';' )->getDefaultParameters()
50
		);
51
52
		return $this->getMapHtmlFromProcessor( $parser, $processor );
53
	}
54
55
	/**
56
	 * @param Parser $parser
57
	 * @param string[] $parameters Key value list of parameters. Unnamed parameters have numeric keys.
58
	 * Both keys and values have not been normalized.
59
	 *
60
	 * @return string
61
	 * @throws MWException
62
	 */
63
	public function getMapHtmlForParameterList( Parser $parser, array $parameters ) {
64
		$processor = new \ParamProcessor\Processor( new \ParamProcessor\Options() );
65
66
		// TODO: do not use global access
67
		$service = MapsMappingServices::getServiceInstance( $this->extractServiceName( $parameters ) );
68
69
		$parameterDefinitions = self::getHookDefinition( "\n" )->getParameters();
70
		$service->addParameterInfo( $parameterDefinitions );
71
		$this->renderer->service = $service;
72
73
		$processor->setParameters(
74
			$parameters,
75
			$parameterDefinitions
76
		);
77
78
		return $this->getMapHtmlFromProcessor( $parser, $processor );
79
	}
80
81
	private function getMapHtmlFromProcessor( Parser $parser, ParamProcessor\Processor $processor ) {
82
		$params = $processor->processParameters()->getParameters();
83
84
		$this->defaultMapZoom( $params );
85
86
		$this->trackMap( $parser );
87
88
		return $this->renderer->renderMap(
89
			$this->processedParametersToKeyValueArray( $params ),
90
			$parser
91
		);
92
	}
93
94
	private function extractServiceName( array $parameters ): string {
95
		$service = ( new ParameterExtractor() )->extract(
96
			[ 'mappingservice', 'service' ],
97
			$parameters
98
		);
99
100
		if ( $service === null ) {
101
			return $this->defaultService;
102
		}
103
104
		// TODO: do not use global access
105
		$service = MapsMappingServices::getMainServiceName( $service );
106
107
		if ( $this->serviceIsInvalid( $service ) ) {
108
			return $this->defaultService;
109
		}
110
111
		return $service;
112
	}
113
114
	private function serviceIsInvalid( string $service ) {
115
		return !in_array( $service, $this->availableServices );
116
	}
117
118
	private function processedParametersToKeyValueArray( array $params ): array {
119
		$parameters = [];
120
121
		foreach ( $params as $parameter ) {
122
			$parameters[$parameter->getName()] = $parameter->getValue();
123
		}
124
125
		return $parameters;
126
	}
127
128
	public static function getHookDefinition( string $locationDelimiter ): \ParserHooks\HookDefinition {
129
		return new \ParserHooks\HookDefinition(
130
			[ 'display_map', 'display_point', 'display_points', 'display_line' ],
131
			self::getParameterDefinitions( $locationDelimiter ),
132
			[ 'coordinates' ]
133
		);
134
	}
135
136
	private static function getParameterDefinitions( $locationDelimiter ): array {
137
		$params = MapsMapper::getCommonParameters();
138
139
		$params['coordinates'] = [
140
			'type' => 'string',
141
			'aliases' => [ 'coords', 'location', 'address', 'addresses', 'locations', 'points' ],
142
			'default' => [],
143
			'islist' => true,
144
			'delimiter' => $locationDelimiter,
145
			'message' => 'maps-displaymap-par-coordinates',
146
		];
147
148
		return $params;
149
	}
150
151
	/**
152
	 * @param ProcessedParam[] $parameters
153
	 */
154
	private function defaultMapZoom( array &$parameters ) {
155
		if ( array_key_exists( 'zoom', $parameters ) && $parameters['zoom']->wasSetToDefault() && count(
156
				$parameters['coordinates']->getValue()
157
			) > 1 ) {
158
			$parameters['zoom'] = $this->getParameterWithValue( $parameters['zoom'], false );
159
		}
160
	}
161
162
	private function getParameterWithValue( ProcessedParam $param, $value ) {
163
		return new ProcessedParam(
164
			$param->getName(),
165
			$value,
166
			$param->wasSetToDefault(),
167
			$param->getOriginalName(),
168
			$param->getOriginalValue()
169
		);
170
	}
171
172
	private function trackMap( Parser $parser ) {
173
		if ( $GLOBALS['egMapsEnableCategory'] ) {
174
			$parser->addTrackingCategory( 'maps-tracking-category' );
175
		}
176
	}
177
178
}
179