-
Notifications
You must be signed in to change notification settings - Fork 522
Creating interfaces to be more SOLID #529
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
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
bddd2ee
Creating interfaces to be more SOLID
Nyholm 093925a
Removed interfaces for value objects
Nyholm 56cf7ba
Removed interfaces for value objects
Nyholm b08e3e9
Updated "use" statements
Nyholm 59999fc
bugfix
Nyholm 297fd19
Removed unused "use" statements
Nyholm cf3213b
bugfix
Nyholm 7ac895a
Added comment
Nyholm f7ae14c
Added test for empty address
Nyholm c903049
Changed name from Position to Location
Nyholm 02924db
Make constructor arguments optional
Nyholm fb32251
We are defined even if (lat, lang) = (0, 0)
Nyholm 114c9c2
Updated variable names
Nyholm d642d81
minor
Nyholm be8709a
Removed some deprecated functions
Nyholm d24dde2
Updated changelon
Nyholm f2095a3
Updated tests
Nyholm baf73b3
Renamed GeocoderResult to Collection
Nyholm 0c919f1
Removed functions in Collection that are covered by the interface it …
Nyholm 3dd683d
Removed isDefined function
Nyholm 431e081
Updated changelog with removed isDefined functions
Nyholm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
|
||
namespace Geocoder; | ||
|
||
class Assert | ||
{ | ||
/** | ||
* @param float $value | ||
* @param string $message | ||
*/ | ||
public static function latitude($value, $message = '') | ||
{ | ||
if (!is_double($value)) { | ||
throw new \InvalidArgumentException( | ||
sprintf($message ?: 'Expected a double. Got: %s', self::typeToString($value)) | ||
); | ||
} | ||
|
||
if ($value < -90 || $value > 90) { | ||
throw new \InvalidArgumentException( | ||
sprintf($message ?: 'Latitude should be between -90 and 90. Got: %s', $value) | ||
); | ||
} | ||
} | ||
|
||
/** | ||
* @param float $value | ||
* @param string $message | ||
*/ | ||
public static function longitude($value, $message = '') | ||
{ | ||
if (!is_double($value)) { | ||
throw new \InvalidArgumentException( | ||
sprintf($message ?: 'Expected a doable. Got: %s', self::typeToString($value)) | ||
); | ||
} | ||
|
||
if ($value < -180 || $value > 180) { | ||
throw new \InvalidArgumentException( | ||
sprintf($message ?: 'Latitude should be between -90 and 90. Got: %s', $value) | ||
); | ||
} | ||
} | ||
|
||
private static function typeToString($value) | ||
{ | ||
return is_object($value) ? get_class($value) : gettype($value); | ||
} | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
namespace Geocoder; | ||
|
||
/** | ||
* This is the interface that is always return from a Geocoder. | ||
* | ||
* @author William Durand <[email protected]> | ||
* @author Tobias Nyholm <[email protected]> | ||
*/ | ||
interface Collection extends \IteratorAggregate, \Countable | ||
{ | ||
/** | ||
* @return Location | ||
*/ | ||
public function first(); | ||
|
||
/** | ||
* @return Location[] | ||
*/ | ||
public function slice($offset, $length = null); | ||
|
||
/** | ||
* @return bool | ||
*/ | ||
public function has($index); | ||
|
||
/** | ||
* @return Location | ||
* @throws \OutOfBoundsException | ||
*/ | ||
public function get($index); | ||
|
||
/** | ||
* @return Location[] | ||
*/ | ||
public function all(); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,20 +10,20 @@ | |
|
||
namespace Geocoder\Dumper; | ||
|
||
use Geocoder\Model\Address; | ||
use Geocoder\Location; | ||
|
||
/** | ||
* @author William Durand <[email protected]> | ||
*/ | ||
interface Dumper | ||
{ | ||
/** | ||
* Dumps an `Address` object as a string representation of | ||
* Dumps an `Location` object as a string representation of | ||
* the implemented format. | ||
* | ||
* @param Address $address | ||
* @param Location $location | ||
* | ||
* @return string | ||
*/ | ||
public function dump(Address $address); | ||
public function dump(Location $location); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,7 +10,7 @@ | |
|
||
namespace Geocoder\Dumper; | ||
|
||
use Geocoder\Model\Address; | ||
use Geocoder\Location; | ||
|
||
/** | ||
* @author Jan Sorgalla <[email protected]> | ||
|
@@ -20,9 +20,9 @@ class GeoJson implements Dumper | |
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function dump(Address $address) | ||
public function dump(Location $location) | ||
{ | ||
$properties = array_filter($address->toArray(), function ($value) { | ||
$properties = array_filter($location->toArray(), function ($value) { | ||
return !empty($value); | ||
}); | ||
|
||
|
@@ -36,19 +36,24 @@ public function dump(Address $address) | |
$properties = null; | ||
} | ||
|
||
$lat = 0; | ||
$lon = 0; | ||
if (null !== $coordinates = $location->getCoordinates()) { | ||
$lat = $coordinates->getLatitude(); | ||
$lon = $coordinates->getLongitude(); | ||
} | ||
|
||
$json = [ | ||
'type' => 'Feature', | ||
'geometry' => [ | ||
'type' => 'Point', | ||
'coordinates' => [ $address->getLongitude(), $address->getLatitude() ] | ||
'coordinates' => [$lon, $lat], | ||
], | ||
'properties' => $properties, | ||
]; | ||
|
||
if (null !== $bounds = $address->getBounds()) { | ||
if ($bounds->isDefined()) { | ||
$json['bounds'] = $bounds->toArray(); | ||
} | ||
if (null !== $bounds = $location->getBounds()) { | ||
$json['bounds'] = $bounds->toArray(); | ||
} | ||
|
||
return json_encode($json); | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,19 +11,19 @@ | |
namespace Geocoder\Dumper; | ||
|
||
use Geocoder\Geocoder; | ||
use Geocoder\Model\Address; | ||
use Geocoder\Location; | ||
|
||
/** | ||
* @author William Durand <[email protected]> | ||
*/ | ||
class Gpx implements Dumper | ||
{ | ||
/** | ||
* @param Address $address | ||
* @param Location $location | ||
* | ||
* @return string | ||
*/ | ||
public function dump(Address $address) | ||
public function dump(Location $location) | ||
{ | ||
$gpx = sprintf(<<<GPX | ||
<?xml version="1.0" encoding="UTF-8" standalone="no" ?> | ||
|
@@ -37,23 +37,29 @@ public function dump(Address $address) | |
GPX | ||
, Geocoder::VERSION); | ||
|
||
if ($address->getBounds()->isDefined()) { | ||
$bounds = $address->getBounds(); | ||
if (null !== $bounds = $location->getBounds()) { | ||
$gpx .= sprintf(<<<GPX | ||
<bounds minlat="%f" minlon="%f" maxlat="%f" maxlon="%f"/> | ||
|
||
GPX | ||
, $bounds->getWest(), $bounds->getSouth(), $bounds->getEast(), $bounds->getNorth()); | ||
} | ||
|
||
$lat = null; | ||
$lon = null; | ||
if (null !== $coordinates = $location->getCoordinates()) { | ||
$lat = $coordinates->getLatitude(); | ||
$lon = $coordinates->getLongitude(); | ||
} | ||
|
||
$gpx .= sprintf(<<<GPX | ||
<wpt lat="%.7f" lon="%.7f"> | ||
<name><![CDATA[%s]]></name> | ||
<type><![CDATA[Address]]></type> | ||
</wpt> | ||
|
||
GPX | ||
, $address->getLatitude(), $address->getLongitude(), $this->formatName($address)); | ||
, $lat, $lon, $this->formatName($location)); | ||
|
||
$gpx .= <<<GPX | ||
</gpx> | ||
|
@@ -63,11 +69,11 @@ public function dump(Address $address) | |
} | ||
|
||
/** | ||
* @param Address $address | ||
* @param Location $address | ||
* | ||
* @return string | ||
*/ | ||
protected function formatName(Address $address) | ||
protected function formatName(Location $address) | ||
{ | ||
$name = []; | ||
$array = $address->toArray(); | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,7 +10,7 @@ | |
|
||
namespace Geocoder\Dumper; | ||
|
||
use Geocoder\Model\Address; | ||
use Geocoder\Location; | ||
|
||
/** | ||
* @author Jan Sorgalla <[email protected]> | ||
|
@@ -20,9 +20,9 @@ class Kml extends Gpx implements Dumper | |
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function dump(Address $address) | ||
public function dump(Location $location) | ||
{ | ||
$name = $this->formatName($address); | ||
$name = $this->formatName($location); | ||
$kml = <<<KML | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<kml xmlns="http://www.opengis.net/kml/2.2"> | ||
|
@@ -38,6 +38,13 @@ public function dump(Address $address) | |
</kml> | ||
KML; | ||
|
||
return sprintf($kml, $name, $name, $address->getLongitude(), $address->getLatitude()); | ||
$lat = null; | ||
$lon = null; | ||
if (null !== $coordinates = $location->getCoordinates()) { | ||
$lat = $coordinates->getLatitude(); | ||
$lon = $coordinates->getLongitude(); | ||
} | ||
|
||
return sprintf($kml, $name, $name, $lon, $lat); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,7 +10,7 @@ | |
|
||
namespace Geocoder\Dumper; | ||
|
||
use Geocoder\Model\Address; | ||
use Geocoder\Location; | ||
|
||
/** | ||
* @author Jan Sorgalla <[email protected]> | ||
|
@@ -20,8 +20,15 @@ class Wkb implements Dumper | |
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function dump(Address $address) | ||
public function dump(Location $location) | ||
{ | ||
return pack('cLdd', 1, 1, $address->getLongitude(), $address->getLatitude()); | ||
$lat = null; | ||
$lon = null; | ||
if (null !== $coordinates = $location->getCoordinates()) { | ||
$lat = $coordinates->getLatitude(); | ||
$lon = $coordinates->getLongitude(); | ||
} | ||
|
||
return pack('cLdd', 1, 1, $lon, $lat); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,7 +10,7 @@ | |
|
||
namespace Geocoder\Dumper; | ||
|
||
use Geocoder\Model\Address; | ||
use Geocoder\Location; | ||
|
||
/** | ||
* @author Jan Sorgalla <[email protected]> | ||
|
@@ -20,8 +20,15 @@ class Wkt implements Dumper | |
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function dump(Address $address) | ||
public function dump(Location $location) | ||
{ | ||
return sprintf('POINT(%F %F)', $address->getLongitude(), $address->getLatitude()); | ||
$lat = null; | ||
$lon = null; | ||
if (null !== $coordinates = $location->getCoordinates()) { | ||
$lat = $coordinates->getLatitude(); | ||
$lon = $coordinates->getLongitude(); | ||
} | ||
|
||
return sprintf('POINT(%F %F)', $lon, $lat); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shortcuts are sometimes useful
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree that a shorter way would be useful. But do we really need both of these:
Address::getCoordinates()->getLatitude()
Address::getLatitude()
I would like to drop one of them. I chose the latter because I liked the separation of coordinates into a separate object. But Im happy to change if you prefer.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As I see it, this is the only issue we have not agreed on. Do you have any feedback here?