Completed
Push — master ( a7a34c...1e3be1 )
by Jeroen De
03:21
created

DisplayMapFunction::getMapHtmlFromProcessor()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 7
cts 7
cp 1
rs 9.8666
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 1
1
<?php
2
3
namespace Maps\MediaWiki\ParserHooks;
4
5
use Maps;
6
use Maps\MapsFunctions;
7
use Maps\MappingServices;
8
use Maps\Presentation\ParameterExtractor;
9
use MWException;
10
use ParamProcessor;
11
use ParamProcessor\ProcessedParam;
12
use Parser;
13
14
/**
15
 * Class for the 'display_map' parser hooks.
16
 *
17
 * @licence GNU GPL v2+
18
 * @author Jeroen De Dauw < [email protected] >
19
 */
20
class DisplayMapFunction {
21
22
	private $services;
23
24
	private $renderer;
25
26 20
	public function __construct( MappingServices $services ) {
27 20
		$this->services = $services;
28
29 20
		$this->renderer = new DisplayMapRenderer();
30 20
	}
31
32
	/**
33
	 * @param Parser $parser
34
	 * @param string[] $parameters Values of the array can be named parameters ("key=value") or unnamed.
35
	 * They are not normalized, so can be "key =  value "
36
	 *
37
	 * @return string
38
	 * @throws MWException
39
	 */
40 18
	public function getMapHtmlForKeyValueStrings( Parser $parser, array $parameters ): string {
41 18
		$processor = new \ParamProcessor\Processor( new \ParamProcessor\Options() );
42
43 18
		$service = $this->services->getServiceOrDefault(
44 18
			$this->extractServiceName(
45 18
				Maps\Presentation\ParameterExtractor::extractFromKeyValueStrings( $parameters )
46
			)
47
		);
48
49 18
		$this->renderer->service = $service;
50
51 18
		$processor->setFunctionParams(
52 18
			$parameters,
53 18
			array_merge(
54 18
				self::getHookDefinition( ';' )->getParameters(),
55 18
				$service->getParameterInfo()
56
			),
57 18
			self::getHookDefinition( ';' )->getDefaultParameters()
58
		);
59
60 18
		return $this->getMapHtmlFromProcessor( $parser, $processor );
61
	}
62
63
	/**
64
	 * @param Parser $parser
65
	 * @param string[] $parameters Key value list of parameters. Unnamed parameters have numeric keys.
66
	 * Both keys and values have not been normalized.
67
	 *
68
	 * @return string
69
	 * @throws MWException
70
	 */
71 2
	public function getMapHtmlForParameterList( Parser $parser, array $parameters ) {
72 2
		$processor = new \ParamProcessor\Processor( new \ParamProcessor\Options() );
73
74 2
		$service = $this->services->getServiceOrDefault( $this->extractServiceName( $parameters ) );
75
76 2
		$this->renderer->service = $service;
77
78 2
		$processor->setParameters(
79 2
			$parameters,
80 2
			array_merge(
0 ignored issues
show
Documentation introduced by
array_merge(self::getHoo...ce->getParameterInfo()) is of type array<integer,object<Par...ParamDefinition>|array>, but the function expects a array<integer,object<Par...ssor\IParamDefinition>>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
81 2
				self::getHookDefinition( "\n" )->getParameters(),
82 2
				$service->getParameterInfo()
83
			)
84
		);
85
86 2
		return $this->getMapHtmlFromProcessor( $parser, $processor );
87
	}
88
89 20
	private function getMapHtmlFromProcessor( Parser $parser, ParamProcessor\Processor $processor ) {
90 20
		$params = $processor->processParameters()->getParameters();
91
92 20
		$this->defaultMapZoom( $params );
93
94 20
		$this->trackMap( $parser );
95
96 20
		return $this->renderer->renderMap(
97 20
			$this->processedParametersToKeyValueArray( $params ),
98 20
			$parser
99
		);
100
	}
101
102 20
	private function extractServiceName( array $parameters ): string {
103 20
		$service = ( new ParameterExtractor() )->extract(
104 20
			[ 'mappingservice', 'service' ],
105 20
			$parameters
106
		);
107
108 20
		return $service ?? '';
109
	}
110
111 20
	private function processedParametersToKeyValueArray( array $params ): array {
112 20
		$parameters = [];
113
114 20
		foreach ( $params as $parameter ) {
115 20
			$parameters[$parameter->getName()] = $parameter->getValue();
116
		}
117
118 20
		return $parameters;
119
	}
120
121 20
	public static function getHookDefinition( string $locationDelimiter ): \ParserHooks\HookDefinition {
122 20
		return new \ParserHooks\HookDefinition(
123 20
			[ 'display_map', 'display_point', 'display_points', 'display_line' ],
124 20
			self::getParameterDefinitions( $locationDelimiter ),
125 20
			[ 'coordinates' ]
126
		);
127
	}
128
129 20
	private static function getParameterDefinitions( $locationDelimiter ): array {
130 20
		$params = MapsFunctions::getCommonParameters();
131
132 20
		$params['coordinates'] = [
133 20
			'type' => 'string',
134
			'aliases' => [ 'coords', 'location', 'address', 'addresses', 'locations', 'points' ],
135
			'default' => [],
136
			'islist' => true,
137 20
			'delimiter' => $locationDelimiter,
138 20
			'message' => 'maps-displaymap-par-coordinates',
139
		];
140
141 20
		return $params;
142
	}
143
144
	/**
145
	 * @param ProcessedParam[] $parameters
146
	 */
147 20
	private function defaultMapZoom( array &$parameters ) {
148 20
		if ( array_key_exists( 'zoom', $parameters ) && $parameters['zoom']->wasSetToDefault() && count(
149 19
				$parameters['coordinates']->getValue()
150 20
			) > 1 ) {
151 2
			$parameters['zoom'] = $this->getParameterWithValue( $parameters['zoom'], false );
152
		}
153 20
	}
154
155 2
	private function getParameterWithValue( ProcessedParam $param, $value ) {
156 2
		return new ProcessedParam(
157 2
			$param->getName(),
158 2
			$value,
159 2
			$param->wasSetToDefault(),
160 2
			$param->getOriginalName(),
161 2
			$param->getOriginalValue()
162
		);
163
	}
164
165 20
	private function trackMap( Parser $parser ) {
166 20
		if ( $GLOBALS['egMapsEnableCategory'] ) {
167
			$parser->addTrackingCategory( 'maps-tracking-category' );
168
		}
169 20
	}
170
171
}
172