Skip to content

Commit 6a7fbc4

Browse files
committed
Merge pull request #398 from giosh94mhz/numbered_admin_levels
Use numbered administrative level instead of named one
2 parents b275620 + 11ccbc4 commit 6a7fbc4

File tree

168 files changed

+1394
-3034
lines changed

Some content is hidden

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

168 files changed

+1394
-3034
lines changed

README.md

+4-12
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

src/Geocoder/Dumper/Gpx.php

+12-10
Original file line numberDiff line numberDiff line change
@@ -69,20 +69,22 @@ public function dump(Address $address)
6969
*/
7070
protected function formatName(Address $address)
7171
{
72-
$name = '';
72+
$name = [];
7373
$array = $address->toArray();
74-
$attrs = array('streetNumber', 'streetName', 'postalCode', 'locality', 'county', 'region', 'country');
74+
$attrs = [
75+
['streetNumber'],
76+
['streetName'],
77+
['postalCode'],
78+
['locality'],
79+
['adminLevels', 2, 'name'],
80+
['adminLevels', 1, 'name'],
81+
['country'],
82+
];
7583

7684
foreach ($attrs as $attr) {
77-
if (isset($array[$attr]) && !empty($array[$attr])) {
78-
$name .= sprintf('%s, ', $array[$attr]);
79-
}
85+
$name[] = \igorw\get_in($array, $attr);
8086
}
8187

82-
if (strlen($name) > 1) {
83-
$name = substr($name, 0, strlen($name) - 2);
84-
}
85-
86-
return $name;
88+
return implode(', ', array_filter($name));
8789
}
8890
}
+18
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

+26-20
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,27 @@ class StringFormatter
5149
*/
5250
public function format(Address $address, $format)
5351
{
54-
return strtr($format, array(
52+
$replace = [
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+
for ($level = 1; $level <= AdminLevelCollection::MAX_LEVEL_DEPTH; $level ++) {
64+
$replace[self::ADMIN_LEVEL . $level] = null;
65+
$replace[self::ADMIN_LEVEL_CODE . $level] = null;
66+
}
67+
68+
foreach ($address->getAdminLevels() as $level => $adminLevel) {
69+
$replace[self::ADMIN_LEVEL . $level] = $adminLevel->getName();
70+
$replace[self::ADMIN_LEVEL_CODE . $level] = $adminLevel->getCode();
71+
}
72+
73+
return strtr($format, $replace);
6874
}
6975
}

src/Geocoder/Geocoder.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
namespace Geocoder;
1212

13-
use Geocoder\Model\Address;
13+
use Geocoder\Model\AddressCollection;
1414

1515
/**
1616
* @author William Durand <[email protected]>
@@ -27,17 +27,17 @@ interface Geocoder
2727
*
2828
* @param string $value
2929
*
30-
* @return Address[]
30+
* @return AddressCollection
3131
*/
3232
public function geocode($value);
3333

3434
/**
3535
* Reverses geocode given latitude and longitude values.
3636
*
37-
* @param double $latitude.
37+
* @param double $latitude
3838
* @param double $longitude
3939
*
40-
* @return Address[]
40+
* @return AddressCollection
4141
*/
4242
public function reverse($latitude, $longitude);
4343

src/Geocoder/Model/Address.php

+26-58
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/AddressCollection.php

+13-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ final class AddressCollection implements \IteratorAggregate, \Countable
99
*/
1010
private $addresses;
1111

12+
/**
13+
* @param Address[] $addresses
14+
*/
1215
public function __construct(array $addresses = [])
1316
{
1417
$this->addresses = array_values($addresses);
@@ -31,7 +34,7 @@ public function count()
3134
}
3235

3336
/**
34-
* @return Address
37+
* @return Address|null
3538
*/
3639
public function first()
3740
{
@@ -50,8 +53,17 @@ public function slice($offset, $length = null)
5053
return array_slice($this->addresses, $offset, $length);
5154
}
5255

56+
/**
57+
* @return bool
58+
*/
59+
public function has($index)
60+
{
61+
return isset($this->addresses[$index]);
62+
}
63+
5364
/**
5465
* @return Address
66+
* @throws \OutOfBoundsException
5567
*/
5668
public function get($index)
5769
{

0 commit comments

Comments
 (0)