Skip to content

Commit c0faf01

Browse files
authored
Added Address::ProvidedBy (#654)
* Added Address::ProvidedBy * Bugfix * cs * fixes * cs
1 parent 97c3c5e commit c0faf01

File tree

7 files changed

+96
-38
lines changed

7 files changed

+96
-38
lines changed

Location.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,4 +106,11 @@ public function getTimezone();
106106
* @return array
107107
*/
108108
public function toArray(): array;
109+
110+
/**
111+
* The name of the provider that created this Location.
112+
*
113+
* @return string
114+
*/
115+
public function getProvidedBy(): string;
109116
}

Model/Address.php

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -70,41 +70,57 @@ class Address implements Location
7070
private $timezone;
7171

7272
/**
73-
* @param Coordinates|null $coordinates
74-
* @param Bounds|null $bounds
75-
* @param string|null $streetNumber
76-
* @param string|null $streetName
77-
* @param string|null $postalCode
78-
* @param string|null $locality
79-
* @param string|null $subLocality
80-
* @param AdminLevelCollection|null $adminLevels
81-
* @param Country|null $country
82-
* @param string|null $timezone
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
8389
*/
8490
public function __construct(
91+
string $providedBy,
92+
AdminLevelCollection $adminLevels,
8593
Coordinates $coordinates = null,
8694
Bounds $bounds = null,
8795
string $streetNumber = null,
8896
string $streetName = null,
8997
string $postalCode = null,
9098
string $locality = null,
9199
string $subLocality = null,
92-
AdminLevelCollection $adminLevels = null,
93100
Country $country = null,
94101
string $timezone = null
95102
) {
103+
$this->providedBy = $providedBy;
104+
$this->adminLevels = $adminLevels;
96105
$this->coordinates = $coordinates;
97106
$this->bounds = $bounds;
98107
$this->streetNumber = $streetNumber;
99108
$this->streetName = $streetName;
100109
$this->postalCode = $postalCode;
101110
$this->locality = $locality;
102111
$this->subLocality = $subLocality;
103-
$this->adminLevels = $adminLevels ?: new AdminLevelCollection();
104112
$this->country = $country;
105113
$this->timezone = $timezone;
106114
}
107115

116+
/**
117+
* @return string
118+
*/
119+
public function getProvidedBy(): string
120+
{
121+
return $this->providedBy;
122+
}
123+
108124
/**
109125
* {@inheritdoc}
110126
*/
@@ -195,6 +211,7 @@ public function getTimezone()
195211
public static function createFromArray(array $data)
196212
{
197213
$defaults = [
214+
'providedBy' => 'n/a',
198215
'latitude' => null,
199216
'longitude' => null,
200217
'bounds' => [
@@ -226,6 +243,8 @@ public static function createFromArray(array $data)
226243
}
227244

228245
return new static(
246+
$data['providedBy'],
247+
new AdminLevelCollection($adminLevels),
229248
self::createCoordinates(
230249
$data['latitude'],
231250
$data['longitude']
@@ -241,7 +260,6 @@ public static function createFromArray(array $data)
241260
$data['postalCode'],
242261
$data['locality'],
243262
$data['subLocality'],
244-
new AdminLevelCollection($adminLevels),
245263
new Country(
246264
$data['country'],
247265
$data['countryCode']
@@ -317,6 +335,7 @@ public function toArray(): array
317335
];
318336

319337
return [
338+
'providedBy' => $this->providedBy,
320339
'latitude' => $lat,
321340
'longitude' => $lon,
322341
'bounds' => null !== $this->bounds ? $this->bounds->toArray() : $noBounds,
Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,13 @@
1919
*
2020
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
2121
*/
22-
final class LocationBuilder
22+
final class AddressBuilder
2323
{
24+
/**
25+
* @var string
26+
*/
27+
private $providedBy;
28+
2429
/**
2530
* @var Coordinates|null
2631
*/
@@ -76,26 +81,35 @@ final class LocationBuilder
7681
*/
7782
private $timezone;
7883

84+
/**
85+
* @param string $providedBy
86+
*/
87+
public function __construct(string $providedBy)
88+
{
89+
$this->providedBy = $providedBy;
90+
}
91+
7992
/**
8093
* @param string $class
8194
*
82-
* @return Location
95+
* @return Address
8396
*/
8497
public function build($class = Address::class)
8598
{
86-
if (!is_a($class, Location::class, true)) {
87-
throw new \LogicException('First parameter to LocationBuilder::build must be a class name implementing Geocoder\Location');
99+
if (!is_a($class, Address::class, true)) {
100+
throw new \LogicException('First parameter to LocationBuilder::build must be a class name extending Geocoder\Model\Address');
88101
}
89102

90103
return new $class(
104+
$this->providedBy,
105+
new AdminLevelCollection($this->adminLevels),
91106
$this->coordinates,
92107
$this->bounds,
93108
$this->streetNumber,
94109
$this->streetName,
95110
$this->postalCode,
96111
$this->locality,
97112
$this->subLocality,
98-
new AdminLevelCollection($this->adminLevels),
99113
new Country($this->country, $this->countryCode),
100114
$this->timezone
101115
);
@@ -107,7 +121,7 @@ public function build($class = Address::class)
107121
* @param float $north
108122
* @param float $east
109123
*
110-
* @return LocationBuilder
124+
* @return AddressBuilder
111125
*/
112126
public function setBounds($south, $west, $north, $east)
113127
{
@@ -124,7 +138,7 @@ public function setBounds($south, $west, $north, $east)
124138
* @param float $latitude
125139
* @param float $longitude
126140
*
127-
* @return LocationBuilder
141+
* @return AddressBuilder
128142
*/
129143
public function setCoordinates($latitude, $longitude)
130144
{
@@ -142,7 +156,7 @@ public function setCoordinates($latitude, $longitude)
142156
* @param string $name
143157
* @param string $code
144158
*
145-
* @return LocationBuilder
159+
* @return AddressBuilder
146160
*/
147161
public function addAdminLevel($level, $name, $code)
148162
{
@@ -154,7 +168,7 @@ public function addAdminLevel($level, $name, $code)
154168
/**
155169
* @param null|string $streetNumber
156170
*
157-
* @return LocationBuilder
171+
* @return AddressBuilder
158172
*/
159173
public function setStreetNumber($streetNumber)
160174
{
@@ -166,7 +180,7 @@ public function setStreetNumber($streetNumber)
166180
/**
167181
* @param null|string $streetName
168182
*
169-
* @return LocationBuilder
183+
* @return AddressBuilder
170184
*/
171185
public function setStreetName($streetName)
172186
{
@@ -178,7 +192,7 @@ public function setStreetName($streetName)
178192
/**
179193
* @param null|string $locality
180194
*
181-
* @return LocationBuilder
195+
* @return AddressBuilder
182196
*/
183197
public function setLocality($locality)
184198
{
@@ -190,7 +204,7 @@ public function setLocality($locality)
190204
/**
191205
* @param null|string $postalCode
192206
*
193-
* @return LocationBuilder
207+
* @return AddressBuilder
194208
*/
195209
public function setPostalCode($postalCode)
196210
{
@@ -202,7 +216,7 @@ public function setPostalCode($postalCode)
202216
/**
203217
* @param null|string $subLocality
204218
*
205-
* @return LocationBuilder
219+
* @return AddressBuilder
206220
*/
207221
public function setSubLocality($subLocality)
208222
{
@@ -214,7 +228,7 @@ public function setSubLocality($subLocality)
214228
/**
215229
* @param array $adminLevels
216230
*
217-
* @return LocationBuilder
231+
* @return AddressBuilder
218232
*/
219233
public function setAdminLevels($adminLevels)
220234
{
@@ -226,7 +240,7 @@ public function setAdminLevels($adminLevels)
226240
/**
227241
* @param null|string $country
228242
*
229-
* @return LocationBuilder
243+
* @return AddressBuilder
230244
*/
231245
public function setCountry($country)
232246
{
@@ -238,7 +252,7 @@ public function setCountry($country)
238252
/**
239253
* @param null|string $countryCode
240254
*
241-
* @return LocationBuilder
255+
* @return AddressBuilder
242256
*/
243257
public function setCountryCode($countryCode)
244258
{
@@ -250,7 +264,7 @@ public function setCountryCode($countryCode)
250264
/**
251265
* @param null|string $timezone
252266
*
253-
* @return LocationBuilder
267+
* @return AddressBuilder
254268
*/
255269
public function setTimezone($timezone)
256270
{

Model/LocationFactory.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ public static function createLocation(array $data, $class = Address::class)
4747
}
4848

4949
$address = new $class(
50+
'n/a',
51+
new AdminLevelCollection($adminLevels),
5052
self::createCoordinates(
5153
self::readDoubleValue($data, 'latitude'),
5254
self::readDoubleValue($data, 'longitude')
@@ -62,7 +64,6 @@ public static function createLocation(array $data, $class = Address::class)
6264
self::readStringValue($data, 'postalCode'),
6365
self::readStringValue($data, 'locality'),
6466
self::readStringValue($data, 'subLocality'),
65-
new AdminLevelCollection($adminLevels),
6667
new Country(
6768
self::readStringValue($data, 'country'),
6869
self::upperize(\igorw\get_in($data, ['countryCode']))

Tests/Dumper/GeoArrayTest.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@ public function testDump()
4040
'type' => 'Point',
4141
'coordinates' => [0, 0],
4242
],
43-
'properties' => null,
43+
'properties' => [
44+
'providedBy' => 'n/a',
45+
],
4446
];
4547

4648
$result = $this->dumper->dump($address);
@@ -62,7 +64,9 @@ public function testDumpWithData()
6264
'type' => 'Point',
6365
'coordinates' => [2.3889114, 48.8631507],
6466
],
65-
'properties' => null,
67+
'properties' => [
68+
'providedBy' => 'n/a',
69+
],
6670
];
6771

6872
$result = $this->dumper->dump($address);
@@ -90,7 +94,9 @@ public function testDumpWithBounds()
9094
'type' => 'Point',
9195
'coordinates' => [2.3889114, 48.8631507],
9296
],
93-
'properties' => null,
97+
'properties' => [
98+
'providedBy' => 'n/a',
99+
],
94100
'bounds' => [
95101
'south' => 48.8631507,
96102
'west' => 2.3889114,
@@ -129,6 +135,7 @@ public function testDumpWithProperties()
129135
'properties' => [
130136
'locality' => 'Paris',
131137
'country' => 'France',
138+
'providedBy' => 'n/a',
132139
],
133140
'bounds' => [
134141
'south' => 48.8631507,

Tests/Dumper/GeoJsonTest.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@ public function testDump()
4141
'type' => 'Point',
4242
'coordinates' => [0, 0],
4343
],
44-
'properties' => null,
44+
'properties' => [
45+
'providedBy' => 'n/a',
46+
],
4547
];
4648

4749
$result = $this->dumper->dump($address);
@@ -63,7 +65,9 @@ public function testDumpWithData()
6365
'type' => 'Point',
6466
'coordinates' => [2.3889114, 48.8631507],
6567
],
66-
'properties' => null,
68+
'properties' => [
69+
'providedBy' => 'n/a',
70+
],
6771
];
6872

6973
$result = $this->dumper->dump($address);
@@ -91,7 +95,9 @@ public function testDumpWithBounds()
9195
'type' => 'Point',
9296
'coordinates' => [2.3889114, 48.8631507],
9397
],
94-
'properties' => null,
98+
'properties' => [
99+
'providedBy' => 'n/a',
100+
],
95101
'bounds' => [
96102
'south' => 48.8631507,
97103
'west' => 2.3889114,
@@ -130,6 +136,7 @@ public function testDumpWithProperties()
130136
'properties' => [
131137
'locality' => 'Paris',
132138
'country' => 'France',
139+
'providedBy' => 'n/a',
133140
],
134141
'bounds' => [
135142
'south' => 48.8631507,

0 commit comments

Comments
 (0)