summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/Maps/src/MediaWiki/ParserHooks/FindDestinationFunction.php
blob: e7cb319c6c65eea19c74bf19bb607b21865e260b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
<?php

namespace Maps\MediaWiki\ParserHooks;

use DataValues\Geo\Values\LatLongValue;
use Maps\MapsFactory;
use Maps\GeoFunctions;
use ParserHook;

/**
 * Class for the 'finddestination' parser hooks, which can find a
 * destination given a starting point, an initial bearing and a distance.
 *
 * @licence GNU GPL v2+
 * @author Jeroen De Dauw < jeroendedauw@gmail.com >
 */
class FindDestinationFunction extends ParserHook {

	/**
	 * Renders and returns the output.
	 *
	 * @see ParserHook::render
	 *
	 * @param array $parameters
	 *
	 * @return string
	 */
	public function render( array $parameters ) {
		$destination = GeoFunctions::findDestination(
			$parameters['location']->getCoordinates(),
			$parameters['bearing'],
			$parameters['distance']
		);

		return MapsFactory::globalInstance()->getCoordinateFormatter()->format(
			new LatLongValue( $destination['lat'], $destination['lon'] ),
			$parameters['format'],
			$parameters['directional']
		);
	}

	/**
	 * @see ParserHook::getMessage()
	 */
	public function getMessage() {
		return 'maps-finddestination-description';
	}

	/**
	 * Gets the name of the parser hook.
	 *
	 * @see ParserHook::getName
	 *
	 * @return string
	 */
	protected function getName() {
		return 'finddestination';
	}

	/**
	 * Returns an array containing the parameter info.
	 *
	 * @see ParserHook::getParameterInfo
	 *
	 * @return array
	 */
	protected function getParameterInfo( $type ) {
		global $egMapsAvailableCoordNotations;
		global $egMapsCoordinateNotation, $egMapsCoordinateDirectional;

		$params = [];

		$params['location'] = [
			'type' => 'mapslocation',
		];

		$params['format'] = [
			'default' => $egMapsCoordinateNotation,
			'values' => $egMapsAvailableCoordNotations,
			'aliases' => 'notation',
			'tolower' => true,
		];

		$params['directional'] = [
			'type' => 'boolean',
			'default' => $egMapsCoordinateDirectional,
		];

		$params['bearing'] = [
			'type' => 'float',
		];

		$params['distance'] = [
			'type' => 'distance',
		];

		// Give grep a chance to find the usages:
		// maps-finddestination-par-location, maps-finddestination-par-format,
		// maps-finddestination-par-directional, maps-finddestination-par-bearing,
		// maps-finddestination-par-distance, maps-finddestination-par-mappingservice,
		// maps-finddestination-par-geoservice, maps-finddestination-par-allowcoordinates
		foreach ( $params as $name => &$param ) {
			$param['message'] = 'maps-finddestination-par-' . $name;
		}

		return $params;
	}

	/**
	 * Returns the list of default parameters.
	 *
	 * @see ParserHook::getDefaultParameters
	 *
	 * @return array
	 */
	protected function getDefaultParameters( $type ) {
		return [ 'location', 'bearing', 'distance' ];
	}

}