1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
use DataValues\Geo\Formatters\GeoCoordinateFormatter; |
4
|
|
|
use Maps\Geocoders; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Class for the 'geocode' parser hooks, which can turn |
8
|
|
|
* human readable locations into sets of coordinates. |
9
|
|
|
* |
10
|
|
|
* @since 0.7 |
11
|
|
|
* |
12
|
|
|
* @licence GNU GPL v2+ |
13
|
|
|
* @author Jeroen De Dauw < [email protected] > |
14
|
|
|
*/ |
15
|
|
|
class MapsGeocode extends ParserHook { |
|
|
|
|
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Gets the name of the parser hook. |
19
|
|
|
* @see ParserHook::getName |
20
|
|
|
* |
21
|
|
|
* @since 0.7 |
22
|
|
|
* |
23
|
|
|
* @return string |
24
|
|
|
*/ |
25
|
1 |
|
protected function getName() { |
26
|
1 |
|
return 'geocode'; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Returns an array containing the parameter info. |
31
|
|
|
* @see ParserHook::getParameterInfo |
32
|
|
|
* |
33
|
|
|
* @since 0.7 |
34
|
|
|
* |
35
|
|
|
* @return array |
36
|
|
|
*/ |
37
|
2 |
|
protected function getParameterInfo( $type ) { |
38
|
2 |
|
global $egMapsAvailableGeoServices, $egMapsAvailableCoordNotations; |
39
|
2 |
|
global $egMapsDefaultGeoService, $egMapsCoordinateNotation; |
40
|
2 |
|
global $egMapsAllowCoordsGeocoding, $egMapsCoordinateDirectional; |
41
|
|
|
|
42
|
2 |
|
$params = []; |
43
|
|
|
|
44
|
2 |
|
$params['location'] = [ |
45
|
2 |
|
'type' => 'string', |
46
|
2 |
|
'message' => 'maps-geocode-par-location', |
47
|
|
|
]; |
48
|
|
|
|
49
|
2 |
|
$params['geoservice'] = [ |
50
|
2 |
|
'default' => $egMapsDefaultGeoService, |
51
|
2 |
|
'aliases' => 'service', |
52
|
2 |
|
'values' => $egMapsAvailableGeoServices, |
53
|
2 |
|
'tolower' => true, |
54
|
2 |
|
'message' => 'maps-geocode-par-geoservice', |
55
|
|
|
]; |
56
|
|
|
|
57
|
2 |
|
$params['allowcoordinates'] = [ |
58
|
2 |
|
'type' => 'boolean', |
59
|
2 |
|
'default' => $egMapsAllowCoordsGeocoding, |
60
|
2 |
|
'message' => 'maps-geocode-par-allowcoordinates', |
61
|
|
|
]; |
62
|
|
|
|
63
|
2 |
|
$params['format'] = [ |
64
|
2 |
|
'default' => $egMapsCoordinateNotation, |
65
|
2 |
|
'values' => $egMapsAvailableCoordNotations, |
66
|
2 |
|
'aliases' => 'notation', |
67
|
2 |
|
'tolower' => true, |
68
|
2 |
|
'message' => 'maps-geocode-par-format', |
69
|
|
|
]; |
70
|
|
|
|
71
|
2 |
|
$params['directional'] = [ |
72
|
2 |
|
'type' => 'boolean', |
73
|
2 |
|
'default' => $egMapsCoordinateDirectional, |
74
|
2 |
|
'message' => 'maps-geocode-par-directional', |
75
|
|
|
]; |
76
|
|
|
|
77
|
2 |
|
return $params; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Returns the list of default parameters. |
82
|
|
|
* @see ParserHook::getDefaultParameters |
83
|
|
|
* |
84
|
|
|
* @since 0.7 |
85
|
|
|
* |
86
|
|
|
* @return array |
87
|
|
|
*/ |
88
|
1 |
|
protected function getDefaultParameters( $type ) { |
89
|
1 |
|
return [ 'location', 'geoservice' ]; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Renders and returns the output. |
94
|
|
|
* @see ParserHook::render |
95
|
|
|
* |
96
|
|
|
* @since 0.7 |
97
|
|
|
* |
98
|
|
|
* @param array $parameters |
99
|
|
|
* |
100
|
|
|
* @return string |
101
|
|
|
*/ |
102
|
1 |
|
public function render( array $parameters ) { |
103
|
1 |
|
if ( !Geocoders::canGeocode() ) { |
104
|
|
|
return 'No geocoders available'; |
105
|
|
|
} |
106
|
|
|
|
107
|
1 |
|
$coordinates = Geocoders::attemptToGeocode( |
108
|
1 |
|
$parameters['location'], |
109
|
1 |
|
$parameters['geoservice'], |
110
|
1 |
|
$parameters['allowcoordinates'] |
111
|
1 |
|
); |
112
|
|
|
|
113
|
1 |
|
if ( $coordinates === false ) { |
114
|
|
|
return 'Geocoding failed'; |
115
|
|
|
} |
116
|
|
|
|
117
|
1 |
|
$options = new \ValueFormatters\FormatterOptions( [ |
118
|
1 |
|
GeoCoordinateFormatter::OPT_FORMAT => $parameters['format'], |
119
|
1 |
|
GeoCoordinateFormatter::OPT_DIRECTIONAL => $parameters['directional'], |
120
|
1 |
|
GeoCoordinateFormatter::OPT_PRECISION => 1 / 360000 |
121
|
1 |
|
] ); |
122
|
|
|
|
123
|
1 |
|
$formatter = new GeoCoordinateFormatter( $options ); |
124
|
|
|
|
125
|
1 |
|
return $formatter->format( $coordinates ); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @see ParserHook::getMessage() |
130
|
|
|
* |
131
|
|
|
* @since 1.0 |
132
|
|
|
*/ |
133
|
|
|
public function getMessage() { |
134
|
|
|
return 'maps-geocode-description'; |
|
|
|
|
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
} |
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.