Completed
Push — oldjunk ( 76a40d...6d7aaf )
by Jeroen De
13:43 queued 04:50
created

MapsDisplayMap::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
use ParamProcessor\ProcessedParam;
4
use ParamProcessor\ProcessingResult;
5
6
/**
7
 * Class for the 'display_map' parser hooks.
8
 *
9
 * @since 0.7
10
 *
11
 * @licence GNU GPL v2+
12
 * @author Jeroen De Dauw < [email protected] >
13
 */
14
class MapsDisplayMap implements \ParserHooks\HookHandler {
15
16
	private $renderer;
17
18
	public function __construct() {
19
		$this->renderer = new MapsDisplayMapRenderer();
20
	}
21
22
	public function handle( Parser $parser, ProcessingResult $result ) {
23
		$params = $result->getParameters();
24
25
		$this->defaultMapZoom( $params );
26
27
		$parameters = [];
28
29
		foreach ( $params as $parameter ) {
30
			$parameters[$parameter->getName()] = $parameter->getValue();
31
		}
32
33
		$this->trackMap( $parser );
34
35
		// TODO: do not use global access
36
		$this->renderer->service = MapsMappingServices::getServiceInstance( $parameters['mappingservice'] );
37
38
		return $this->renderer->renderMap( $parameters, $parser );
39
	}
40
41
	public static function getHookDefinition( string $locationDelimiter ): \ParserHooks\HookDefinition {
42
		return new \ParserHooks\HookDefinition(
43
			[ 'display_map', 'display_point', 'display_points', 'display_line' ],
44
			self::getParameterDefinitions( $locationDelimiter ),
45
			[ 'coordinates' ]
46
		);
47
	}
48
49
	private static function getParameterDefinitions( $locationDelimiter ): array {
50
		$params = MapsMapper::getCommonParameters();
51
52
		$params['coordinates'] = [
53
			'type' => 'string',
54
			'aliases' => [ 'coords', 'location', 'address', 'addresses', 'locations', 'points' ],
55
			'default' => [],
56
			'islist' => true,
57
			'delimiter' => $locationDelimiter,
58
			'message' => 'maps-displaymap-par-coordinates',
59
		];
60
61
		return $params;
62
	}
63
64
	/**
65
	 * @param ProcessedParam[] $parameters
66
	 */
67
	private function defaultMapZoom( array &$parameters ) {
68
		if ( array_key_exists( 'zoom', $parameters ) && $parameters['zoom']->wasSetToDefault() && count(
69
				$parameters['coordinates']->getValue()
70
			) > 1 ) {
71
			$parameters['zoom'] = $this->getParameterWithValue( $parameters['zoom'], false );
72
		}
73
	}
74
75
	private function getParameterWithValue( ProcessedParam $param, $value ) {
76
		return new ProcessedParam(
77
			$param->getName(),
78
			$value,
79
			$param->wasSetToDefault(),
80
			$param->getOriginalName(),
81
			$param->getOriginalValue()
82
		);
83
	}
84
85
	private function trackMap( Parser $parser ) {
86
		if ( $GLOBALS['egMapsEnableCategory'] ) {
87
			$parser->addTrackingCategory( 'maps-tracking-category' );
88
		}
89
	}
90
91
92
}
93