10000 Use numbered administrative level instead of named one by giosh94mhz · Pull Request #398 · geocoder-php/Geocoder · GitHub
[go: up one dir, main page]

Skip to content
< 8000 div class="d-flex flex-column flex-md-row flex-items-start flex-md-items-center">

Use numbered administrative level instead of named one #398

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 17 commits into from
Feb 13, 2015
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Replace County and Region with AdminLevels
  • Loading branch information
giosh94mhz committed Feb 11, 2015
commit 58e5defcba5b249e70b8d5091267b750995cac13
16 changes: 4 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,8 @@ objects (`AddressCollection`), each providing the following API:
* `getLocality()` will return the `locality` or `city`;
* `getPostalCode()` will return the `postalCode` or `zipcode`;
* `getSubLocality()` will return the `city district`, or `sublocality`;
* `getCounty()` will return a `County` object (with `name` and `code`
properties);
* `getCountyCode()` will return the `county` code (county short name);
* `getRegion()` will return a `Region` object (with `name` and `code`
properties);
* `getRegionCode()` will return the `region` code (region short name);
* `getAdminLevels()` will return an ordered collection (`AdminLevelCollection`)
of `AdminLevel` object (with `level`, `name` and `code` properties);
* `getCountry()` will return a `Country` object (with `name` and `code`
properties);
* `getCountryCode()` will return the ISO `country` code;
Expand Down Expand Up @@ -478,13 +474,9 @@ Here is the mapping:

* Zipcode: `%z`

* County: `%P`

* County Code: `%p`

* Region: `%R`
* Admin Level Name: `%A1`, `%A2`, `%A3`, `%A4`, `%A5`

* Region Code: `%r`
* Admin Level Code: `%a1`, `%a2`, `%a3`, `%a4`, `%a5`

* Country: `%C`

Expand Down
18 changes: 18 additions & 0 deletions src/Geocoder/Exception/UnexpectedValue.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

/**
* This file is part of the Geocoder package.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @license MIT License
*/

namespace Geocoder\Exception;

/**
* @author Giorgio Premi <giosh94mhz@gmail.com>
*/
class UnexpectedValue extends \UnexpectedValueException implements Exception
{
}
45 changes: 25 additions & 20 deletions src/Geocoder/Formatter/StringFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,35 +11,33 @@
namespace Geocoder\Formatter;

use Geocoder\Model\Address;
use Geocoder\Model\AdminLevel;
use Geocoder\Model\AdminLevelCollection;

/**
* @author William Durand <william.durand1@gmail.com>
*/
class StringFormatter
{
const STREET_NUMBER = '%n';
const STREET_NUMBER = '%n';

9E88 const STREET_NAME = '%S';
const STREET_NAME = '%S';

const LOCALITY = '%L';
const LOCALITY = '%L';

const POSTAL_CODE = '%z';
const POSTAL_CODE = '%z';

const SUB_LOCALITY = '%D';
const SUB_LOCALITY = '%D';

const COUNTY = '%P';
const ADMIN_LEVEL = '%A';

const COUNTY_CODE = '%p';
const ADMIN_LEVEL_CODE = '%a';

const REGION = '%R';
const COUNTRY = '%C';

const REGION_CODE = '%r';
const COUNTRY_CODE = '%c';

const COUNTRY = '%C';

const COUNTRY_CODE = '%c';

const TIMEZONE = '%T';
const TIMEZONE = '%T';

/**
* Transform an `Address` instance into a string representation.
Expand All @@ -51,19 +49,26 @@ class StringFormatter
*/
public function format(Address $address, $format)
{
return strtr($format, array(
$tr = [
self::STREET_NUMBER => $address->getStreetNumber(),
self::STREET_NAME => $address->getStreetName(),
self::LOCALITY => $address->getLocality(),
self::POSTAL_CODE => $address->getPostalCode(),
self::SUB_LOCALITY => $address->getSubLocality(),
self::COUNTY => $address->getCounty()->getName(),
self::COUNTY_CODE => $address->getCounty()->getCode(),
self::REGION => $address->getRegion()->getName(),
self::REGION_CODE => $address->getRegion()->getCode(),
self::COUNTRY => $address->getCountry()->getName(),
self::COUNTRY_CODE => $address->getCountry()->getCode(),
self::TIMEZONE => $address->getTimezone(),
));
];

$adminLevels = $address->getAdminLevels();
$nullAdminLevel = new AdminLevel(null, null, null);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we return a null object or only null?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand what you mean, since the returned value is the formatted string.
Here I've used a null object just to avoid two cycle: the first to set all ADMIN_LEVEL_* $tr to null, and the second (foreach adminLevels) to set the actual value for the address. Since AdminLevel class is final there should be no problem.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you please avoid this null object?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, no prob.


for ($level = 1; $level <= AdminLevelCollection::MAX_LEVEL_DEPTH; ++ $level) {
$adminLevel = $adminLevels->has($level) ? $adminLevels->get($level) : $nullAdminLevel;
$tr[self::ADMIN_LEVEL . $level] = $adminLevel->getName();
$tr[self::ADMIN_LEVEL_CODE . $level] = $adminLevel->getCode();
}

return strtr($format, $tr);
}
}
2 changes: 1 addition & 1 deletion src/Geocoder/Geocoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function geocode($value);
/**
* Reverses geocode given latitude and longitude values.
*
* @param double $latitude.
* @param double $latitude
* @param double $longitude
*
* @return AddressCollection
Expand Down
84 changes: 26 additions & 58 deletions src/Geocoder/Model/Address.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,9 @@ final class Address
private $postalCode;

/**
* @var County
* @var AdminLevelCollection
*/
private $county;

/**
* @var Region
*/
private $region;
private $adminLevels;

/**
* @var Country
Expand All @@ -78,17 +73,16 @@ final class Address
* @param string $subLocality
*/
public function __construct(
Coordinates $coordinates = null,
Bounds $bounds = null,
$streetNumber = null,
$streetName = null,
$postalCode = null,
$locality = null,
$subLocality = null,
County $county = null,
Region $region = null,
Country $country = null,
$timezone = null
Coordinates $coordinates = null,
Bounds $bounds = null,
$streetNumber = null,
$streetName = null,
$postalCode = null,
$locality = null,
$subLocality = null,
AdminLevelCollection $adminLevels = null,
Country $country = null,
$timezone = null
) {
$this->coordinates = $coordinates;
$this->bounds = $bounds;
Expand All @@ -97,8 +91,7 @@ public function __construct(
$this->postalCode = $postalCode;
$this->locality = $locality;
$this->subLocality = $subLocality;
$this->county = $county;
$this->region = $region;
$this->adminLevels = $adminLevels ?: new AdminLevelCollection();
$this->country = $country;
$this->timezone = $timezone;
}
Expand Down Expand Up @@ -203,43 +196,13 @@ public function getSubLocality()
}

/**
* Returns the county value.
* Returns the administrative levels.
*
* @return County
* @return AdminLevelCollection
*/
public function getCounty()
public function getAdminLevels()
{
return $this->county;
}

/**
* Returns the county short name.
*
* @return string
*/
public function getCountyCode()
{
return $this->county->getCode();
}

/**
* Returns the region value.
*
* @return Region
*/
public function getRegion()
{
return $this->region;
}

/**
* Returns the region short name.
*
* @return string
*/
public function getRegionCode()
{
return $this->region->getCode();
return $this->adminLevels;
}

/**
Expand Down Expand Up @@ -279,6 +242,14 @@ public function getTimezone()
*/
public function toArray()
{
$adminLevels = [];
foreach ($this->adminLevels as $adminLevel) {
$adminLevels[$adminLevel->getLevel()] = [
'name' => $adminLevel->getName(),
'code' => $adminLevel->getCode()
];
}

return array(
'latitude' => $this->getLatitude(),
'longitude' => $this->getLongitude(),
Expand All @@ -288,10 +259,7 @@ public function toArray()
'postalCode' => $this->postalCode,
'locality' => $this->locality,
'subLocality' => $this->subLocality,
'county' => $this->county->getName(),
'countyCode' => $this->county->getCode(),
'region' => $this->region->getName(),
'regionCode' => $this->region->getCode(),
'adminLevels' => $adminLevels,
'country' => $this->country->getName(),
'countryCode' => $this->country->getCode(),
'timezone' => $this->timezone,
Expand Down
28 changes: 20 additions & 8 deletions src/Geocoder/Model/AddressFactory.php
1241
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,15 @@ public function createFromArray(array $results)
{
$addresses = [];
foreach ($results as $result) {
$adminLevels = [];
foreach ($this->readArrayValue($result, 'adminLevels') as $adminLevel) {
$adminLevels[] = new AdminLevel(
intval($this->readStringValue($adminLevel, 'level')),
$this->readStringValue($adminLevel, 'name'),
$this->readStringValue($adminLevel, 'code')
);
}

$addresses[] = new Address(
$this->createCoordinates(
$this->readDoubleValue($result, 'latitude'),
Expand All @@ -40,14 +49,7 @@ public function createFromArray(array $results)
$this->readStringValue($result, 'postalCode'),
$this->readStringValue($result, 'locality'),
$this->readStringValue($result, 'subLocality'),
new County(
$this->readStringValue($result, 'county'),
$this->upperize(\igorw\get_in($result, ['countyCode']))
),
new Region(
$this->readStringValue($result, 'region'),
$this->upperize(\igorw\get_in($result, ['regionCode']))
),
new AdminLevelCollection($adminLevels),
new Country(
$this->readStringValue($result, 'country'),
$this->upperize(\igorw\get_in($result, ['countryCode']))
Expand Down Expand Up @@ -79,6 +81,16 @@ private function readStringValue(array $data, $key)
return $this->valueOrNull(\igorw\get_in($data, [ $key ]));
}

/**
* @param array $data
* @param string $key
* @return array
*/
private function readArrayValue(array $data, $key)
{
return \igorw\get_in($data, [ $key ]) ?: [];
}

/**
* @return string|null
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,13 @@
/**
* @author William Durand <william.durand1@gmail.com>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't remove the @author tag please.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not removed, the IDE changed to my tag author during the refactoring. I'll revert it anyway, no problem.

*/
final class County
final class AdminLevel
{
/**
* @var int
*/
private $level;

/**
* @var string
*/
Expand All @@ -26,17 +31,29 @@ final class County
private $code;

/**
* @param int $level
* @param string $name
* @param string $code
*/
public function __construct($name, $code)
public function __construct($level, $name, $code)
{
$this->level = $level;
$this->name = $name;
$this->code = $code;
}

/**
* Returns the country name
* Returns the administrative level
*
* @return int Level number [1,5]
*/
public function getLevel()
{
return $this->level;
}

/**
* Returns the administrative level name
*
* @return string
*/
Expand All @@ -46,7 +63,7 @@ public function getName()
}

/**
* Returns the county short name.
* Returns the administrative level short name.
*
* @return string
*/
Expand All @@ -56,7 +73,7 @@ public function getCode()
}

/**
* Returns a string with the county name.
* Returns a string with the administrative level name.
*
* @return string
*/
Expand Down
Loading
0