Completed
Push — master ( b814cf...f1473b )
by Kamran
13:45 queued 06:55
created

Location::getDistrict()   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 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
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
    /** @var string Address to which the detail belong */
13
    private $address = '';
14
15
    /** @var string Latitude of the location */
16
    private $latitude = '';
17
18
    /** @var string Longitude of the location */
19
    private $longitude = '';
20
21
    /** @var string Country of the location */
22
    private $country = '';
23
24
    /** @var string Locality of the location */
25
    private $locality = '';
26
27
    /** @var string District of the location */
28
    private $district = '';
29
30
    /** @var string Postal code of the location */
31
    private $postcode = '';
32
33
    /** @var string Town of the location */
34
    private $town = '';
35
36
    /** @var string Street number */
37
    private $streetNumber = '';
38
39
    /** @var string Street address */
40
    private $streetAddress = '';
41
42
    /** @var boolean Whether the location is valid or not */
43
    private $isValid = true;
44
45
    /**
46
     * Create a new Location object 
47
     * @param string    $address         Address whose detail it is
48
     * @param \stdClass $dataFromService The data retrieved from the Geocoding service
49
     */
50 3
    public function __construct($address, \stdClass $dataFromService)
51
    {
52 3
        $this->address = $address;
53 3
        $this->populateDetail($dataFromService);
54 3
    }
55
56
    /**
57
     * Checks whether the data passed to the class was valid
58
     * @return boolean True if the data is valid and false otherwise
59
     */
60 3
    public function isValid()
61
    {
62 3
        return $this->isValid;
63
    }
64
65
    /**
66
     * Populates the object with the detail from the service
67
     * @param  \stdClass $locationDetail The address detail i.e. which was retrieved from the API
68
     * @return boolean          True if successfuly populated the detail and false otherwise
69
     */
70 3
    private function populateDetail(\stdClass $locationDetail)
71
    {
72
        // The data from the API is returned under the `results` key
73 3
        if (!property_exists($locationDetail, 'results')) {
74 1
            $this->isValid = false;
75 1
            return false;
76
        }
77
78 2
        $this->latitude = $locationDetail->results[0]->geometry->location->lat;
79 2
        $this->longitude = $locationDetail->results[0]->geometry->location->lng;
80
81 2
        foreach ($locationDetail->results[0]->address_components as $component) {
82
83 2
            if (in_array('street_number', $component->types)) {
84 2
                $this->streetNumber = $component->long_name;
85 2
            } else if (in_array('locality', $component->types)) {
86 2
                $this->locality = $component->long_name;
87 2
            } else if (in_array('postal_town', $component->types)) {
88
                $this->town = $component->long_name;
89 2
            } else if (in_array('administrative_area_level_2', $component->types)) {
90 2
                $this->country = $component->long_name;
91 2
            } else if (in_array('country', $component->types)) {
92 2
                $this->country = $component->long_name;
93 2
            } else if (in_array('administrative_area_level_1', $component->types)) {
94 2
                $this->district = $component->long_name;
95 2
            } else if (in_array('postal_code', $component->types)) {
96 2
                $this->postcode = $component->long_name;
97 2
            } else if (in_array('route', $component->types)) {
98 2
                $this->streetAddress = $component->long_name;
99 2
            }
100 2
        }
101
102 2
        return true;
103
    }
104
105
    /**
106
     * Gets the address
107
     * @return string
108
     */
109 2
    public function getAddress($default = '')
110
    {
111 2
        return $this->address ?: $default;
112
    }
113
114
    /**
115
     * Gets the latitude of the location
116
     * @return string
117
     */
118
    public function getLatitude($default = '')
119
    {
120
        return $this->latitude ?: $default;
121
    }
122
123
    /**
124
     * Gets the longitude of the location
125
     * @return string
126
     */
127
    public function getLongitude($default = '')
128
    {
129
        return $this->longitude ?: $default;
130
    }
131
132
    /**
133
     * Gets the country of the location
134
     * @return string
135
     */
136 2
    public function getCountry($default = '')
137
    {
138 2
        return $this->country ?: $default;
139
    }
140
141
    /**
142
     * Gets the locality of the location
143
     * @return string
144
     */
145 2
    public function getLocality($default = '')
146
    {
147 2
        return $this->locality ?: $default;
148
    }
149
150
    /**
151
     * Gets the district of the location
152
     * @return string
153
     */
154 2
    public function getDistrict($default = '')
155
    {
156 2
        return $this->district ?: $default;
157
    }
158
159
    /**
160
     * Gets the post code for the location
161
     * @return string
162
     */
163 2
    public function getPostcode($default = '')
164
    {
165 2
        return $this->postcode ?: $default;
166
    }
167
168
    /**
169
     * Gets the town for the location
170
     * @return string
171
     */
172 1
    public function getTown($default = '')
173
    {
174 1
        return $this->town ?: $default;
175
    }
176
177
    /**
178
     * Gets the street number for the location
179
     * @return string
180
     */
181 2
    public function getStreetNumber($default = '')
182
    {
183 2
        return $this->streetNumber ?: $default;
184
    }
185
186
    /**
187
     * Gets the street address
188
     * @return string
189
     */
190 1
    public function getStreetAddress($default = '')
191
    {
192 1
        return $this->streetAddress ?: $default;
193
    }
194
}
195