|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Class NominatimClient |
|
4
|
|
|
* |
|
5
|
|
|
* @author Mauro Moreno <[email protected]> |
|
6
|
|
|
*/ |
|
7
|
|
|
namespace MauroMoreno\OpenStreetMap\Client; |
|
8
|
|
|
|
|
9
|
|
|
use GuzzleHttp\Client as GuzzleClient; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Class NominatimClient |
|
13
|
|
|
* @package MauroMoreno\OpenStreetMap\Client |
|
14
|
|
|
*/ |
|
15
|
|
|
class NominatimClient |
|
16
|
|
|
{ |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Base URL |
|
20
|
|
|
*/ |
|
21
|
|
|
const BASE_URI = 'http://nominatim.openstreetmap.org'; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var GuzzleClient |
|
25
|
|
|
*/ |
|
26
|
|
|
private $guzzleClient; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* NominatimClient constructor. |
|
30
|
|
|
* @param array $config |
|
31
|
|
|
*/ |
|
32
|
4 |
|
public function __construct(array $config = []) |
|
33
|
|
|
{ |
|
34
|
4 |
|
$defaults['base_uri'] = self::BASE_URI; |
|
|
|
|
|
|
35
|
|
|
|
|
36
|
|
|
// Format xml or json |
|
37
|
4 |
|
if (!isset($config['format'])) { |
|
38
|
4 |
|
$config['format'] = 'json'; |
|
39
|
4 |
|
} |
|
40
|
|
|
|
|
41
|
|
|
// Address details |
|
42
|
4 |
|
if (!isset($config['addressdetails'])) { |
|
43
|
4 |
|
$config['addressdetails'] = 1; |
|
44
|
4 |
|
} |
|
45
|
|
|
|
|
46
|
4 |
|
$defaults = array_merge($defaults, $config); |
|
47
|
4 |
|
$this->guzzleClient = new GuzzleClient($defaults); |
|
48
|
4 |
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Reverse action |
|
52
|
|
|
* |
|
53
|
|
|
* @param $latitude |
|
54
|
|
|
* @param $longitude |
|
55
|
|
|
* |
|
56
|
|
|
* @return \Psr\Http\Message\ResponseInterface |
|
57
|
|
|
*/ |
|
58
|
3 |
|
public function reverse($latitude, $longitude) |
|
59
|
|
|
{ |
|
60
|
3 |
|
if (!$this->isLatitude($latitude)) { |
|
61
|
2 |
|
throw new \InvalidArgumentException( |
|
62
|
|
|
'Wrong value for latitude.' |
|
63
|
2 |
|
); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
1 |
|
if (!$this->isLongitude($longitude)) { |
|
67
|
|
|
throw new \InvalidArgumentException( |
|
68
|
|
|
'Wrong value for longitude.' |
|
69
|
|
|
); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
1 |
|
$response = $this->guzzleClient->get( |
|
73
|
1 |
|
'reverse', |
|
74
|
|
|
[ |
|
75
|
|
|
'query' => [ |
|
76
|
1 |
|
'format' => $this->guzzleClient->getConfig('format'), |
|
77
|
1 |
|
'lat' => $latitude, |
|
78
|
1 |
|
'lon' => $longitude, |
|
79
|
1 |
|
'addressdetails' => $this->guzzleClient->getConfig('addressdetails') |
|
80
|
1 |
|
], |
|
81
|
|
|
] |
|
82
|
1 |
|
); |
|
83
|
|
|
|
|
84
|
1 |
|
return $response; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Validate is latitude |
|
89
|
|
|
* |
|
90
|
|
|
* @param $value |
|
91
|
|
|
* |
|
92
|
|
|
* @return int |
|
93
|
|
|
*/ |
|
94
|
3 |
|
private function isLatitude($value) |
|
95
|
|
|
{ |
|
96
|
3 |
|
return preg_match('/^[-]?(([0-8]?[0-9])\.(\d+))|(90(\.0+)?)$/', $value); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Validate is longitude |
|
101
|
|
|
* |
|
102
|
|
|
* @param $value |
|
103
|
|
|
* |
|
104
|
|
|
* @return int |
|
105
|
|
|
*/ |
|
106
|
1 |
|
private function isLongitude($value) |
|
107
|
|
|
{ |
|
108
|
1 |
|
return preg_match( |
|
109
|
1 |
|
'/^[-]?((((1[0-7][0-9])|([0-9]?[0-9]))\.(\d+))|180(\.0+)?)$/', |
|
110
|
|
|
$value |
|
111
|
1 |
|
); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Get Guzzle Client |
|
116
|
|
|
* |
|
117
|
|
|
* @return GuzzleClient |
|
118
|
|
|
*/ |
|
119
|
2 |
|
public function getGuzzleClient() |
|
120
|
|
|
{ |
|
121
|
2 |
|
return $this->guzzleClient; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
} |
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.