Skip to content

Commit 0c852d1

Browse files
committed
Refactored Admin to AdminLevel
1 parent f9e3848 commit 0c852d1

36 files changed

+364
-303
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
/**
4+
* This file is part of the Geocoder package.
5+
* For the full copyright and license information, please view the LICENSE
6+
* file that was distributed with this source code.
7+
*
8+
* @license MIT License
9+
*/
10+
11+
namespace Geocoder\Exception;
12+
13+
/**
14+
* @author Giorgio Premi <giosh94mhz@gmail.com>
15+
*/
16+
class UnexpectedValue extends \UnexpectedValueException implements Exception
17+
{
18+
}

src/Geocoder/Formatter/StringFormatter.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
namespace Geocoder\Formatter;
1212

1313
use Geocoder\Model\Address;
14-
use Geocoder\Model\Admin;
14+
use Geocoder\Model\AdminLevel;
1515

1616
/**
1717
* @author William Durand <william.durand1@gmail.com>
@@ -59,12 +59,12 @@ public function format(Address $address, $format)
5959
self::TIMEZONE => $address->getTimezone(),
6060
];
6161

62-
$admins = $address->getAdmins();
63-
$nullAdmin = new Admin(null, null);
64-
for ($level = 0; $level < 5; ++ $level) {
65-
$admin = isset($admins[$level]) ? $admins[$level] : $nullAdmin;
66-
$tr[self::ADMIN_LEVEL . ($level + 1)] = $admin->getName();
67-
$tr[self::ADMIN_LEVEL_CODE . ($level + 1)] = $admin->getCode();
62+
$adminLevels = $address->getAdminLevels();
63+
$nullAdminLevel = new AdminLevel(null, null, null);
64+
for ($level = 1; $level <= 5; ++ $level) {
65+
$adminLevel = isset($adminLevels[$level]) ? $adminLevels[$level] : $nullAdminLevel;
66+
$tr[self::ADMIN_LEVEL . $level] = $adminLevel->getName();
67+
$tr[self::ADMIN_LEVEL_CODE . $level] = $adminLevel->getCode();
6868
}
6969

7070
return strtr($format, $tr);

src/Geocoder/Model/Address.php

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,9 @@ final class Address
5151
private $postalCode;
5252

5353
/**
54-
* @var Admin[]
54+
* @var AdminLevel[]
5555
*/
56-
private $admins;
56+
private $adminLevels;
5757

5858
/**
5959
* @var Country
@@ -80,7 +80,7 @@ public function __construct(
8080
$postalCode = null,
8181
$locality = null,
8282
$subLocality = null,
83-
array $admins = [],
83+
array $adminLevels = [],
8484
Country $country = null,
8585
$timezone = null
8686
) {
@@ -91,7 +91,7 @@ public function __construct(
9191
$this->postalCode = $postalCode;
9292
$this->locality = $locality;
9393
$this->subLocality = $subLocality;
94-
$this->admins = $admins;
94+
$this->adminLevels = $adminLevels;
9595
$this->country = $country;
9696
$this->timezone = $timezone;
9797
}
@@ -198,11 +198,11 @@ public function getSubLocality()
198198
/**
199199
* Returns the administrative levels.
200200
*
201-
* @return Admin[]
201+
* @return AdminLevel[]
202202
*/
203-
public function getAdmins()
203+
public function getAdminLevels()
204204
{
205-
return $this->admins;
205+
return $this->adminLevels;
206206
}
207207

208208
/**
@@ -242,11 +242,11 @@ public function getTimezone()
242242
*/
243243
public function toArray()
244244
{
245-
$admins = [];
246-
foreach ($this->admins as $admin) {
247-
$admins[] = [
248-
'name' => $admin->getName(),
249-
'code' => $admin->getCode()
245+
$adminLevels = [];
246+
foreach ($this->adminLevels as $adminLevel) {
247+
$adminLevels[$adminLevel->getLevel()] = [
248+
'name' => $adminLevel->getName(),
249+
'code' => $adminLevel->getCode()
250250
];
251251
}
252252

@@ -259,7 +259,7 @@ public function toArray()
259259
'postalCode' => $this->postalCode,
260260
'locality' => $this->locality,
261261
'subLocality' => $this->subLocality,
262-
'admins' => $admins,
262+
'adminLevels' => $adminLevels,
263263
'country' => $this->country->getName(),
264264
'countryCode' => $this->country->getCode(),
265265
'timezone' => $this->timezone,

src/Geocoder/Model/AddressFactory.php

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
namespace Geocoder\Model;
1212

13+
use Geocoder\Exception\UnexpectedValue;
14+
1315
/**
1416
* @author Markus Bachmann <markus.bachmann@bachi.biz>
1517
* @author William Durand <william.durand1@gmail.com>
@@ -20,16 +22,18 @@ public function createFromArray(array $results)
2022
{
2123
$addresses = [];
2224
foreach ($results as $result) {
23-
$rawAdmins = \igorw\get_in($result, ['admins'], []);
24-
25-
$admins = [];
26-
foreach ($rawAdmins as $level => $admin) {
27-
$admins[$level] = new Admin(
28-
\igorw\get_in($admin, ['name']),
29-
\igorw\get_in($admin, ['code'])
25+
$adminLevels = [];
26+
foreach ($this->readArrayValue($result, 'adminLevels') as $level => $adminLevel) {
27+
$level = $this->normalizeLevelValue($level);
28+
$adminLevels[$level] = new AdminLevel(
29+
$level,
30+
$this->readStringValue($adminLevel, 'name'),
31+
$this->readStringValue($adminLevel, 'code')
3032
);
3133
}
3234

35+
ksort($adminLevels, SORT_NUMERIC);
36+
3337
$addresses[] = new Address(
3438
$this->createCoordinates(
3539
$this->readDoubleValue($result, 'latitude'),
@@ -46,7 +50,7 @@ public function createFromArray(array $results)
4650
$this->readStringValue($result, 'postalCode'),
4751
$this->readStringValue($result, 'locality'),
4852
$this->readStringValue($result, 'subLocality'),
49-
$admins,
53+
$adminLevels,
5054
new Country(
5155
$this->readStringValue($result, 'country'),
5256
$this->upperize(\igorw\get_in($result, ['countryCode']))
@@ -78,6 +82,30 @@ private function readStringValue(array $data, $key)
7882
return $this->valueOrNull(\igorw\get_in($data, [ $key ]));
7983
}
8084

85+
/**
86+
* @param array $data
87+
* @param string $key
88+
* @return array
89+
*/
90+
private function readArrayValue(array $data, $key)
91+
{
92+
return \igorw\get_in($data, [ $key ]) ?: [];
93+
}
94+
/**
95+
* @param string|int $level
96+
* @return int
97+
*/
98+
private function normalizeLevelValue($level)
99+
{
100+
$level = intval($level);
101+
102+
if ($level <= 0 || $level > 5) {
103+
throw new UnexpectedValue(sprintf("Administrative level should be an integer in [1,5], %s given", $level));
104+
}
105+
106+
return $level;
107+
}
108+
81109
/**
82110
* @return string|null
83111
*/

src/Geocoder/Model/Admin.php renamed to src/Geocoder/Model/AdminLevel.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
/**
1414
* @author Giorgio Premi <giosh94mhz@gmail.com>
1515
*/
16-
final class Admin
16+
final class AdminLevel
1717
{
1818
/**
1919
* @var string
@@ -26,15 +26,27 @@ final class Admin
2626
private $code;
2727

2828
/**
29+
* @param int $level
2930
* @param string $name
3031
* @param string $code
3132
*/
32-
public function __construct($name, $code)
33+
public function __construct($level, $name, $code)
3334
{
35+
$this->level = $level;
3436
$this->name = $name;
3537
$this->code = $code;
3638
}
3739

40+
/**
41+
* Returns the administrative level
42+
*
43+
* @return int Level number [1,5]
44+
*/
45+
public function getLevel()
46+
{
47+
return $this->level;
48+
}
49+
3850
/**
3951
* Returns the administrative level name
4052
*

src/Geocoder/Provider/AbstractProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ protected function getDefaults()
7373
'locality' => null,
7474
'postalCode' => null,
7575
'subLocality' => null,
76-
'admins' => [],
76+
'adminLevels' => [],
7777
'country' => null,
7878
'countryCode' => null,
7979
'timezone' => null,

src/Geocoder/Provider/GeoIPs.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
use Geocoder\Exception\QuotaExceeded;
1717
use Geocoder\Exception\UnsupportedOperation;
1818
use Ivory\HttpAdapter\HttpAdapterInterface;
19-
use Geocoder\Model\Admin;
2019

2120
/**
2221
* @author Andrea Cristaudo <andrea.cristaudo@gmail.com>
@@ -170,21 +169,21 @@ private function executeQuery($query)
170169
return '' === $value ? null : $value;
171170
}, $response['location']);
172171

173-
$admins = [];
172+
$adminLevels = [];
174173

175174
if (null !== $location['region_name'] || null !== $location['region_code']) {
176-
$admins[0] = ['name' => $location['region_name'], 'code' => $location['region_code']];
175+
$adminLevels[1] = ['name' => $location['region_name'], 'code' => $location['region_code']];
177176
}
178177

179178
if (null !== $location['county_name']) {
180-
$admins[1] = ['name' => $location['county_name']];
179+
$adminLevels[2] = ['name' => $location['county_name']];
181180
}
182181

183182
$results = [];
184183
$results[] = array_merge($this->getDefaults(), array(
185184
'country' => $location['country_name'],
186185
'countryCode' => $location['country_code'],
187-
'admins' => $admins,
186+
'adminLevels' => $adminLevels,
188187
'locality' => $location['city_name'],
189188
'latitude' => $location['latitude'],
190189
'longitude' => $location['longitude'],

src/Geocoder/Provider/GeoPlugin.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,21 +80,21 @@ private function executeQuery($query)
8080

8181
$data = array_filter($json);
8282

83-
$admins = [];
83+
$adminLevels = [];
8484

8585
$region = \igorw\get_in($data, ['geoplugin_regionName']);
8686
$regionCode = \igorw\get_in($data, ['geoplugin_regionCode']);
8787

8888
if (null !== $region || null !== $regionCode) {
89-
$admins[0] = ['name' => $region, 'code' => $regionCode];
89+
$adminLevels[1] = ['name' => $region, 'code' => $regionCode];
9090
}
9191

9292
$results = [];
9393
$results[] = array_merge($this->getDefaults(), [
9494
'locality' => isset($data['geoplugin_city']) ? $data['geoplugin_city'] : null,
9595
'country' => isset($data['geoplugin_countryName']) ? $data['geoplugin_countryName'] : null,
9696
'countryCode' => isset($data['geoplugin_countryCode']) ? $data['geoplugin_countryCode'] : null,
97-
'admins' => $admins,
97+
'adminLevels' => $adminLevels,
9898
'latitude' => isset($data['geoplugin_latitude']) ? $data['geoplugin_latitude'] : null,
9999
'longitude' => isset($data['geoplugin_longitude']) ? $data['geoplugin_longitude'] : null,
100100
]);

src/Geocoder/Provider/Geoip.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public function geocode($address)
6363
'longitude' => $results['longitude'],
6464
'locality' => $results['city'],
6565
'postalCode' => $results['postal_code'],
66-
'admins' => $results['region'] ? [['name' => $region, 'regionCode' => $results['region']]] : [],
66+
'adminLevels' => $results['region'] ? [1 => ['name' => $region, 'code' => $results['region']]] : [],
6767
'country' => $results['country_name'],
6868
'countryCode' => $results['country_code'],
6969
'timezone' => $timezone,

src/Geocoder/Provider/Geonames.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -134,22 +134,22 @@ private function executeQuery($query)
134134
);
135135
}
136136

137-
$admins = [];
137+
$adminLevels = [];
138138

139139
if (isset($item->adminName1)) {
140-
$admins[0] = ['name' => $item->adminName1];
140+
$adminLevels[1] = ['name' => $item->adminName1];
141141
}
142142

143143
if (isset($item->adminName2)) {
144-
$admins[1] = ['name' => $item->adminName2];
144+
$adminLevels[2] = ['name' => $item->adminName2];
145145
}
146146

147147
$results[] = array_merge($this->getDefaults(), [
148148
'latitude' => isset($item->lat) ? $item->lat : null,
149149
'longitude' => isset($item->lng) ? $item->lng : null,
150150
'bounds' => $bounds,
151151
'locality' => isset($item->name) ? $item->name : null,
152-
'admins' => $admins,
152+
'adminLevels' => $adminLevels,
153153
'country' => isset($item->countryName) ? $item->countryName : null,
154154
'countryCode' => isset($item->countryCode) ? $item->countryCode : null,
155155
'timezone' => isset($item->timezone->timeZoneId) ? $item->timezone->timeZoneId : null,

0 commit comments

Comments
 (0)