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

Search::format()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 7

Duplication

Lines 14
Ratio 100 %

Importance

Changes 0
Metric Value
dl 14
loc 14
c 0
b 0
f 0
rs 9.4285
cc 2
eloc 7
nc 2
nop 1

1 Method

Rating   Name   Duplication   Size   Complexity  
A Search::street() 0 6 1
1
<?php
2
/**
3
 * Class Search
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
 * Searches a OSM nominatim service for places.
15
 *
16
 * @see http://wiki.openstreetmap.org/wiki/Nominatim
17
 */
18
class Search extends Query
19
{
20
21
	 /**
22
	 * Output format accepted
23
	 * @var array
24
	 */
25
	public $accepteFormat = ['html', 'xml', 'json', 'jsonv2'];
26
27
28
	/**
29
	 * Constuctor
30
	 * @param array $query Default value for this query
31
	 */
32
	public function __construct(array $query = [])
33
	{
34
		parent::__construct();
35
36
		$this->setPath('search');
37
38
	}
39
40
	// -- Builder methods ------------------------------------------------------
41
42
	/**
43
	 * Query string to search for.
44
	 *
45
	 * @param  string $query The query
46
	 *
47
	 * @return maxh\Nominatim\Search
48
	 */
49
	public function query($query)
50
	{
51
		$this->query['q'] = $query;
52
53
		return $this;
54
	}
55
56
	/**
57
	 * Street to search for.
58
	 *
59
	 * Do not combine with query().
60
	 *
61
	 * @param  string $street The street
62
	 *
63
	 * @return maxh\Nominatim\Search
64
	 */
65
	public function street($street)
66
	{
67
		$this->query['street'] = $street;
68
69
		return $this;
70
	}
71
72
	/**
73
	 * City to search for (experimental).
74
	 *
75
	 * Do not combine with query().
76
	 * 
77
	 * @param  string $city The city
78
	 *
79
	 * @return maxh\Nominatim\Search
80
	 */
81
	public function city($city)
82
	{
83
		$this->query['city'] = $city;
84
85
		return $this;
86
	}
87
88
	/**
89
	 * County to search for.
90
	 *
91
	 * Do not combine with query().
92
	 * 
93
	 * @param  string $county The county
94
	 *
95
	 * @return maxh\Nominatim\Search
96
	 */
97
	public function county($county)
98
	{
99
		$this->query['county'] = $county;
100
101
		return $this;
102
	}
103
104
	/**
105
	 * State to search for.
106
	 *
107
	 * Do not combine with query().
108
	 *
109
	 * @param  string $state The state
110
	 *
111
	 * @return maxh\Nominatim\Search
112
	 */
113
	public function state($state)
114
	{
115
		$this->query['state'] = $state;
116
117
		return $this;
118
	}
119
120
	/**
121
	 * Country to search for.
122
	 *
123
	 * Do not combine with query().
124
	 *
125
	 * @param  string $country The country
126
	 *
127
	 * @return maxh\Nominatim\Search
128
	 */
129
	public function country($country)
130
	{
131
		$this->query['country'] = $country;
132
133
		return $this;
134
	}
135
136
	/**
137
	 * Postal code to search for (experimental).
138
	 *
139
	 * Do not combine with query().
140
	 *
141
	 * @param  integer $postalCode The postal code
142
	 *
143
	 * @return maxh\Nominatim\Search
144
	 */
145
	public function postalCode($postalCode)
146
	{
147
		$this->query['postalcode'] = $postalCode;
148
149
		return $this;
150
	}
151
152
	/**
153
	 * Limit search results to a specific country (or a list of countries).
154
	 *
155
	 * <countrycode> should be the ISO 3166-1alpha2 code, e.g. gb for the United
156
	 * Kingdom, de for Germany, etc.
157
	 *
158
	 * @param  string $countrycode The country code
159
	 *
160
	 * @return maxh\Nominatim\Search
161
	 * @throws maxh\Nominatim\Exceptions\InvalidParameterException if country code is invalid
162
	 */
163
	public function countryCode($countrycode)
164
	{
165
		if (!preg_match('/^[a-z]{2}$/i', $countrycode)) {
166
			throw new InvalidParameterException("Invalid country code: \"$countrycode\"");
167
		}
168
169
		if (!isset($this->query['countrycode'])) {
170
			$this->query['countrycode'] = $countrycode;
171
		} else {
172
			$this->query['countrycode'] .= "," . $countrycode;
173
		}
174
175
		return $this;
176
	}
177
178
	/**
179
	 * The preferred area to find search results
180
	 * 
181
	 * @param  string $left   Left of the area
182
	 * @param  string $top	Top of the area
183
	 * @param  string $right  Right of the area
184
	 * @param  string $bottom Bottom of the area
185
	 * 
186
	 * @return maxh\Nominatim\Search
187
	 */
188
	public function viewBox($left, $top, $right, $bottom)
189
	{
190
		$this->query['viewbox'] = $left . ',' . $top . ',' . $right . ',' . $bottom;
191
192
		return $this;
193
	}
194
195
	/**
196
	 * If you do not want certain openstreetmap objects to appear in the search results.
197
	 * 
198
	 * @return maxh\Nominatim\Search
199
	 * @throws maxh\Nominatim\Exceptions\InvalidParameterException  if no place id
200
	 */
201
	public function exludePlaceIds()
202
	{
203
		$args = func_get_args();
204
205
		if(count($args) > 0)
206
		{
207
			$this->query['exclude_place_ids'] = implode(', ', $args);
208
209
			return $this;
210
		}
211
212
		throw new InvalidParameterException("No place id in parameter");
213
	}
214
215
	/**
216
	 * Limit the number of returned results
217
	 * 
218
	 * @param  integer $limit 
219
	 * 
220
	 * @return maxh\Nominatim\Search
221
	 */
222
	public function limit($limit)
223
	{
224
		$this->query['limit'] = strval($limit);
225
226
		return $this;
227
	}
228
229
	/**
230
	 * Output format for the geometry of results
231
	 * 
232
	 * @param  string $polygon
233
	 * 
234
	 * @return maxh\Nominatim\Reverse
235
	 * @throws maxh\Nominatim\Exceptions\InvalidParameterException  if polygon format is not supported
236
	 */
237 View Code Duplication
	public function polygon($polygon)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
238
	{
239
		if(in_array($polygon, $this->polygon))
240
		{
241
			$this->query['polygon_'.$polygon] = "1";
242
243
			return $this;
244
		}
245
246
		throw new InvalidParameterException("This polygon format is not supported");
247
	}
248
249
250
}
251