Completed
Push — master ( fd983c...6ef91a )
by Maxime
04:57
created

Search   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 205
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 38.3%

Importance

Changes 0
Metric Value
wmc 15
c 0
b 0
f 0
lcom 1
cbo 2
dl 0
loc 205
rs 10
ccs 18
cts 47
cp 0.383

12 Methods

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