Completed
Push — newparam ( 7a21d8...9f04a0 )
by Jeroen De
02:53
created

DisplayMapFunction::getAllParameterDefinitions()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 4

Importance

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