Completed
Pull Request — master (#3)
by Robin
02:10
created

Address::guardAgainstInvalidLatLong()   A

Complexity

Conditions 5
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 4
rs 9.6111
c 0
b 0
f 0
cc 5
nc 2
nop 2
1
<?php
2
3
namespace Communibase\Entity;
4
5
use Communibase\CommunibaseId;
6
use Communibase\DataBag;
7
8
/**
9
 * Communibase Address
10
 *
11
 * @author Kingsquare ([email protected])
12
 * @copyright Copyright (c) Kingsquare BV (http://www.kingsquare.nl)
13
 */
14
final class Address
15
{
16
    /**
17
     * @var DataBag;
18
     */
19
    private $dataBag;
20
21
    /**
22
     * @param array $addressData
23
     */
24
    private function __construct(array $addressData)
25
    {
26
        $this->dataBag = DataBag::create();
27
        if ($addressData === []) {
28
            return;
29
        }
30
        $this->dataBag->addEntityData('address', $addressData);
31
    }
32
33
    /**
34
     * @param array|null $addressData
35
     *
36
     * @return Address
37
     */
38
    public static function fromAddressData(array $addressData = null)
39
    {
40
        if ($addressData === null) {
41
            $addressData = [];
42
        }
43
        return new self($addressData);
44
    }
45
46
    /**
47
     * @return string
48
     */
49
    public function getProperty()
50
    {
51
        return $this->dataBag->get('address.property');
52
    }
53
54
    /**
55
     * @param string $property
56
     */
57
    public function setProperty($property)
58
    {
59
        $this->dataBag->set('address.property', $property);
60
    }
61
62
    /**
63
     * @return string
64
     */
65
    public function getStreet()
66
    {
67
        return trim((string)$this->dataBag->get('address.street'));
68
    }
69
70
    /**
71
     * @param string $street
72
     */
73
    public function setStreet($street)
74
    {
75
        $this->dataBag->set('address.street', (string)$street);
76
    }
77
78
    /**
79
     * @return string
80
     */
81
    public function getStreetNumber()
82
    {
83
        return $this->dataBag->get('address.streetNumber');
84
    }
85
86
    /**
87
     * @param string $streetNumber
88
     */
89
    public function setStreetNumber($streetNumber)
90
    {
91
        $this->dataBag->set('address.streetNumber', (string)$streetNumber);
92
    }
93
94
    /**
95
     * @return string
96
     */
97
    public function getStreetNumberAddition()
98
    {
99
        return $this->dataBag->get('address.streetNumberAddition');
100
    }
101
102
    /**
103
     * @param string $streetNumberAddition
104
     */
105
    public function setStreetNumberAddition($streetNumberAddition)
106
    {
107
        $this->dataBag->set('address.streetNumberAddition', (string)$streetNumberAddition);
108
    }
109
110
    /**
111
     * @return string
112
     */
113
    public function getZipcode()
114
    {
115
        return trim((string)$this->dataBag->get('address.zipcode'));
116
    }
117
118
    /**
119
     * @param string $zipcode
120
     */
121
    public function setZipcode($zipcode)
122
    {
123
        $this->dataBag->set('address.zipcode', (string)$zipcode);
124
    }
125
126
    /**
127
     * @return string
128
     */
129
    public function getCity()
130
    {
131
        return trim((string)$this->dataBag->get('address.city'));
132
    }
133
134
    /**
135
     * @param string $city
136
     */
137
    public function setCity($city)
138
    {
139
        $this->dataBag->set('address.city', (string)$city);
140
    }
141
142
    /**
143
     * @param string $default
144
     *
145
     * @return string
146
     */
147
    public function getCountryCode($default = 'NL')
148
    {
149
        return trim((string)$this->dataBag->get('address.countryCode', $default));
150
    }
151
152
    /**
153
     * @param string $countryCode
154
     */
155
    public function setCountryCode($countryCode)
156
    {
157
        $this->dataBag->set('address.countryCode', (string)$countryCode);
158
    }
159
160
    /**
161
     * @return string
162
     */
163
    public function getType()
164
    {
165
        return trim((string)$this->dataBag->get('address.type'));
166
    }
167
168
    /**
169
     * @param string $type
170
     */
171
    public function setType($type)
172
    {
173
        $this->dataBag->set('address.type', (string)$type);
174
    }
175
176
    /**
177
     * @return CommunibaseId
178
     */
179
    public function getId()
180
    {
181
        return CommunibaseId::fromString($this->dataBag->get('address._id'));
182
    }
183
184
    public function __toString()
185
    {
186
        return $this->toString();
187
    }
188
189
    /**
190
     * @param bool $singleLine
191
     *
192
     * @return string
193
     */
194
    public function toString($singleLine = true)
195
    {
196
        if ($this->getState() === null) {
197
            return '';
198
        }
199
        $lines = [
200
            array_filter([$this->getStreet(), $this->getStreetNumber(), $this->getStreetNumberAddition()]),
201
            array_filter([$this->getZipcode(), $this->getCity()]),
202
        ];
203
204
        if ($singleLine) {
205
            return implode(', ', array_filter([
206
                implode(' ', $lines[0]),
207
                implode(', ', $lines[1]),
208
            ]));
209
        }
210
        return implode(PHP_EOL, array_filter([
211
            implode(' ', $lines[0]),
212
            implode(' ', $lines[1]),
213
        ]));
214
    }
215
216
    /**
217
     * @return float[]|null
218
     */
219
    public function getGeoLocation()
220
    {
221
        $lat = $this->dataBag->get('address.latitude');
222
        $lng = $this->dataBag->get('address.longitude');
223
        if (!isset($lat, $lng)) {
224
            return null;
225
        }
226
        return [
227
            'lat' => (float)$lat,
228
            'lng' => (float)$lng,
229
        ];
230
    }
231
232
    /**
233
     * @param float $latitude
234
     * @param float $longitude
235
     */
236
    public function setGeoLocation($latitude, $longitude)
237
    {
238
        $latitude = (float)$latitude;
239
        $longitude = (float)$longitude;
240
        $this->guardAgainstInvalidLatLong($latitude, $longitude);
241
242
        $this->dataBag->set('address.latitude', $latitude);
243
        $this->dataBag->set('address.longitude', $longitude);
244
    }
245
246
    /**
247
     * @return bool
248
     */
249
    public function isEmpty()
250
    {
251
        return empty(array_filter([
252
            $this->getStreet(),
253
            $this->getStreetNumber(),
254
            $this->getZipcode(),
255
            $this->getCity()
256
        ]));
257
    }
258
259
    /**
260
     * @return array|null
261
     */
262
    public function getState()
263
    {
264
        if ($this->isEmpty()) {
265
            return null;
266
        }
267
        return $this->dataBag->getState('address');
268
    }
269
270
    /**
271
     * @param float $latitude
272
     * @param float $longitude
273
     */
274
    private function guardAgainstInvalidLatLong($latitude, $longitude)
275
    {
276
        if ($latitude < -90 || $latitude > 90 || $longitude < -180 || $longitude > 180) {
277
            throw new \UnexpectedValueException(\sprintf('Invalid latitude/longitude: %s, %s', $latitude, $longitude));
278
        }
279
    }
280
}
281