Completed
Push — master ( 211857...11662a )
by Maxime
07:33 queued 03:39
created

Query::addressDetails()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 1
1
<?php
2
/**
3
 * Class Query
4
 *
5
 * @package      maxh\nominatim
6
 * @author       Maxime Hélias <[email protected]>
7
 */
8
9
namespace maxh\Nominatim;
10
11
use maxh\Nominatim\Exceptions\InvalidParameterException;
12
13
/**
14
 * Class implementing functionality common to requests nominatim.
15
 */
16
class Query implements QueryInterface
17
{
18
	/**
19
	 * Contain the path of the request
20
	 * @var string
21
	 */
22
	protected $path;
23
24
	/**
25
	 * Contain the query for request
26
	 * @var array
27
	 */
28
	protected $query = [];
29
30
	/**
31
	 * Contain the format for decode data returning by the request
32
	 * @var string
33
	 */
34
	protected $format;
35
36
	/**
37
	 * Output format accepted
38
	 * @var array
39
	 */
40
	protected $accepteFormat = ['xml', 'json'];
41
42
	/**
43
	 * Output polygon format accepted
44
	 * @var array
45
	 */
46
	protected $polygon = ['geojson', 'kml', 'svg', 'text'];
47
48
49
	/**
50
	 * Constuctor
51
	 * @param array $query Default value for this query
52
	 */
53
	public function __construct(array $query = [])
54
	{
55
		if (!isset($query['format']))
56
		{
57
			//Default format
58
			$query['format'] = 'json';
59
		}
60
61
		$this->setQuery($query);
62
		$this->setFormat($query['format']);
63
64
	}
65
66
	// -- Builder methods ------------------------------------------------------
67
68
	/**
69
	 * Format returning by the request.
70
	 *
71
	 * @param  string $format The output format for the request
72
	 *
73
	 * @return maxh\Nominatim\Query
74
	 * @throws maxh\Nominatim\Exceptions\InvalidParameterException if format is not supported
75
	 */
76
	public function format($format)
77
	{
78
		$format = strtolower($format);
79
80
		if (in_array($format, $this->accepteFormat))
81
		{
82
			$this->setFormat($format);
83
84
			return $this;
85
		}
86
87
		throw new InvalidParameterException("Format is not supported");
88
	}
89
90
	/**
91
	 * Preferred language order for showing search results, overrides the value
92
	 * specified in the "Accept-Language" HTTP header. Either uses standard
93
	 * rfc2616 accept-language string or a simple comma separated list of
94
	 * language codes.
95
	 *
96
	 * @param  string $language         Preferred language order for showing search results, overrides the value specified in the "Accept-Language" HTTP header.
97
	 * Either uses standard rfc2616 accept-language string or a simple comma separated list of language codes.
98
	 *
99
	 * @return maxh\Nominatim\Query
100
	 */
101
	public function language($language)
102
	{
103
		$this->query['accept-language'] = $language;
104
105
		return $this;
106
	}
107
108
	/**
109
	 * Include a breakdown of the address into elements.
110
	 *
111
	 * @param  boolean $details
112
	 * 
113
	 * @return maxh\Nominatim\Query
114
	 */
115
	public function addressDetails($details = true)
116
	{
117
		$this->query['addressdetails'] = $details ? "1" : "0";
118
119
		return $this;
120
	}
121
122
	/**
123
	 * If you are making large numbers of request please include a valid email address or alternatively include your email address as part of the User-Agent string.
124
	 * This information will be kept confidential and only used to contact you in the event of a problem, see Usage Policy for more details.
125
	 * 
126
	 * @param  string $email Address mail
127
	 * 
128
	 * @return maxh\Nominatim\Query
129
	 */
130
	public function email($email)
131
	{
132
		$this->query['email'] = $email;
133
134
		return $this;
135
	}
136
137
	/**
138
	 * Output format for the geometry of results
139
	 * 
140
	 * @param  string $polygon
141
	 * 
142
	 * @return maxh\Nominatim\Query
143
	 * @throws maxh\Nominatim\Exceptions\InvalidParameterException  if polygon format is not supported
144
	 */
145
	public function polygon($polygon)
146
	{
147
		if (in_array($polygon, $this->polygon))
148
		{
149
			$this->query['polygon_' . $polygon] = "1";
150
151
			return $this;
152
		}
153
154
		throw new InvalidParameterException("This polygon format is not supported");
155
	}
156
157
	/**
158
	 * Include additional information in the result if available
159
	 * 
160
	 * @param  boolean $tags 
161
	 * 
162
	 * @return maxh\Nominatim\Query
163
	 */
164
	public function extraTags($tags = true)
165
	{
166
		$this->query['extratags'] = $tags ? "1" : "0";
167
168
		return $this;
169
	}
170
171
	/**
172
	 * Include a list of alternative names in the results.
173
	 * These may include language variants, references, operator and brand.
174
	 * 
175
	 * @param  boolean $details 
176
	 * 
177
	 * @return maxh\Nominatim\Query
178
	 */
179
	public function nameDetails($details = true)
180
	{
181
		$this->query['namedetails'] = $details ? "1" : "0";
182
183
		return $this;
184
	}
185
186
	/**
187
	 * Returns the URL-encoded query.
188
	 *
189
	 * @return string
190
	 */
191
	public function getQueryString()
192
	{
193
		return http_build_query($this->query);
194
	}
195
196
	// -- Getters & Setters ----------------------------------------------------
197
198
	/**
199
	 * Get path
200
	 * @return string
201
	 */
202
	public function getPath()
203
	{
204
		return $this->path;
205
	}
206
207
	/**
208
	 * Get query
209
	 * @return array
210
	 */
211
	public function getQuery()
212
	{
213
		return $this->query;
214
	}
215
216
	/**
217
	 * Get format
218
	 * @return string
219
	 */
220
	public function getFormat()
221
	{
222
		return $this->format;
223
	}
224
225
	/**
226
	 * Set path
227
	 * @param string $path Name's path of the service
228
	 */
229
	protected function setPath($path)
230
	{
231
		$this->path = $path;
232
	}
233
234
	/**
235
	 * Set query
236
	 * @param array $query Parameter of the query
237
	 */
238
	protected function setQuery($query = array())
239
	{
240
		$this->query = $query;
241
	}
242
243
	/**
244
	 * Set format
245
	 * @param string $format Format returning by the response
246
	 */
247
	protected function setFormat($format)
248
	{
249
		$this->format = $this->query['format'] = $format;
250
	}
251
252
}
253