Location::getLocality()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace CodeblogPro\GeoLocationAddress;
6
7
use CodeblogPro\GeoCoordinates\CoordinatesInterface;
8
9
class Location implements LocationInterface
10
{
11
    private ?CoordinatesInterface $coordinates;
12
    private ?Country $country;
13
    private ?Region $region;
14
    private string $streetName;
15
    private string $locality;
16
    private string $postalCode;
17
18 1
    public function __construct(
19
        CoordinatesInterface $coordinates = null,
20
        Country $country = null,
21
        Region $region = null,
22
        string $locality = '',
23
        string $streetName = '',
24
        string $postalCode = ''
25
    ) {
26 1
        $this->coordinates = $coordinates;
27 1
        $this->country = $country;
28 1
        $this->region = $region;
29 1
        $this->locality = $locality;
30 1
        $this->streetName = $streetName;
31 1
        $this->postalCode = $postalCode;
32 1
    }
33
34
    public function getCoordinates(): ?CoordinatesInterface
35
    {
36
        return $this->coordinates;
37
    }
38
39
    public function getCountry(): ?Country
40
    {
41
        return $this->country;
42
    }
43
44
    public function getRegion(): ?Region
45
    {
46
        return $this->region;
47
    }
48
49
    public function getLocality(): string
50
    {
51
        return $this->locality;
52
    }
53
54
    public function getStreetName(): string
55
    {
56
        return $this->streetName;
57
    }
58
59
    public function getPostal(): string
60
    {
61
        return $this->postalCode;
62
    }
63
64
    public function getAddressString(): string
65
    {
66
        $address = '';
67
68
        if (!empty($this->getCountry())) {
69
            $address = $this->getCountry()->getName();
70
        }
71
72
        if (!empty($this->getRegion())) {
73
            $address .= ', ' . $this->getRegion()->getName();
74
        }
75
76
        if (!empty($this->getLocality())) {
77
            $address .= ', ' . $this->getLocality();
78
        }
79
80
        if (!empty($this->getStreetName())) {
81
            $address .= ', ' . $this->getStreetName();
82
        }
83
84
        return $address;
85
    }
86
87
    public function toArray(): array
88
    {
89
        return [
90
            'latitude' => (!empty($this->getCoordinates())) ? $this->getCoordinates()->getLatitude() : null,
91
            'longitude' => (!empty($this->getCoordinates())) ? $this->getCoordinates()->getLongitude() : null,
92
            'country_name' => $this->getCountry()->getName(),
93
            'country_code' => $this->getCountry()->getCode(),
94
            'region_name' => $this->getRegion()->getName(),
95
            'region_code' => $this->getRegion()->getCode(),
96
            'street_name' => $this->getStreetName(),
97
            'postal_code' => $this->getPostal(),
98
            'locality' => $this->getLocality()
99
        ];
100
    }
101
}
102