Skip to content

Commit 88ae8cf

Browse files
authored
Merge pull request #529 from Nyholm/interfaces
Creating interfaces to be more SOLID
2 parents 7dfe69b + 431e081 commit 88ae8cf

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

+933
-617
lines changed

CHANGELOG.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,21 @@
11
CHANGELOG
22
=========
33

4+
### 4.0.0 (2016-xx-xx)
5+
6+
* Added: Interface for `Geocoder\Model\AddressCollection` called `Geocoder\Collection`. Public APIs are updated to type hint for `Geocoder\GeocoderResult`.
7+
* Added: Interface for `Geocoder\Model\Address` called `Geocoder\Location`. Public APIs are updated to type hint for `Geocoder\Location`.
8+
* Changed: `Location::getCoordinates` will return null or a `Coordinates` object with coordinates data. It will never return `Coordinates` without data.
9+
* Changed: `Location::getBounds` will return null or a `Bounds` object with coordinates data. It will never return `Bounds` without data.
10+
* Removed: `AdminLevel::toString` in favor for `AdminLevel::__toString`.
11+
* Removed: `Country::toString` in favor for `Country::__toString`.
12+
* Removed: `Address::getCountryCode` in favor for `Address::getCountry()->getCode()`.
13+
* Removed: `Address::getLongitude` in favor for `Address::getCoordinates()->getLongitude()`.
14+
* Removed: `Address::getLatitude` in favor for `Address::getCoordinates()->getLatitude()`.
15+
* Removed: `Bounds::isDefined` as it is always defined.
16+
17+
18+
419
### 3.3.0 (2015-12-06)
520

621
* Added: timezone field for `FreeGeoIp` provider

src/Geocoder/Assert.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
namespace Geocoder;
4+
5+
class Assert
6+
{
7+
/**
8+
* @param float $value
9+
* @param string $message
10+
*/
11+
public static function latitude($value, $message = '')
12+
{
13+
if (!is_double($value)) {
14+
throw new \InvalidArgumentException(
15+
sprintf($message ?: 'Expected a double. Got: %s', self::typeToString($value))
16+
);
17+
}
18+
19+
if ($value < -90 || $value > 90) {
20+
throw new \InvalidArgumentException(
21+
sprintf($message ?: 'Latitude should be between -90 and 90. Got: %s', $value)
22+
);
23+
}
24+
}
25+
26+
/**
27+
* @param float $value
28+
* @param string $message
29+
*/
30+
public static function longitude($value, $message = '')
31+
{
32+
if (!is_double($value)) {
33+
throw new \InvalidArgumentException(
34+
sprintf($message ?: 'Expected a doable. Got: %s', self::typeToString($value))
35+
);
36+
}
37+
38+
if ($value < -180 || $value > 180) {
39+
throw new \InvalidArgumentException(
40+
sprintf($message ?: 'Latitude should be between -90 and 90. Got: %s', $value)
41+
);
42+
}
43+
}
44+
45+
private static function typeToString($value)
46+
{
47+
return is_object($value) ? get_class($value) : gettype($value);
48+
}
49+
50+
}

src/Geocoder/Collection.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace Geocoder;
4+
5+
/**
6+
* This is the interface that is always return from a Geocoder.
7+
*
8+
* @author William Durand <[email protected]>
9+
* @author Tobias Nyholm <[email protected]>
10+
*/
11+
interface Collection extends \IteratorAggregate, \Countable
12+
{
13+
/**
14+
* @return Location
15+
*/
16+
public function first();
17+
18+
/**
19+
* @return Location[]
20+
*/
21+
public function slice($offset, $length = null);
22+
23+
/**
24+
* @return bool
25+
*/
26+
public function has($index);
27+
28+
/**
29+
* @return Location
30+
* @throws \OutOfBoundsException
31+
*/
32+
public function get($index);
33+
34+
/**
35+
* @return Location[]
36+
*/
37+
public function all();
38+
}

src/Geocoder/Dumper/Dumper.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,20 @@
1010

1111
namespace Geocoder\Dumper;
1212

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

1515
/**
1616
* @author William Durand <[email protected]>
1717
*/
1818
interface Dumper
1919
{
2020
/**
21-
* Dumps an `Address` object as a string representation of
21+
* Dumps an `Location` object as a string representation of
2222
* the implemented format.
2323
*
24-
* @param Address $address
24+
* @param Location $location
2525
*
2626
* @return string
2727
*/
28-
public function dump(Address $address);
28+
public function dump(Location $location);
2929
}

src/Geocoder/Dumper/GeoJson.php

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
namespace Geocoder\Dumper;
1212

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

1515
/**
1616
* @author Jan Sorgalla <[email protected]>
@@ -20,9 +20,9 @@ class GeoJson implements Dumper
2020
/**
2121
* {@inheritDoc}
2222
*/
23-
public function dump(Address $address)
23+
public function dump(Location $location)
2424
{
25-
$properties = array_filter($address->toArray(), function ($value) {
25+
$properties = array_filter($location->toArray(), function ($value) {
2626
return !empty($value);
2727
});
2828

@@ -36,19 +36,24 @@ public function dump(Address $address)
3636
$properties = null;
3737
}
3838

39+
$lat = 0;
40+
$lon = 0;
41+
if (null !== $coordinates = $location->getCoordinates()) {
42+
$lat = $coordinates->getLatitude();
43+
$lon = $coordinates->getLongitude();
44+
}
45+
3946
$json = [
4047
'type' => 'Feature',
4148
'geometry' => [
4249
'type' => 'Point',
43-
'coordinates' => [ $address->getLongitude(), $address->getLatitude() ]
50+
'coordinates' => [$lon, $lat],
4451
],
4552
'properties' => $properties,
4653
];
4754

48-
if (null !== $bounds = $address->getBounds()) {
49-
if ($bounds->isDefined()) {
50-
$json['bounds'] = $bounds->toArray();
51-
}
55+
if (null !== $bounds = $location->getBounds()) {
56+
$json['bounds'] = $bounds->toArray();
5257
}
5358

5459
return json_encode($json);

src/Geocoder/Dumper/Gpx.php

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,19 @@
1111
namespace Geocoder\Dumper;
1212

1313
use Geocoder\Geocoder;
14-
use Geocoder\Model\Address;
14+
use Geocoder\Location;
1515

1616
/**
1717
* @author William Durand <[email protected]>
1818
*/
1919
class Gpx implements Dumper
2020
{
2121
/**
22-
* @param Address $address
22+
* @param Location $location
2323
*
2424
* @return string
2525
*/
26-
public function dump(Address $address)
26+
public function dump(Location $location)
2727
{
2828
$gpx = sprintf(<<<GPX
2929
<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
@@ -37,23 +37,29 @@ public function dump(Address $address)
3737
GPX
3838
, Geocoder::VERSION);
3939

40-
if ($address->getBounds()->isDefined()) {
41-
$bounds = $address->getBounds();
40+
if (null !== $bounds = $location->getBounds()) {
4241
$gpx .= sprintf(<<<GPX
4342
<bounds minlat="%f" minlon="%f" maxlat="%f" maxlon="%f"/>
4443
4544
GPX
4645
, $bounds->getWest(), $bounds->getSouth(), $bounds->getEast(), $bounds->getNorth());
4746
}
4847

48+
$lat = null;
49+
$lon = null;
50+
if (null !== $coordinates = $location->getCoordinates()) {
51+
$lat = $coordinates->getLatitude();
52+
$lon = $coordinates->getLongitude();
53+
}
54+
4955
$gpx .= sprintf(<<<GPX
5056
<wpt lat="%.7f" lon="%.7f">
5157
<name><![CDATA[%s]]></name>
5258
<type><![CDATA[Address]]></type>
5359
</wpt>
5460
5561
GPX
56-
, $address->getLatitude(), $address->getLongitude(), $this->formatName($address));
62+
, $lat, $lon, $this->formatName($location));
5763

5864
$gpx .= <<<GPX
5965
</gpx>
@@ -63,11 +69,11 @@ public function dump(Address $address)
6369
}
6470

6571
/**
66-
* @param Address $address
72+
* @param Location $address
6773
*
6874
* @return string
6975
*/
70-
protected function formatName(Address $address)
76+
protected function formatName(Location $address)
7177
{
7278
$name = [];
7379
$array = $address->toArray();

src/Geocoder/Dumper/Kml.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
namespace Geocoder\Dumper;
1212

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

1515
/**
1616
* @author Jan Sorgalla <[email protected]>
@@ -20,9 +20,9 @@ class Kml extends Gpx implements Dumper
2020
/**
2121
* {@inheritDoc}
2222
*/
23-
public function dump(Address $address)
23+
public function dump(Location $location)
2424
{
25-
$name = $this->formatName($address);
25+
$name = $this->formatName($location);
2626
$kml = <<<KML
2727
<?xml version="1.0" encoding="UTF-8"?>
2828
<kml xmlns="http://www.opengis.net/kml/2.2">
@@ -38,6 +38,13 @@ public function dump(Address $address)
3838
</kml>
3939
KML;
4040

41-
return sprintf($kml, $name, $name, $address->getLongitude(), $address->getLatitude());
41+
$lat = null;
42+
$lon = null;
43+
if (null !== $coordinates = $location->getCoordinates()) {
44+
$lat = $coordinates->getLatitude();
45+
$lon = $coordinates->getLongitude();
46+
}
47+
48+
return sprintf($kml, $name, $name, $lon, $lat);
4249
}
4350
}

src/Geocoder/Dumper/Wkb.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
namespace Geocoder\Dumper;
1212

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

1515
/**
1616
* @author Jan Sorgalla <[email protected]>
@@ -20,8 +20,15 @@ class Wkb implements Dumper
2020
/**
2121
* {@inheritDoc}
2222
*/
23-
public function dump(Address $address)
23+
public function dump(Location $location)
2424
{
25-
return pack('cLdd', 1, 1, $address->getLongitude(), $address->getLatitude());
25+
$lat = null;
26+
$lon = null;
27+
if (null !== $coordinates = $location->getCoordinates()) {
28+
$lat = $coordinates->getLatitude();
29+
$lon = $coordinates->getLongitude();
30+
}
31+
32+
return pack('cLdd', 1, 1, $lon, $lat);
2633
}
2734
}

src/Geocoder/Dumper/Wkt.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
namespace Geocoder\Dumper;
1212

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

1515
/**
1616
* @author Jan Sorgalla <[email protected]>
@@ -20,8 +20,15 @@ class Wkt implements Dumper
2020
/**
2121
* {@inheritDoc}
2222
*/
23-
public function dump(Address $address)
23+
public function dump(Location $location)
2424
{
25-
return sprintf('POINT(%F %F)', $address->getLongitude(), $address->getLatitude());
25+
$lat = null;
26+
$lon = null;
27+
if (null !== $coordinates = $location->getCoordinates()) {
28+
$lat = $coordinates->getLatitude();
29+
$lon = $coordinates->getLongitude();
30+
}
31+
32+
return sprintf('POINT(%F %F)', $lon, $lat);
2633
}
2734
}

0 commit comments

Comments
 (0)