Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 58e5def

Browse files
committed
Replace County and Region with AdminLevels
1 parent 813b270 commit 58e5def

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+882
-581
lines changed

README.md

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -106,12 +106,8 @@ objects (`AddressCollection`), each providing the following API:
106106
* `getLocality()` will return the `locality` or `city`;
107107
* `getPostalCode()` will return the `postalCode` or `zipcode`;
108108
* `getSubLocality()` will return the `city district`, or `sublocality`;
109-
* `getCounty()` will return a `County` object (with `name` and `code`
110-
properties);
111-
* `getCountyCode()` will return the `county` code (county short name);
112-
* `getRegion()` will return a `Region` object (with `name` and `code`
113-
properties);
114-
* `getRegionCode()` will return the `region` code (region short name);
109+
* `getAdminLevels()` will return an ordered collection (`AdminLevelCollection`)
110+
of `AdminLevel` object (with `level`, `name` and `code` properties);
115111
* `getCountry()` will return a `Country` object (with `name` and `code`
116112
properties);
117113
* `getCountryCode()` will return the ISO `country` code;
@@ -478,13 +474,9 @@ Here is the mapping:
478474

479475
* Zipcode: `%z`
480476

481-
* County: `%P`
482-
483-
* County Code: `%p`
484-
485-
* Region: `%R`
477+
* Admin Level Name: `%A1`, `%A2`, `%A3`, `%A4`, `%A5`
486478

487-
* Region Code: `%r`
479+
* Admin Level Code: `%a1`, `%a2`, `%a3`, `%a4`, `%a5`
488480

489481
* Country: `%C`
490482

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 <[email protected]>
15+
*/
16+
class UnexpectedValue extends \UnexpectedValueException implements Exception
17+
{
18+
}

src/Geocoder/Formatter/StringFormatter.php

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

1313
use Geocoder\Model\Address;
14+
use Geocoder\Model\AdminLevel;
15+
use Geocoder\Model\AdminLevelCollection;
1416

1517
/**
1618
* @author William Durand <[email protected]>
1719
*/
1820
class StringFormatter
1921
{
20-
const STREET_NUMBER = '%n';
22+
const STREET_NUMBER = '%n';
2123

22-
const STREET_NAME = '%S';
24+
const STREET_NAME = '%S';
2325

24-
const LOCALITY = '%L';
26+
const LOCALITY = '%L';
2527

26-
const POSTAL_CODE = '%z';
28+
const POSTAL_CODE = '%z';
2729

28-
const SUB_LOCALITY = '%D';
30+
const SUB_LOCALITY = '%D';
2931

30-
const COUNTY = '%P';
32+
const ADMIN_LEVEL = '%A';
3133

32-
const COUNTY_CODE = '%p';
34+
const ADMIN_LEVEL_CODE = '%a';
3335

34-
const REGION = '%R';
36+
const COUNTRY = '%C';
3537

36-
const REGION_CODE = '%r';
38+
const COUNTRY_CODE = '%c';
3739

38-
const COUNTRY = '%C';
39-
40-
const COUNTRY_CODE = '%c';
41-
42-
const TIMEZONE = '%T';
40+
const TIMEZONE = '%T';
4341

4442
/**
4543
* Transform an `Address` instance into a string representation.
@@ -51,19 +49,26 @@ class StringFormatter
5149
*/
5250
public function format(Address $address, $format)
5351
{
54-
return strtr($format, array(
52+
$tr = [
5553
self::STREET_NUMBER => $address->getStreetNumber(),
5654
self::STREET_NAME => $address->getStreetName(),
5755
self::LOCALITY => $address->getLocality(),
5856
self::POSTAL_CODE => $address->getPostalCode(),
5957
self::SUB_LOCALITY => $address->getSubLocality(),
60-
self::COUNTY => $address->getCounty()->getName(),
61-
self::COUNTY_CODE => $address->getCounty()->getCode(),
62-
self::REGION => $address->getRegion()->getName(),
63-
self::REGION_CODE => $address->getRegion()->getCode(),
6458
self::COUNTRY => $address->getCountry()->getName(),
6559
self::COUNTRY_CODE => $address->getCountry()->getCode(),
6660
self::TIMEZONE => $address->getTimezone(),
67-
));
61+
];
62+
63+
$adminLevels = $address->getAdminLevels();
64+
$nullAdminLevel = new AdminLevel(null, null, null);
65+
66+
for ($level = 1; $level <= AdminLevelCollection::MAX_LEVEL_DEPTH; ++ $level) {
67+
$adminLevel = $adminLevels->has($level) ? $adminLevels->get($level) : $nullAdminLevel;
68+
$tr[self::ADMIN_LEVEL . $level] = $adminLevel->getName();
69+
$tr[self::ADMIN_LEVEL_CODE . $level] = $adminLevel->getCode();
70+
}
71+
72+
return strtr($format, $tr);
6873
}
6974
}

src/Geocoder/Geocoder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public function geocode($value);
3434
/**
3535
* Reverses geocode given latitude and longitude values.
3636
*
37-
* @param double $latitude.
37+
* @param double $latitude
3838
* @param double $longitude
3939
*
4040
* @return AddressCollection

src/Geocoder/Model/Address.php

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

5353
/**
54-
* @var County
54+
* @var AdminLevelCollection
5555
*/
56-
private $county;
57-
58-
/**
59-
* @var Region
60-
*/
61-
private $region;
56+
private $adminLevels;
6257

6358
/**
6459
* @var Country
@@ -78,17 +73,16 @@ final class Address
7873
* @param string $subLocality
7974
*/
8075
public function __construct(
81-
Coordinates $coordinates = null,
82-
Bounds $bounds = null,
83-
$streetNumber = null,
84-
$streetName = null,
85-
$postalCode = null,
86-
$locality = null,
87-
$subLocality = null,
88-
County $county = null,
89-
Region $region = null,
90-
Country $country = null,
91-
$timezone = null
76+
Coordinates $coordinates = null,
77+
Bounds $bounds = null,
78+
$streetNumber = null,
79+
$streetName = null,
80+
$postalCode = null,
81+
$locality = null,
82+
$subLocality = null,
83+
AdminLevelCollection $adminLevels = null,
84+
Country $country = null,
85+
$timezone = null
9286
) {
9387
$this->coordinates = $coordinates;
9488
$this->bounds = $bounds;
@@ -97,8 +91,7 @@ public function __construct(
9791
$this->postalCode = $postalCode;
9892
$this->locality = $locality;
9993
$this->subLocality = $subLocality;
100-
$this->county = $county;
101-
$this->region = $region;
94+
$this->adminLevels = $adminLevels ?: new AdminLevelCollection();
10295
$this->country = $country;
10396
$this->timezone = $timezone;
10497
}
@@ -203,43 +196,13 @@ public function getSubLocality()
203196
}
204197

205198
/**
206-
* Returns the county value.
199+
* Returns the administrative levels.
207200
*
208-
* @return County
201+
* @return AdminLevelCollection
209202
*/
210-
public function getCounty()
203+
public function getAdminLevels()
211204
{
212-
return $this->county;
213-
}
214-
215-
/**
216-
* Returns the county short name.
217-
*
218-
* @return string
219-
*/
220-
public function getCountyCode()
221-
{
222-
return $this->county->getCode();
223-
}
224-
225-
/**
226-
* Returns the region value.
227-
*
228-
* @return Region
229-
*/
230-
public function getRegion()
231-
{
232-
return $this->region;
233-
}
234-
235-
/**
236-
* Returns the region short name.
237-
*
238-
* @return string
239-
*/
240-
public function getRegionCode()
241-
{
242-
return $this->region->getCode();
205+
return $this->adminLevels;
243206
}
244207

245208
/**
@@ -279,6 +242,14 @@ public function getTimezone()
279242
*/
280243
public function toArray()
281244
{
245+
$adminLevels = [];
246+
foreach ($this->adminLevels as $adminLevel) {
247+
$adminLevels[$adminLevel->getLevel()] = [
248+
'name' => $adminLevel->getName(),
249+
'code' => $adminLevel->getCode()
250+
];
251+
}
252+
282253
return array(
283254
'latitude' => $this->getLatitude(),
284255
'longitude' => $this->getLongitude(),
@@ -288,10 +259,7 @@ public function toArray()
288259
'postalCode' => $this->postalCode,
289260
'locality' => $this->locality,
290261
'subLocality' => $this->subLocality,
291-
'county' => $this->county->getName(),
292-
'countyCode' => $this->county->getCode(),
293-
'region' => $this->region->getName(),
294-
'regionCode' => $this->region->getCode(),
262+
'adminLevels' => $adminLevels,
295263
'country' => $this->country->getName(),
296264
'countryCode' => $this->country->getCode(),
297265
'timezone' => $this->timezone,

src/Geocoder/Model/AddressFactory.php

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,15 @@ public function createFromArray(array $results)
2424
{
2525
$addresses = [];
2626
foreach ($results as $result) {
27+
$adminLevels = [];
28+
foreach ($this->readArrayValue($result, 'adminLevels') as $adminLevel) {
29+
$adminLevels[] = new AdminLevel(
30+
intval($this->readStringValue($adminLevel, 'level')),
31+
$this->readStringValue($adminLevel, 'name'),
32+
$this->readStringValue($adminLevel, 'code')
33+
);
34+
}
35+
2736
$addresses[] = new Address(
2837
$this->createCoordinates(
2938
$this->readDoubleValue($result, 'latitude'),
@@ -40,14 +49,7 @@ public function createFromArray(array $results)
4049
$this->readStringValue($result, 'postalCode'),
4150
$this->readStringValue($result, 'locality'),
4251
$this->readStringValue($result, 'subLocality'),
43-
new County(
44-
$this->readStringValue($result, 'county'),
45-
$this->upperize(\igorw\get_in($result, ['countyCode']))
46-
),
47-
new Region(
48-
$this->readStringValue($result, 'region'),
49-
$this->upperize(\igorw\get_in($result, ['regionCode']))
50-
),
52+
new AdminLevelCollection($adminLevels),
5153
new Country(
5254
$this->readStringValue($result, 'country'),
5355
$this->upperize(\igorw\get_in($result, ['countryCode']))
@@ -79,6 +81,16 @@ private function readStringValue(array $data, $key)
7981
return $this->valueOrNull(\igorw\get_in($data, [ $key ]));
8082
}
8183

84+
/**
85+
* @param array $data
86+
* @param string $key
87+
* @return array
88+
*/
89+
private function readArrayValue(array $data, $key)
90+
{
91+
return \igorw\get_in($data, [ $key ]) ?: [];
92+
}
93+
8294
/**
8395
* @return string|null
8496
*/

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

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,13 @@
1313
/**
1414
* @author William Durand <[email protected]>
1515
*/
16-
final class County
16+
final class AdminLevel
1717
{
18+
/**
19+
* @var int
20+
*/
21+
private $level;
22+
1823
/**
1924
* @var string
2025
*/
@@ -26,17 +31,29 @@ final class County
2631
private $code;
2732

2833
/**
34+
* @param int $level
2935
* @param string $name
3036
* @param string $code
3137
*/
32-
public function __construct($name, $code)
38+
public function __construct($level, $name, $code)
3339
{
40+
$this->level = $level;
3441
$this->name = $name;
3542
$this->code = $code;
3643
}
3744

3845
/**
39-
* Returns the country name
46+
* Returns the administrative level
47+
*
48+
* @return int Level number [1,5]
49+
*/
50+
public function getLevel()
51+
{
52+
return $this->level;
53+
}
54+
55+
/**
56+
* Returns the administrative level name
4057
*
4158
* @return string
4259
*/
@@ -46,7 +63,7 @@ public function getName()
4663
}
4764

4865
/**
49-
* Returns the county short name.
66+
* Returns the administrative level short name.
5067
*
5168
* @return string
5269
*/
@@ -56,7 +73,7 @@ public function getCode()
5673
}
5774

5875
/**
59-
* Returns a string with the county name.
76+
* Returns a string with the administrative level name.
6077
*
6178
* @return string
6279
*/

0 commit comments

Comments
 (0)