Completed
Pull Request — master (#12)
by
unknown
02:48
created

Location::getAddress()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 2
eloc 2
nc 2
nop 1
crap 2
1
<?php
2
namespace KamranAhmed\Geocode;
3
4
/**
5
 * Location
6
 *
7
 * Represents the location details obtained from the Geocoding
8
 * Service
9
 */
10
class Location
11
{
12
    /**
13
     * @var string Address to which the detail belong
14
     */
15
    private $address = '';
16
17
    /**
18
     * @var string Latitude of the location
19
     */
20
    private $latitude = '';
21
22
    /**
23
     * @var string Longitude of the location
24
     */
25
    private $longitude = '';
26
27
    /**
28
     * @var string Country of the location
29
     */
30
    private $country = '';
31
32
    /**
33
     * @var string Locality of the location
34
     */
35
    private $locality = '';
36
37
    /**
38
     * @var string District of the location
39
     */
40
    private $district = '';
41
42
    /**
43
     * @var string Postal code of the location
44
     */
45
    private $postcode = '';
46
47
    /**
48
     * @var string Town of the location
49
     */
50
    private $town = '';
51
52
    /**
53
     * @var string Street number
54
     */
55
    private $streetNumber = '';
56
57
    /**
58
     * @var string Street address
59
     */
60
    private $streetAddress = '';
61
62
    /**
63
     * @var string Short Country of the location
64
     */
65
    private $shortCountry = '';
66
67
    /**
68
     * @var string Short Locality of the location
69
     */
70
    private $shortLocality = '';
71
72
    /**
73
     * @var string Short District of the location
74
     */
75
    private $shortDistrict = '';
76
77
    /**
78
     * @var string Short Town of the location
79
     */
80
    private $shortTown = '';
81
82
    /**
83
     * @var string Short Street address
84
     */
85
    private $shortStreetAddress = '';
86
87
    /**
88
     * @var boolean Whether the location is valid or not
89
     */
90
    private $isValid = true;
91
92
    /**
93
     * Create a new Location object
94
     *
95
     * @param string    $address         Address whose detail it is
96
     * @param \stdClass $dataFromService The data retrieved from the Geocoding service
97
     */
98 3
    public function __construct($address, \stdClass $dataFromService)
99
    {
100 3
        $this->address = $address;
101 3
        $this->populateDetail($dataFromService);
102 3
    }
103
104
    /**
105
     * Checks whether the data passed to the class was valid
106
     *
107
     * @return boolean True if the data is valid and false otherwise
108
     */
109 3
    public function isValid()
110
    {
111 3
        return $this->isValid;
112
    }
113
114
    /**
115
     * Populates the object with the detail from the service
116
     *
117
     * @param \stdClass $locationDetail The address detail i.e. which was retrieved from the API
118
     *
119
     * @return boolean          True if successfully populated the detail and false otherwise
120
     */
121 3
    private function populateDetail(\stdClass $locationDetail)
122
    {
123
        // The data from the API is returned under the `results` key
124 3
        if (!property_exists($locationDetail, 'results')) {
125 1
            $this->isValid = false;
126
127 1
            return false;
128
        }
129
130 2
        $this->latitude  = $locationDetail->results[0]->geometry->location->lat;
131 2
        $this->longitude = $locationDetail->results[0]->geometry->location->lng;
132
133 2
        foreach ($locationDetail->results[0]->address_components as $component) {
134 2
            if (in_array('street_number', $component->types)) {
135 2
                $this->streetNumber = $component->long_name;
136 2
            } elseif (in_array('locality', $component->types)) {
137 2
                $this->locality = $component->long_name;
138 2
                $this->shortLocality = $component->short_name;
139 2
            } elseif (in_array('postal_town', $component->types)) {
140
                $this->town = $component->long_name;
141
                $this->shortTown = $component->short_name;
142 2
            } elseif (in_array('administrative_area_level_2', $component->types)) {
143 2
                $this->country = $component->long_name;
144 2
                $this->shortCountry = $component->short_name;
145 2
            } elseif (in_array('country', $component->types)) {
146 2
                $this->country = $component->long_name;
147 2
                $this->shortCountry = $component->short_name;
148 2
            } elseif (in_array('administrative_area_level_1', $component->types)) {
149 2
                $this->district = $component->long_name;
150 2
                $this->shortDistrict = $component->short_name;
151 2
            } elseif (in_array('postal_code', $component->types)) {
152 2
                $this->postcode = $component->long_name;
153 2
            } elseif (in_array('route', $component->types)) {
154 2
                $this->streetAddress = $component->long_name;
155 2
                $this->shortStreetAddress = $component->short_name;
156
            }
157
        }
158
159 2
        return true;
160
    }
161
162
    /**
163
     * Gets the address
164
     *
165
     * @param string $default
166
     *
167
     * @return string
168
     */
169 2
    public function getAddress($default = '')
170
    {
171 2
        return $this->address ?: $default;
172
    }
173
174
    /**
175
     * Gets the latitude of the location
176
     *
177
     * @param string $default
178
     *
179
     * @return string
180
     */
181
    public function getLatitude($default = '')
182
    {
183
        return $this->latitude ?: $default;
184
    }
185
186
    /**
187
     * Gets the longitude of the location
188
     *
189
     * @param string $default
190
     *
191
     * @return string
192
     */
193
    public function getLongitude($default = '')
194
    {
195
        return $this->longitude ?: $default;
196
    }
197
198
    /**
199
     * Gets the country of the location
200
     *
201
     * @param string $default
202
     *
203
     * @return string
204
     */
205 2
    public function getCountry($default = '')
206
    {
207 2
        return $this->country ?: $default;
208
    }
209
210
    /**
211
     * Gets the short country of the location
212
     *
213
     * @param string $default
214
     *
215
     * @return string
216
     */
217 2
    public function getShortCountry($default = '')
218
    {
219 2
        return $this->shortCountry ?: $default;
220
    }
221
222
    /**
223
     * Gets the locality of the location
224
     *
225
     * @param string $default
226
     *
227
     * @return string
228
     */
229 2
    public function getLocality($default = '')
230
    {
231 2
        return $this->locality ?: $default;
232
    }
233
    /**
234
     * Gets the short locality of the location
235
     *
236
     * @param string $default
237
     *
238
     * @return string
239
     */
240 2
    public function getShortLocality($default = '')
241
    {
242 2
        return $this->shortLocality ?: $default;
243
    }
244
245
    /**
246
     * Gets the district of the location
247
     *
248
     * @param string $default
249
     *
250
     * @return string
251
     */
252 2
    public function getDistrict($default = '')
253
    {
254 2
        return $this->district ?: $default;
255
    }
256
    /**
257
     * Gets the short district of the location
258
     *
259
     * @param string $default
260
     *
261
     * @return string
262
     */
263 2
    public function getShortDistrict($default = '')
264
    {
265 2
        return $this->shortDistrict ?: $default;
266
    }
267
268
    /**
269
     * Gets the post code for the location
270
     *
271
     * @param string $default
272
     *
273
     * @return string
274
     */
275 2
    public function getPostcode($default = '')
276
    {
277 2
        return $this->postcode ?: $default;
278
    }
279
280
    /**
281
     * Gets the town for the location
282
     *
283
     * @param string $default
284
     *
285
     * @return string
286
     */
287 1
    public function getTown($default = '')
288
    {
289 1
        return $this->town ?: $default;
290
    }
291
    /**
292
     * Gets the short town of the location
293
     *
294
     * @param string $default
295
     *
296
     * @return string
297
     */
298
    public function getShortTown($default = '')
299
    {
300
        return $this->shortTown ?: $default;
301
    }
302
303
    /**
304
     * Gets the street number for the location
305
     *
306
     * @param string $default
307
     *
308
     * @return string
309
     */
310 2
    public function getStreetNumber($default = '')
311
    {
312 2
        return $this->streetNumber ?: $default;
313
    }
314
315
    /**
316
     * Gets the street address
317
     *
318
     * @param string $default
319
     *
320
     * @return string
321
     */
322 1
    public function getStreetAddress($default = '')
323
    {
324 1
        return $this->streetAddress ?: $default;
325
    }
326
    /**
327
     * Gets the short street address of the location
328
     *
329
     * @param string $default
330
     *
331
     * @return string
332
     */
333 1
    public function getShortStreetAddress($default = '')
334
    {
335 1
        return $this->shortStreetAddress ?: $default;
336
    }
337
}
338