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

Query   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 234
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 56%

Importance

Changes 0
Metric Value
wmc 21
c 0
b 0
f 0
lcom 1
cbo 1
dl 0
loc 234
rs 10
ccs 28
cts 50
cp 0.56

15 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 2
A format() 0 12 2
A language() 0 6 1
A addressDetails() 0 6 2
A email() 0 6 1
A polygon() 0 10 2
A extraTags() 0 6 2
A nameDetails() 0 6 2
A getQueryString() 0 4 1
A getPath() 0 4 1
A getQuery() 0 4 1
A getFormat() 0 4 1
A setPath() 0 4 1
A setQuery() 0 4 1
A setFormat() 0 4 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 5
    public function __construct(array $query = [])
54
    {
55 5
        if (!isset($query['format'])) {
56
            //Default format
57 5
            $query['format'] = 'json';
58 5
        }
59
60 5
        $this->setQuery($query);
61 5
        $this->setFormat($query['format']);
62 5
    }
63
64
    // -- Builder methods ------------------------------------------------------
65
66
    /**
67
     * Format returning by the request.
68
     *
69
     * @param  string $format The output format for the request
70
     *
71
     * @return maxh\Nominatim\Query
72
     * @throws maxh\Nominatim\Exceptions\InvalidParameterException if format is not supported
73
     */
74 1
    public function format($format)
75
    {
76 1
        $format = strtolower($format);
77
78 1
        if (in_array($format, $this->accepteFormat)) {
79 1
            $this->setFormat($format);
80
81 1
            return $this;
82
        }
83
84
        throw new InvalidParameterException("Format is not supported");
85
    }
86
87
    /**
88
     * Preferred language order for showing search results, overrides the value
89
     * specified in the "Accept-Language" HTTP header. Either uses standard
90
     * rfc2616 accept-language string or a simple comma separated list of
91
     * language codes.
92
     *
93
     * @param  string $language         Preferred language order for showing search results, overrides the value
94
     *                                  specified in the "Accept-Language" HTTP header. Either uses standard rfc2616
95
     *                                  accept-language string or a simple comma separated list of language codes.
96
     *
97
     * @return maxh\Nominatim\Query
98
     */
99
    public function language($language)
100
    {
101
        $this->query['accept-language'] = $language;
102
103
        return $this;
104
    }
105
106
    /**
107
     * Include a breakdown of the address into elements.
108
     *
109
     * @param  boolean $details
110
     *
111
     * @return maxh\Nominatim\Query
112
     */
113 1
    public function addressDetails($details = true)
114
    {
115 1
        $this->query['addressdetails'] = $details ? "1" : "0";
116
117 1
        return $this;
118
    }
119
120
    /**
121
     * If you are making large numbers of request please include a valid email address or alternatively include your
122
     * email address as part of the User-Agent string. This information will be kept confidential and only used to
123
     * contact you in the event of a problem, see Usage Policy for more details.
124
     *
125
     * @param  string $email Address mail
126
     *
127
     * @return maxh\Nominatim\Query
128
     */
129
    public function email($email)
130
    {
131
        $this->query['email'] = $email;
132
133
        return $this;
134
    }
135
136
    /**
137
     * Output format for the geometry of results
138
     *
139
     * @param  string $polygon
140
     *
141
     * @return maxh\Nominatim\Query
142
     * @throws maxh\Nominatim\Exceptions\InvalidParameterException  if polygon format is not supported
143
     */
144
    public function polygon($polygon)
145
    {
146
        if (in_array($polygon, $this->polygon)) {
147
            $this->query['polygon_' . $polygon] = "1";
148
149
            return $this;
150
        }
151
152
        throw new InvalidParameterException("This polygon format is not supported");
153
    }
154
155
    /**
156
     * Include additional information in the result if available
157
     *
158
     * @param  boolean $tags
159
     *
160
     * @return maxh\Nominatim\Query
161
     */
162
    public function extraTags($tags = true)
163
    {
164
        $this->query['extratags'] = $tags ? "1" : "0";
165
166
        return $this;
167
    }
168
169
    /**
170
     * Include a list of alternative names in the results.
171
     * These may include language variants, references, operator and brand.
172
     *
173
     * @param  boolean $details
174
     *
175
     * @return maxh\Nominatim\Query
176
     */
177
    public function nameDetails($details = true)
178
    {
179
        $this->query['namedetails'] = $details ? "1" : "0";
180
181
        return $this;
182
    }
183
184
    /**
185
     * Returns the URL-encoded query.
186
     *
187
     * @return string
188
     */
189 4
    public function getQueryString()
190
    {
191 4
        return http_build_query($this->query);
192
    }
193
194
    // -- Getters & Setters ----------------------------------------------------
195
196
    /**
197
     * Get path
198
     * @return string
199
     */
200
    public function getPath()
201
    {
202
        return $this->path;
203
    }
204
205
    /**
206
     * Get query
207
     * @return array
208
     */
209 4
    public function getQuery()
210
    {
211 4
        return $this->query;
212
    }
213
214
    /**
215
     * Get format
216
     * @return string
217
     */
218
    public function getFormat()
219
    {
220
        return $this->format;
221
    }
222
223
    /**
224
     * Set path
225
     * @param string $path Name's path of the service
226
     */
227 5
    protected function setPath($path)
228
    {
229 5
        $this->path = $path;
230 5
    }
231
232
    /**
233
     * Set query
234
     * @param array $query Parameter of the query
235
     */
236 5
    protected function setQuery($query = array())
237
    {
238 5
        $this->query = $query;
239 5
    }
240
241
    /**
242
     * Set format
243
     * @param string $format Format returning by the response
244
     */
245 5
    protected function setFormat($format)
246
    {
247 5
        $this->format = $this->query['format'] = $format;
248 5
    }
249
}
250