8000 Added Address::ProvidedBy by Nyholm · Pull Request #654 · geocoder-php/Geocoder · GitHub
[go: up one dir, main page]

Skip to content

Added Address::ProvidedBy #654

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 5 commits into from
Jun 1, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
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
7 changes: 7 additions & 0 deletions src/Common/Location.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,4 +106,11 @@ public function getTimezone();
* @return array
*/
public function toArray(): array;

/**
* The name of the provider that created this Location.
*
* @return string
*/
public function getProvidedBy(): string;
}
45 changes: 32 additions & 13 deletions src/Common/Model/Address.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,41 +70,57 @@ class Address implements Location
private $timezone;

/**
* @param Coordinates|null $coordinates
* @param Bounds|null $bounds
* @param string|null $streetNumber
* @param string|null $streetName
* @param string|null $postalCode
* @param string|null $locality
* @param string|null $subLocality
* @param AdminLevelCollection|null $adminLevels
* @param Country|null $country
* @param string|null $timezone
* @var string
*/
private $providedBy;

/**
* @param string $providedBy
* @param AdminLevelCollection $adminLevels
* @param Coordinates|null $coordinates
* @param Bounds|null $bounds
* @param string|null $streetNumber
* @param string|null $streetName
* @param string|null $postalCode
* @param string|null $locality
* @param string|null $subLocality
* @param Country|null $country
* @param string|null $timezone
*/
public function __construct(
string $providedBy,
AdminLevelCollection $adminLevels,
Coordinates $coordinates = null,
Bounds $bounds = null,
string $streetNumber = null,
string $streetName = null,
string $postalCode = null,
string $locality = null,
string $subLocality = null,
AdminLevelCollection $adminLevels = null,
Country $country = null,
string $timezone = null
) {
$this->providedBy = $providedBy;
$this->adminLevels = $adminLevels;
$this->coordinates = $coordinates;
$this->bounds = $bounds;
$this->streetNumber = $streetNumber;
$this->streetName = $streetName;
$this->postalCode = $postalCode;
$this->locality = $locality;
$this->subLocality = $subLocality;
$this->adminLevels = $adminLevels ?: new AdminLevelCollection();
$this->country = $country;
$this->timezone = $timezone;
}

/**
* @return string
*/
public function getProvidedBy(): string
{
return $this->providedBy;
}

/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -195,6 +211,7 @@ public function getTimezone()
public static function createFromArray(array $data)
{
$defaults = [
'providedBy' => 'n/a',
'latitude' => null,
'longitude' => null,
'bounds' => [
Expand Down Expand Up @@ -226,6 +243,8 @@ public static function createFromArray(array $data)
}

return new static(
$data['providedBy'],
new AdminLevelCollection($adminLevels),
self::createCoordinates(
$data['latitude'],
$data['longitude']
Expand All @@ -241,7 +260,6 @@ public static function createFromArray(array $data)
$data['postalCode'],
$data['locality'],
$data['subLocality'],
new AdminLevelCollection($adminLevels),
new Country(
$data['country'],
$data['countryCode']
Expand Down Expand Up @@ -317,6 +335,7 @@ public function toArray(): array
];

return [
'providedBy' => $this->providedBy,
'latitude' => $lat,
'longitude' => $lon,
'bounds' => null !== $this->bounds ? $this->bounds->toArray() : $noBounds,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,13 @@
*
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
*/
final class LocationBuilder
final class AddressBuilder
{
/**
* @var string
*/
private $providedBy;

/**
* @var Coordinates|null
*/
Expand Down Expand Up @@ -76,26 +81,35 @@ final class LocationBuilder
*/
private $timezone;

/**
* @param string $providedBy
*/
public function __construct(string $providedBy)
{
$this->providedBy = $providedBy;
}

/**
* @param string $class
*
* @return Location
* @return Address
*/
public function build($class = Address::class)
{
if (!is_a($class, Location::class, true)) {
throw new \LogicException('First parameter to LocationBuilder::build must be a class name implementing Geocoder\Location');
if (!is_a($class, Address::class, true)) {
throw new \LogicException('First parameter to LocationBuilder::build must be a class name extending Geocoder\Model\Address');
}

return new $class(
$this->providedBy,
new AdminLevelCollection($this->adminLevels),
$this->coordinates,
$this->bounds,
$this->streetNumber,
$this->streetName,
$this->postalCode,
$this->locality,
$this->subLocality,
new AdminLevelCollection($this->adminLevels),
new Country($this->country, $this->countryCode),
$this->timezone
);
Expand All @@ -107,7 +121,7 @@ public function build($class = Address::class)
* @param float $north
* @param float $east
67ED *
* @return LocationBuilder
* @return AddressBuilder
*/
public function setBounds($south, $west, $north, $east)
{
Expand All @@ -124,7 +138,7 @@ public function setBounds($south, $west, $north, $east)
* @param float $latitude
* @param float $longitude
*
* @return LocationBuilder
* @return AddressBuilder
*/
public function setCoordinates($latitude, $longitude)
{
Expand All @@ -142,7 +156,7 @@ public function setCoordinates($latitude, $longitude)
* @param string $name
* @param string $code
*
* @return LocationBuilder
* @return AddressBuilder
*/
public function addAdminLevel($level, $name, $code)
{
Expand All @@ -154,7 +168,7 @@ public function addAdminLevel($level, $name, $code)
/**
* @param null|string $streetNumber
*
* @return LocationBuilder
* @return AddressBuilder
*/
public function setStreetNumber($streetNumber)
{
Expand All @@ -166,7 +180,7 @@ public function setStreetNumber($streetNumber)
/**
* @param null|string $streetName
*
* @return LocationBuilder
* @return AddressBuilder
*/
public function setStreetName($streetName)
{
Expand All @@ -178,7 +192,7 @@ public function setStreetName($streetName)
/**
* @param null|string $locality
*
* @return LocationBuilder
* @return AddressBuilder
*/
public function setLocality($locality)
{
Expand All @@ -190,7 +204,7 @@ public function setLocality($locality)
/**
* @param null|string $postalCode
*
* @return LocationBuilder
* @return AddressBuilder
*/
public function setPostalCode($postalCode)
{
Expand All @@ -202,7 +216,7 @@ public function setPostalCode($postalCode)
/**
* @param null|string $subLocality
*
* @return LocationBuilder
* @return AddressBuilder
*/
public function setSubLocality($subLocality)
{
Expand All @@ -214,7 +228,7 @@ public function setSubLocality($subLocality)
/**
* @param array $adminLevels
*
* @return LocationBuilder
* @return AddressBuilder
*/
public function setAdminLevels($adminLevels)
{
Expand All @@ -226,7 +240,7 @@ public function setAdminLevels($adminLevels)
/**
* @param null|string $country
*
* @return LocationBuilder
* @return AddressBuilder
*/
public function setCountry($country)
{
Expand All @@ -238,7 +252,7 @@ public function setCountry($country)
/**
* @param null|string $countryCode
*
* @return LocationBuilder
* @return AddressBuilder
*/
public function setCountryCode($countryCode)
{
Expand All @@ -250,7 +264,7 @@ public function setCountryCode($countryCode)
/**
* @param null|string $timezone
*
* @return LocationBuilder
* @return AddressBuilder
*/
public function setTimezone($timezone)
{
Expand Down
3 changes: 2 additions & 1 deletion src/Common/Model/LocationFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ public static function createLocation(array $data, $class = Address::class)
}

$address = new $class(
'n/a',
new AdminLevelCollection($adminLevels),
self::createCoordinates(
self::readDoubleValue($data, 'latitude'),
self::readDoubleValue($data, 'longitude')
Expand All @@ -62,7 +64,6 @@ public static function createLocation(array $data, $class = Address::class)
self::readStringValue($data, 'postalCode'),
self::readStringValue($data, 'locality'),
self::readStringValue($data, 'subLocality'),
new AdminLevelCollection($adminLevels),
new Country(
self::readStringValue($data, 'country'),
self::upperize(\igorw\get_in($data, ['countryCode']))
Expand Down
13 changes: 10 additions & 3 deletions src/Common/Tests/Dumper/GeoArrayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ public function testDump()
'type' => 'Point',
'coordinates' => [0, 0],
10000 ],
'properties' => null,
'properties' => [
'providedBy' => 'n/a',
],
];

$result = $this->dumper->dump($address);
Expand All @@ -62,7 +64,9 @@ public function testDumpWithData()
'type' => 'Point',
'coordinates' => [2.3889114, 48.8631507],
],
'properties' => null,
'properties' => [
'providedBy' => 'n/a',
],
];

$result = $this->dumper->dump($address);
Expand Down Expand Up @@ -90,7 +94,9 @@ public function testDumpWithBounds()
'type' => 'Point',
'coordinates' => [2.3889114, 48.8631507],
],
'properties' => null,
'properties' => [
'providedBy' => 'n/a',
],
'bounds' => [
'south' => 48.8631507,
'west' => 2.3889114,
Expand Down Expand Up @@ -129,6 +135,7 @@ public function testDumpWithProperties()
'properties' => [
'locality' => 'Paris',
'country' => 'France',
'providedBy' => 'n/a',
],
'bounds' => [
'south' => 48.8631507,
Expand Down
13 changes: 10 additions & 3 deletions src/Common/Tests/Dumper/GeoJsonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ public function testDump()
'type' => 'Point',
'coordinates' => [0, 0],
],
'properties' => null,
'properties' => [
'providedBy' => 'n/a',
],
];

$result = $this->dumper->dump($address);
Expand All @@ -63,7 +65,9 @@ public function testDumpWithData()
'type' => 'Point',
'coordinates' => [2.3889114, 48.8631507],
],
'properties' => null,
'properties' => [
'providedBy' => 'n/a',
],
];

$result = $this->dumper->dump($address);
Expand Down Expand Up @@ -91,7 +95,9 @@ public function testDumpWithBounds()
'type' => 'Point',
'coordinates' => [2.3889114, 48.8631507],
],
'properties' => null,
'properties' => [
'providedBy' => 'n/a',
],
'bounds' => [
'south' => 48.8631507,
'west' => 2.3889114,
Expand Down Expand Up @@ -130,6 +136,7 @@ public function testDumpWithProperties()
'properties' => [
'locality' => 'Paris',
'country' => 'France',
'providedBy' => 'n/a',
],
'bounds' => [
'south' => 48.8631507,
Expand Down
Loading
0