Completed
Push — master ( 684184...9a35aa )
by Jeroen De
08:07
created

includes/parserhooks/Maps_Geocode.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
use DataValues\Geo\Formatters\GeoCoordinateFormatter;
3
4
/**
5
 * Class for the 'geocode' parser hooks, which can turn
6
 * human readable locations into sets of coordinates.
7
 * 
8
 * @since 0.7
9
 * 
10
 * @licence GNU GPL v2+
11
 * @author Jeroen De Dauw < [email protected] >
12
 */
13
class MapsGeocode extends ParserHook {
0 ignored issues
show
Deprecated Code introduced by
The class ParserHook has been deprecated with message: since 1.0 in favour of the ParserHooks library

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
14
15
	/**
16
	 * Gets the name of the parser hook.
17
	 * @see ParserHook::getName
18
	 * 
19
	 * @since 0.7
20
	 * 
21
	 * @return string
22
	 */
23
	protected function getName() {
24
		return 'geocode';
25
	}
26
	
27
	/**
28
	 * Returns an array containing the parameter info.
29
	 * @see ParserHook::getParameterInfo
30
	 * 
31
	 * @since 0.7
32
	 * 
33
	 * @return array
34
	 */
35
	protected function getParameterInfo( $type ) {
36
		global $egMapsAvailableGeoServices, $egMapsAvailableCoordNotations;
37
		global $egMapsDefaultGeoService, $egMapsCoordinateNotation;
38
		global $egMapsAllowCoordsGeocoding, $egMapsCoordinateDirectional;
39
		
40
		$params = [];
41
42
		$params['location'] = [
43
			'type' => 'mapslocation',
44
			'dependencies' => [ 'mappingservice', 'geoservice' ],
45
		];
46
47
		$params['mappingservice'] = [
48
			'default' => '',
49
			'values' => MapsMappingServices::getAllServiceValues(),
50
			'tolower' => true,
51
		];
52
53
		$params['geoservice'] = [
54
			'default' => $egMapsDefaultGeoService,
55
			'aliases' => 'service',
56
			'values' => $egMapsAvailableGeoServices,
57
			'tolower' => true,
58
		];
59
60
		$params['allowcoordinates'] = [
61
			'type' => 'boolean',
62
			'default' => $egMapsAllowCoordsGeocoding,
63
		];
64
65
		$params['format'] = [
66
			'default' => $egMapsCoordinateNotation,
67
			'values' => $egMapsAvailableCoordNotations,
68
			'aliases' => 'notation',
69
			'tolower' => true,
70
		];
71
72
		$params['directional'] = [
73
			'type' => 'boolean',
74
			'default' => $egMapsCoordinateDirectional,
75
		];
76
77
		// Give grep a chance to find the usages:
78
		// maps-geocode-par-location, maps-geocode-par-mappingservice, maps-geocode-par-geoservice,
79
		// maps-geocode-par-allowcoordinates, maps-geocode-par-format, maps-geocode-par-directional
80
		foreach ( $params as $name => &$param ) {
81
			$param['message'] = 'maps-geocode-par-' . $name;
82
		}
83
84
		return $params;
85
	}
86
	
87
	/**
88
	 * Returns the list of default parameters.
89
	 * @see ParserHook::getDefaultParameters
90
	 * 
91
	 * @since 0.7
92
	 * 
93
	 * @return array
94
	 */
95
	protected function getDefaultParameters( $type ) {
96
		return [ 'location', 'geoservice', 'mappingservice' ];
97
	}	
98
	
99
	/**
100
	 * Renders and returns the output.
101
	 * @see ParserHook::render
102
	 * 
103
	 * @since 0.7
104
	 * 
105
	 * @param array $parameters
106
	 * 
107
	 * @return string
108
	 */
109
	public function render( array $parameters ) {
110
		/**
111
		 * @var \DataValues\LatLongValue $coordinates
112
		 */
113
		$coordinates = $parameters['location']->getCoordinates();
114
115
		$options = new \ValueFormatters\FormatterOptions( [
116
			GeoCoordinateFormatter::OPT_FORMAT => $parameters['format'],
117
			GeoCoordinateFormatter::OPT_DIRECTIONAL => $parameters['directional'],
118
			GeoCoordinateFormatter::OPT_PRECISION => 1 / 360000
119
		] );
120
121
		$formatter = new GeoCoordinateFormatter( $options );
122
123
		return $formatter->format( $coordinates );
124
	}
125
126
	/**
127
	 * @see ParserHook::getMessage()
128
	 * 
129
	 * @since 1.0
130
	 */
131
	public function getMessage() {
132
		return 'maps-geocode-description';
133
	}		
134
	
135
}