Completed
Branch heritage_class (c07d31)
by Maxime
02:23
created

Nominatim::newLookup()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
/**
3
 * Class Nominatim
4
 *
5
 * @package      maxh\nominatim
6
 * @author       Maxime Hélias <[email protected]>
7
 */
8
9
namespace maxh\Nominatim;
10
11
use GuzzleHttp\Client;
12
use GuzzleHttp\Psr7\Request;
13
use Psr\Http\Message\ResponseInterface;
14
15
use maxh\Nominatim\Exceptions\InvalidParameterException;
16
use maxh\Nominatim\Exceptions\NominatimException;
17
18
/**
19
 *  Wrapper to manage exchanges with OSM Nominatim API
20
 *  
21
 */
22
class Nominatim
23
{
24
25
	/**
26
	 * Contain url of the current application
27
	 * @var string
28
	 */
29
	private $application_url = null;
30
31
	/**
32
	 * Contain http client connection
33
	 * @var Client
34
	 */
35
	private $http_client = null;
36
37
	/**
38
	 * The search object which serves as a template for new ones created
39
	 * by 'newSearch()' method.
40
	 *
41
	 * @var Search
42
	 */
43
	private $baseSearch;
44
45
	/**
46
	 * Template for new ones created by 'newReverser()' method.
47
	 * @var Reverse
48
	 */
49
	private $baseReverse;
50
51
	/**
52
	 * Template for new ones created by 'newLookup()' method.
53
	 * @var Lookup
54
	 */
55
	private $baseLookup;
56
57
	/**
58
	 * Constructor
59
	 * @param string      			$application_url Contain url of the current application
60
	 * @param Guzzle\Client|null 	$http_client     Client object from Guzzle
61
	 */
62
	public function __construct(
63
		$application_url,
64
		Client $http_client = null
65
	) {
66
67
		if (!isset($application_url))
68
		{
69
			throw new InvalidParameterException("Application url parameter is empty");
70
		}
71
72
		if (!isset($http_client))
73
		{
74
			$http_client = new Client([
75
				'base_uri'			 => $application_url,
76
				'timeout'			 => 30,
77
				'connection_timeout' => 5,
78
			]);
79
		} 
80
		else if ($http_client instanceof Client)
81
		{
82
			$application_url_client = $http_client->getConfig('base_uri');
83
84
			if (!isset($application_url_client))
85
			{
86
				$http_client->setDefaultOption('base_uri', $application_url);
87
			}
88
			else if ($application_url_client != $application_url)
89
			{
90
				throw new InvalidParameterException("http_client parameter has a differente url to application_url parameter");
91
			}
92
		}
93
		else
94
		{
95
			throw new InvalidParameterException("http_client parameter must be a GuzzleHttp\Client object or empty");
96
		}
97
98
		$this->application_url = $application_url;
99
		$this->http_client = $http_client;
100
101
		//Create base
102
		$this->baseSearch = new Search();
103
		$this->baseReverse = new Reverse();
104
		$this->baseLookup = new Lookup();
105
106
	}
107
108
	/**
109
	 * Returns a new search object based on the base search.
110
	 *
111
	 * @return Search
112
	 */
113
	public function newSearch()
114
	{
115
		return clone $this->baseSearch;
116
	}
117
118
	/**
119
	 * Returns a new search object based on the base reverse.
120
	 *
121
	 * @return Reverse
122
	 */
123
	public function newReverse()
124
	{
125
		return clone $this->baseReverse;
126
	}
127
128
	/**
129
	 * Returns a new search object based on the base lookup.
130
	 * 
131
	 * @return Lookup
132
	 */
133
	public function newLookup()
134
	{
135
		return clone $this->baseLookup;
136
	}
137
138
	/**
139
	 * Decode the data returned from the request
140
	 * 
141
	 * @param  string   $format   json or xml
142
	 * @param  Request  $request  Request object from Guzzle
143
	 * @param  ResponseInterface $response Interface response object from Guzzle
144
	 * 
145
	 * @return array|\SimpleXMLElement
146
	 * @throws maxh\Nominatim\Exceptions\NominatimException if no format for decode
147
	 */
148
	private function decodeResponse($format, Request $request, ResponseInterface $response)
149
	{
150
151
		if ($format == 'json')
152
		{
153
			return json_decode($response->getBody(), true);
154
		}
155
156
		if ($format == 'xml')
157
		{
158
			return new \SimpleXMLElement($response->getBody());
159
		}
160
161
		throw new NominatimException("Format is undefined or not supported for decode response", $request, $response);
162
	}
163
164
	/**
165
	 * Runs the query and returns the result set from Nominatim.
166
	 * @param  QueryInterface $nRequest  The object request to send
167
	 * 
168
	 * @return array                                        The decoded data returned from Nominatim
169
	 * @throws \GuzzleHttp\Exception\ClientException 		if http request is an error
170
	 */
171
	public function find(QueryInterface $nRequest)
172
	{
173
		$url = $this->application_url . '/' . $nRequest->getPath() . '?';
174
		$request = new Request('GET', $url);
175
176
		//Convert the query array to string with space replace to +
177
		$query = \GuzzleHttp\Psr7\build_query($nRequest->getQuery(), PHP_QUERY_RFC1738);
178
179
		$url = $request->getUri()->withQuery($query);
180
		$request = $request->withUri($url);
181
182
		return $this->decodeResponse(
183
			$nRequest->getFormat(),
184
			$request,
185
			$this->http_client->send($request)
186
		);
187
	}
188
189
	/**
190
	 * Return the client using by instance
191
	 * @return GuzzleHttp\Client
192
	 */
193
	public function getClient()
194
	{
195
		return $this->http_client;
196
	}
197
198
}
199