Completed
Branch heritage_class (c7b9cc)
by Maxime
02:29
created

Query::email()   A

Complexity

Conditions 1
Paths 1

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 1
eloc 3
nc 1
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
/**
12
 * Class implementing functionality common to requests nominatim.
13
 */
14
class Query implements QueryInterface
15
{
16
	/**
17
	 * Contain the path of the request
18
	 * @var string
19
	 */
20
	protected $path;
21
22
	/**
23
	 * Contain the query for request
24
	 * @var array
25
	 */
26
	protected $query = [];
27
28
	/**
29
	 * Contain the format for decode data returning by the request
30
	 * @var string
31
	 */
32
	protected $format;
33
34
	/**
35
	 * Output polygon format accepted
36
	 * @var array
37
	 */
38
	protected $polygon = ['geojson', 'kml', 'svg', 'text'];
39
40
41
	/**
42
	 * Constuctor
43
	 * @param array $query Default value for this query
44
	 */
45
	public function __construct(array $query = [])
46
	{
47
		if(!isset($query['format']))
48
		{
49
			//Default format
50
			$query['format'] = 'json';
51
		}
52
53
		$this->setQuery($query);
54
		$this->setFormat($query['format']);
55
56
	}
57
58
	// -- Builder methods ------------------------------------------------------
59
60
	/**
61
	 * Format returning by the request.
62
	 *
63
	 * @param  string $format The output format for the request
64
	 *
65
	 * @return maxh\Nominatim\Query
66
	 * @throws maxh\Nominatim\Exceptions\InvalidParameterException if format is not supported
67
	 */
68
	public function format($format)
69
	{
70
		$format = strtolower($format);
71
72
		if(in_array($format, $this->accepteFormat))
0 ignored issues
show
Bug introduced by
The property accepteFormat does not seem to exist. Did you mean format?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
73
		{
74
			$this->query['format'] = $format;
75
			$this->setFormat($format);
76
77
			return $this;
78
		}
79
80
		throw new InvalidParameterException("Format is not supported");
81
	}
82
83
	/**
84
	 * Preferred language order for showing search results, overrides the value
85
	 * specified in the "Accept-Language" HTTP header. Either uses standard
86
	 * rfc2616 accept-language string or a simple comma separated list of
87
	 * language codes.
88
	 *
89
	 * @param  string $language         Preferred language order for showing search results, overrides the value specified in the "Accept-Language" HTTP header.
90
	 * Either uses standard rfc2616 accept-language string or a simple comma separated list of language codes.
91
	 *
92
	 * @return maxh\Nominatim\Query
93
	 */
94
	public function language($language)
95
	{
96
		$this->query['accept-language'] = $language;
97
98
		return $this;
99
	}
100
101
	/**
102
	 * Include a breakdown of the address into elements.
103
	 *
104
	 * @param  boolean $details
105
	 * 
106
	 * @return maxh\Nominatim\Query
107
	 */
108
	public function addressDetails($details = true)
109
	{
110
		$this->query['addressdetails'] = $details ? "1" : "0";
111
112
		return $this;
113
	}
114
115
	/**
116
	 * Include additional information in the result if available
117
	 * 
118
	 * @param  boolean $tags 
119
	 * 
120
	 * @return maxh\Nominatim\Query
121
	 */
122
	public function extraTags($tags = true)
123
	{
124
		$this->query['extratags'] = $tags ? "1" : "0";
125
126
		return $this;
127
	}
128
129
	/**
130
	 * Include a list of alternative names in the results.
131
	 * These may include language variants, references, operator and brand.
132
	 * 
133
	 * @param  boolean $details 
134
	 * 
135
	 * @return maxh\Nominatim\Query
136
	 */
137
	public function nameDetails($details = true)
138
	{
139
		$this->query['namedetails'] = $details ? "1" : "0";
140
141
		return $this;
142
	}
143
144
	/**
145
	 * 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.
146
	 * This information will be kept confidential and only used to contact you in the event of a problem, see Usage Policy for more details.
147
	 * 
148
	 * @param  string $email Address mail
149
	 * 
150
	 * @return maxh\Nominatim\Query
151
	 */
152
	public function email($email)
153
	{
154
		$this->query['email'] = $email;
155
156
		return $this;
157
	}
158
159
	/**
160
	 * Returns the URL-encoded query.
161
	 *
162
	 * @return string
163
	 */
164
	public function getQueryString()
165
	{
166
		return http_build_query($this->query);
167
	}
168
169
	// -- Getters & Setters ----------------------------------------------------
170
171
	/**
172
	 * Get path
173
	 * @return string
174
	 */
175
	public function getPath()
176
	{
177
		return $this->path;
178
	}
179
180
	/**
181
	 * Get query
182
	 * @return array
183
	 */
184
	public function getQuery()
185
	{
186
		return $this->query;
187
	}
188
189
	/**
190
	 * Get format
191
	 * @return string
192
	 */
193
	public function getFormat()
194
	{
195
		return $this->format;
196
	}
197
198
	/**
199
	 * Set path
200
	 * @param string $path Name's path of the service
201
	 */
202
	protected function setPath($path)
203
	{
204
		$this->path = $path;
205
	}
206
207
	/**
208
	 * Set query
209
	 * @param array $query Parameter of the query
210
	 */
211
	protected function setQuery($query = array())
212
	{
213
		$this->query = $query;
214
	}
215
216
	/**
217
	 * Set format
218
	 * @param string $format Format returning by the response
219
	 */
220
	protected function setFormat($format)
221
	{
222
		$this->format = $format;
223
	}
224
225
}
226