GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 235d7f...9d89b3 )
by Tobias
02:58
created

Address::getPostalCode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
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
    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
        $this->providedBy = $providedBy;
104
        $this->adminLevels = $adminLevels;
105
        $this->coordinates = $coordinates;
106
        $this->bounds = $bounds;
107
        $this->streetNumber = $streetNumber;
108
        $this->streetName = $streetName;
109
        $this->postalCode = $postalCode;
110
        $this->locality = $locality;
111
        $this->subLocality = $subLocality;
112
        $this->country = $country;
113
        $this->timezone = $timezone;
114
    }
115
116
    /**
117
     * @return string
118
     */
119
    public function getProvidedBy(): string
120
    {
121
        return $this->providedBy;
122
    }
123
124
    /**
125
     * {@inheritdoc}
126
     */
127
    public function getCoordinates()
128
    {
129
        return $this->coordinates;
130
    }
131
132
    /**
133
     * {@inheritdoc}
134
     */
135
    public function getBounds()
136
    {
137
        return $this->bounds;
138
    }
139
140
    /**
141
     * {@inheritdoc}
142
     */
143
    public function getStreetNumber()
144
    {
145
        return $this->streetNumber;
146
    }
147
148
    /**
149
     * {@inheritdoc}
150
     */
151
    public function getStreetName()
152
    {
153
        return $this->streetName;
154
    }
155
156
    /**
157
     * {@inheritdoc}
158
     */
159
    public function getLocality()
160
    {
161
        return $this->locality;
162
    }
163
164
    /**
165
     * {@inheritdoc}
166
     */
167
    public function getPostalCode()
168
    {
169
        return $this->postalCode;
170
    }
171
172
    /**
173
     * {@inheritdoc}
174
     */
175
    public function getSubLocality()
176
    {
177
        return $this->subLocality;
178
    }
179
180
    /**
181
     * {@inheritdoc}
182
     */
183
    public function getAdminLevels(): AdminLevelCollection
184
    {
185
        return $this->adminLevels;
186
    }
187
188
    /**
189
     * {@inheritdoc}
190
     */
191
    public function getCountry()
192
    {
193
        return $this->country;
194
    }
195
196
    /**
197
     * {@inheritdoc}
198
     */
199
    public function getTimezone()
200
    {
201
        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
    public static function createFromArray(array $data)
212
    {
213
        $defaults = [
214
            '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
        $data = array_merge($defaults, $data);
235
236
        $adminLevels = [];
237
        foreach ($data['adminLevels'] as $adminLevel) {
238
            if (empty($adminLevel['level'])) {
239
                continue;
240
            }
241
242
            $name = $adminLevel['name'] ?? $adminLevel['code'] ?? null;
243
            if (empty($name)) {
244
                continue;
245
            }
246
247
            $adminLevels[] = new AdminLevel($adminLevel['level'], $name, $adminLevel['code'] ?? null);
248
        }
249
250
        return new static(
251
            $data['providedBy'],
252
            new AdminLevelCollection($adminLevels),
253
            self::createCoordinates(
254
                $data['latitude'],
255
                $data['longitude']
256
            ),
257
            self::createBounds(
258
                $data['bounds']['south'],
259
                $data['bounds']['west'],
260
                $data['bounds']['north'],
261
                $data['bounds']['east']
262
            ),
263
            $data['streetNumber'],
264
            $data['streetName'],
265
            $data['postalCode'],
266
            $data['locality'],
267
            $data['subLocality'],
268
            self::createCountry($data['country'], $data['countryCode']),
269
            $data['timezone']
270
        );
271
    }
272
273
    /**
274
     * @param float $latitude
275
     * @param float $longitude
276
     *
277
     * @return Coordinates|null
278
     */
279
    private static function createCoordinates($latitude, $longitude)
280
    {
281
        if (null === $latitude || null === $longitude) {
282
            return null;
283
        }
284
285
        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
    private static function createCountry($name, $code)
295
    {
296
        if (null === $name && null === $code) {
297
            return null;
298
        }
299
300
        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
    private static function createBounds($south, $west, $north, $east)
311
    {
312
        if (null === $south || null === $west || null === $north || null === $east) {
313
            return null;
314
        }
315
316
        return new Bounds($south, $west, $north, $east);
317
    }
318
319
    /**
320
     * {@inheritdoc}
321
     */
322
    public function toArray(): array
323
    {
324
        $adminLevels = [];
325
        foreach ($this->adminLevels as $adminLevel) {
326
            $adminLevels[$adminLevel->getLevel()] = [
327
                'name' => $adminLevel->getName(),
328
                'code' => $adminLevel->getCode(),
329
                'level' => $adminLevel->getLevel(),
330
            ];
331
        }
332
333
        $lat = null;
334
        $lon = null;
335
        if (null !== $coordinates = $this->getCoordinates()) {
336
            $lat = $coordinates->getLatitude();
337
            $lon = $coordinates->getLongitude();
338
        }
339
340
        $countryName = null;
341
        $countryCode = null;
342
        if (null !== $country = $this->getCountry()) {
343
            $countryName = $country->getName();
344
            $countryCode = $country->getCode();
345
        }
346
347
        $noBounds = [
348
            'south' => null,
349
            'west' => null,
350
            'north' => null,
351
            'east' => null,
352
        ];
353
354
        return [
355
            'providedBy' => $this->providedBy,
356
            'latitude' => $lat,
357
            'longitude' => $lon,
358
            'bounds' => null !== $this->bounds ? $this->bounds->toArray() : $noBounds,
359
            'streetNumber' => $this->streetNumber,
360
            'streetName' => $this->streetName,
361
            'postalCode' => $this->postalCode,
362
            'locality' => $this->locality,
363
            'subLocality' => $this->subLocality,
364
            'adminLevels' => $adminLevels,
365
            'country' => $countryName,
366
            'countryCode' => $countryCode,
367
            'timezone' => $this->timezone,
368
        ];
369
    }
370
}
371