Completed
Push — master ( c4a119...b5db7c )
by Tobias
01:22
created

AddressBuilder::build()   B

Complexity

Conditions 6
Paths 3

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
dl 0
loc 25
ccs 0
cts 7
cp 0
rs 8.8977
c 0
b 0
f 0
cc 6
nc 3
nop 1
crap 42
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\Exception\InvalidArgument;
16
use Geocoder\Exception\LogicException;
17
18
/**
19
 * A class that builds a Location or any of its subclasses.
20
 *
21
 * @author Tobias Nyholm <[email protected]>
22
 */
23
final class AddressBuilder
24
{
25
    /**
26
     * @var string
27
     */
28
    private $providedBy;
29
30
    /**
31
     * @var Coordinates|null
32
     */
33
    private $coordinates;
34
35
    /**
36
     * @var Bounds|null
37
     */
38
    private $bounds;
39
40
    /**
41
     * @var string|null
42
     */
43
    private $streetNumber;
44
45
    /**
46
     * @var string|null
47
     */
48
    private $streetName;
49
50
    /**
51
     * @var string|null
52
     */
53
    private $locality;
54
55
    /**
56
     * @var string|null
57
     */
58
    private $postalCode;
59
60
    /**
61
     * @var string|null
62
     */
63
    private $subLocality;
64
65
    /**
66
     * @var array
67
     */
68
    private $adminLevels = [];
69
70
    /**
71
     * @var string|null
72
     */
73
    private $country;
74
75
    /**
76
     * @var string|null
77
     */
78
    private $countryCode;
79
80
    /**
81
     * @var string|null
82
     */
83
    private $timezone;
84
85
    /**
86
     * A storage for extra parameters.
87
     *
88
     * @var array
89
     */
90
    private $data = [];
91
92
    /**
93
     * @param string $providedBy
94
     */
95
    public function __construct(string $providedBy)
96
    {
97
        $this->providedBy = $providedBy;
98
    }
99
100
    /**
101
     * @param string $class
102
     *
103
     * @return Address
104
     */
105
    public function build(string $class = Address::class): Address
106
    {
107
        if (!is_a($class, Address::class, true)) {
108
            throw new LogicException('First parameter to LocationBuilder::build must be a class name extending Geocoder\Model\Address');
109
        }
110
111
        $country = null;
112
        if ((null !== $this->country && '' !== $this->country) || (null !== $this->countryCode && '' !== $this->countryCode)) {
113
            $country = new Country($this->country, $this->countryCode);
114
        }
115
116
        return new $class(
117
            $this->providedBy,
118
            new AdminLevelCollection($this->adminLevels),
119
            $this->coordinates,
120
            $this->bounds,
121
            $this->streetNumber,
122
            $this->streetName,
123
            $this->postalCode,
124
            $this->locality,
125
            $this->subLocality,
126
            $country,
127
            $this->timezone
128
        );
129
    }
130
131
    /**
132
     * @param float $south
133
     * @param float $west
134
     * @param float $north
135
     * @param float $east
136
     *
137
     * @return AddressBuilder
138
     */
139
    public function setBounds($south, $west, $north, $east): self
140
    {
141
        try {
142
            $this->bounds = new Bounds($south, $west, $north, $east);
143
        } catch (InvalidArgument $e) {
144
            $this->bounds = null;
145
        }
146
147
        return $this;
148
    }
149
150
    /**
151
     * @param float $latitude
152
     * @param float $longitude
153
     *
154
     * @return AddressBuilder
155
     */
156
    public function setCoordinates($latitude, $longitude): self
157
    {
158
        try {
159
            $this->coordinates = new Coordinates($latitude, $longitude);
160
        } catch (InvalidArgument $e) {
161
            $this->coordinates = null;
162
        }
163
164
        return $this;
165
    }
166
167
    /**
168
     * @param int         $level
169
     * @param string      $name
170
     * @param string|null $code
171
     *
172
     * @return AddressBuilder
173
     */
174
    public function addAdminLevel(int $level, string $name, string $code = null): self
175
    {
176
        $this->adminLevels[] = new AdminLevel($level, $name, $code);
177
178
        return $this;
179
    }
180
181
    /**
182
     * @param string|null $streetNumber
183
     *
184
     * @return AddressBuilder
185
     */
186
    public function setStreetNumber($streetNumber): self
187
    {
188
        $this->streetNumber = $streetNumber;
189
190
        return $this;
191
    }
192
193
    /**
194
     * @param string|null $streetName
195
     *
196
     * @return AddressBuilder
197
     */
198
    public function setStreetName($streetName): self
199
    {
200
        $this->streetName = $streetName;
201
202
        return $this;
203
    }
204
205
    /**
206
     * @param string|null $locality
207
     *
208
     * @return AddressBuilder
209
     */
210
    public function setLocality($locality): self
211
    {
212
        $this->locality = $locality;
213
214
        return $this;
215
    }
216
217
    /**
218
     * @param string|null $postalCode
219
     *
220
     * @return AddressBuilder
221
     */
222
    public function setPostalCode($postalCode): self
223
    {
224
        $this->postalCode = $postalCode;
225
226
        return $this;
227
    }
228
229
    /**
230
     * @param string|null $subLocality
231
     *
232
     * @return AddressBuilder
233
     */
234
    public function setSubLocality($subLocality): self
235
    {
236
        $this->subLocality = $subLocality;
237
238
        return $this;
239
    }
240
241
    /**
242
     * @param array $adminLevels
243
     *
244
     * @return AddressBuilder
245
     */
246
    public function setAdminLevels($adminLevels): self
247
    {
248
        $this->adminLevels = $adminLevels;
249
250
        return $this;
251
    }
252
253
    /**
254
     * @param string|null $country
255
     *
256
     * @return AddressBuilder
257
     */
258
    public function setCountry($country): self
259
    {
260
        $this->country = $country;
261
262
        return $this;
263
    }
264
265
    /**
266
     * @param string|null $countryCode
267
     *
268
     * @return AddressBuilder
269
     */
270
    public function setCountryCode($countryCode): self
271
    {
272
        $this->countryCode = $countryCode;
273
274
        return $this;
275
    }
276
277
    /**
278
     * @param string|null $timezone
279
     *
280
     * @return AddressBuilder
281
     */
282
    public function setTimezone($timezone): self
283
    {
284
        $this->timezone = $timezone;
285
286
        return $this;
287
    }
288
289
    /**
290
     * @param string $name
291
     * @param mixed  $value
292
     *
293
     * @return AddressBuilder
294
     */
295
    public function setValue(string $name, $value): self
296
    {
297
        $this->data[$name] = $value;
298
299
        return $this;
300
    }
301
302
    /**
303
     * @param string     $name
304
     * @param mixed|null $default
305
     *
306
     * @return mixed
307
     */
308
    public function getValue(string $name, $default = null)
309
    {
310
        if ($this->hasValue($name)) {
311
            return $this->data[$name];
312
        }
313
314
        return $default;
315
    }
316
317
    /**
318
     * @param string $name
319
     *
320
     * @return bool
321
     */
322
    public function hasValue(string $name): bool
323
    {
324
        return array_key_exists($name, $this->data);
325
    }
326
}
327