Completed
Push — master ( 272924...57ae86 )
by Tobias
02:12
created

Address::createCountry()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 4
nc 2
nop 2
crap 3
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Geocoder package.
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * @license    MIT License
11
 */
12
13
namespace Geocoder\Model;
14
15
use Geocoder\Location;
16
17
/**
18
 * @author William Durand <[email protected]>
19
 */
20
class Address implements Location
21
{
22
    /**
23
     * @var Coordinates|null
24
     */
25
    private $coordinates;
26
27
    /**
28
     * @var Bounds|null
29
     */
30
    private $bounds;
31
32
    /**
33
     * @var string|int|null
34
     */
35
    private $streetNumber;
36
37
    /**
38
     * @var string|null
39
     */
40
    private $streetName;
41
42
    /**
43
     * @var string|null
44
     */
45
    private $subLocality;
46
47
    /**
48
     * @var string|null
49
     */
50
    private $locality;
51
52
    /**
53
     * @var string|null
54
     */
55
    private $postalCode;
56
57
    /**
58
     * @var AdminLevelCollection
59
     */
60
    private $adminLevels;
61
62
    /**
63
     * @var Country|null
64
     */
65
    private $country;
66
67
    /**
68
     * @var string|null
69
     */
70
    private $timezone;
71
72
    /**
73
     * @var string
74
     */
75
    private $providedBy;
76
77
    /**
78
     * @param string               $providedBy
79
     * @param AdminLevelCollection $adminLevels
80
     * @param Coordinates|null     $coordinates
81
     * @param Bounds|null          $bounds
82
     * @param string|null          $streetNumber
83
     * @param string|null          $streetName
84
     * @param string|null          $postalCode
85
     * @param string|null          $locality
86
     * @param string|null          $subLocality
87
     * @param Country|null         $country
88
     * @param string|null          $timezone
89
     */
90 37
    public function __construct(
91
        string $providedBy,
92
        AdminLevelCollection $adminLevels,
93
        Coordinates $coordinates = null,
94
        Bounds $bounds = null,
95
        string $streetNumber = null,
96
        string $streetName = null,
97
        string $postalCode = null,
98
        string $locality = null,
99
        string $subLocality = null,
100
        Country $country = null,
101
        string $timezone = null
102
    ) {
103 37
        $this->providedBy = $providedBy;
104 37
        $this->adminLevels = $adminLevels;
105 37
        $this->coordinates = $coordinates;
106 37
        $this->bounds = $bounds;
107 37
        $this->streetNumber = $streetNumber;
108 37
        $this->streetName = $streetName;
109 37
        $this->postalCode = $postalCode;
110 37
        $this->locality = $locality;
111 37
        $this->subLocality = $subLocality;
112 37
        $this->country = $country;
113 37
        $this->timezone = $timezone;
114 37
    }
115
116
    /**
117
     * @return string
118
     */
119 2
    public function getProvidedBy(): string
120
    {
121 2
        return $this->providedBy;
122
    }
123
124
    /**
125
     * {@inheritdoc}
126
     */
127 20
    public function getCoordinates()
128
    {
129 20
        return $this->coordinates;
130
    }
131
132
    /**
133
     * {@inheritdoc}
134
     */
135 12
    public function getBounds()
136
    {
137 12
        return $this->bounds;
138
    }
139
140
    /**
141
     * {@inheritdoc}
142
     */
143 15
    public function getStreetNumber()
144
    {
145 15
        return $this->streetNumber;
146
    }
147
148
    /**
149
     * {@inheritdoc}
150
     */
151 15
    public function getStreetName()
152
    {
153 15
        return $this->streetName;
154
    }
155
156
    /**
157
     * {@inheritdoc}
158
     */
159 15
    public function getLocality()
160
    {
161 15
        return $this->locality;
162
    }
163
164
    /**
165
     * {@inheritdoc}
166
     */
167 15
    public function getPostalCode()
168
    {
169 15
        return $this->postalCode;
170
    }
171
172
    /**
173
     * {@inheritdoc}
174
     */
175 15
    public function getSubLocality()
176
    {
177 15
        return $this->subLocality;
178
    }
179
180
    /**
181
     * {@inheritdoc}
182
     */
183 15
    public function getAdminLevels(): AdminLevelCollection
184
    {
185 15
        return $this->adminLevels;
186
    }
187
188
    /**
189
     * {@inheritdoc}
190
     */
191 31
    public function getCountry()
192
    {
193 31
        return $this->country;
194
    }
195
196
    /**
197
     * {@inheritdoc}
198
     */
199 15
    public function getTimezone()
200
    {
201 15
        return $this->timezone;
202
    }
203
204
    /**
205
     * Create an Address with an array. Useful for testing.
206
     *
207
     * @param array $data
208
     *
209
     * @return static
210
     */
211 36
    public static function createFromArray(array $data)
212
    {
213
        $defaults = [
214 36
            'providedBy' => 'n/a',
215
            'latitude' => null,
216
            'longitude' => null,
217
            'bounds' => [
218
                'south' => null,
219
                'west' => null,
220
                'north' => null,
221
                'east' => null,
222
            ],
223
            'streetNumber' => null,
224
            'streetName' => null,
225
            'locality' => null,
226
            'postalCode' => null,
227
            'subLocality' => null,
228
            'adminLevels' => [],
229
            'country' => null,
230
            'countryCode' => null,
231
            'timezone' => null,
232
        ];
233
234 36
        $data = array_merge($defaults, $data);
235
236 36
        $adminLevels = [];
237 36
        foreach ($data['adminLevels'] as $adminLevel) {
238 6
            if (empty($adminLevel['level'])) {
239
                continue;
240
            }
241
242 6
            $name = $adminLevel['name'] ?? $adminLevel['code'] ?? null;
243 6
            if (empty($name)) {
244
                continue;
245
            }
246
247 6
            $adminLevels[] = new AdminLevel($adminLevel['level'], $name, $adminLevel['code'] ?? null);
248
        }
249
250 36
        return new static(
251 36
            $data['providedBy'],
252 36
            new AdminLevelCollection($adminLevels),
253 36
            self::createCoordinates(
254 36
                $data['latitude'],
255 36
                $data['longitude']
256
            ),
257 36
            self::createBounds(
258 36
                $data['bounds']['south'],
259 36
                $data['bounds']['west'],
260 36
                $data['bounds']['north'],
261 36
                $data['bounds']['east']
262
            ),
263 36
            $data['streetNumber'],
264 36
            $data['streetName'],
265 36
            $data['postalCode'],
266 36
            $data['locality'],
267 36
            $data['subLocality'],
268 36
            self::createCountry($data['country'], $data['countryCode']),
269 36
            $data['timezone']
270
        );
271
    }
272
273
    /**
274
     * @param float $latitude
275
     * @param float $longitude
276
     *
277
     * @return Coordinates|null
278
     */
279 36
    private static function createCoordinates($latitude, $longitude)
280
    {
281 36
        if (null === $latitude || null === $longitude) {
282 23
            return null;
283
        }
284
285 13
        return new Coordinates($latitude, $longitude);
286
    }
287
288
    /**
289
     * @param string|null $name
290
     * @param string|null $code
291
     *
292
     * @return Country|null
293
     */
294 36
    private static function createCountry($name, $code)
295
    {
296 36
        if (null === $name && null === $code) {
297 30
            return null;
298
        }
299
300 6
        return new Country($name, $code);
301
    }
302
303
    /**
304
     * @param float $south
305
     * @param float $west
306
     * @param float $north
307
     *
308
     * @return Bounds|null
309
     */
310 36
    private static function createBounds($south, $west, $north, $east)
311
    {
312 36
        if (null === $south || null === $west || null === $north || null === $east) {
313 29
            return null;
314
        }
315
316 7
        return new Bounds($south, $west, $north, $east);
317
    }
318
319
    /**
320
     * {@inheritdoc}
321
     */
322 16
    public function toArray(): array
323
    {
324 16
        $adminLevels = [];
325 16
        foreach ($this->adminLevels as $adminLevel) {
326 2
            $adminLevels[$adminLevel->getLevel()] = [
327 2
                'name' => $adminLevel->getName(),
328 2
                'code' => $adminLevel->getCode(),
329 2
                'level' => $adminLevel->getLevel(),
330
            ];
331
        }
332
333 16
        $lat = null;
334 16
        $lon = null;
335 16
        if (null !== $coordinates = $this->getCoordinates()) {
336 11
            $lat = $coordinates->getLatitude();
337 11
            $lon = $coordinates->getLongitude();
338
        }
339
340 16
        $countryName = null;
341 16
        $countryCode = null;
342 16
        if (null !== $country = $this->getCountry()) {
343 4
            $countryName = $country->getName();
344 4
            $countryCode = $country->getCode();
345
        }
346
347
        $noBounds = [
348 16
            'south' => null,
349
            'west' => null,
350
            'north' => null,
351
            'east' => null,
352
        ];
353
354
        return [
355 16
            'providedBy' => $this->providedBy,
356 16
            'latitude' => $lat,
357 16
            'longitude' => $lon,
358 16
            'bounds' => null !== $this->bounds ? $this->bounds->toArray() : $noBounds,
359 16
            'streetNumber' => $this->streetNumber,
360 16
            'streetName' => $this->streetName,
361 16
            'postalCode' => $this->postalCode,
362 16
            'locality' => $this->locality,
363 16
            'subLocality' => $this->subLocality,
364 16
            'adminLevels' => $adminLevels,
365 16
            'country' => $countryName,
366 16
            'countryCode' => $countryCode,
367 16
            'timezone' => $this->timezone,
368
        ];
369
    }
370
}
371