Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #214 from JeroenDeDauw/mawiki
Refactor new geocoder and its test
- Loading branch information
Showing
11 changed files
with
277 additions
and
131 deletions.
There are no files selected for viewing
This file contains 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 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 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 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,42 @@ | ||
<?php | ||
|
||
use Maps\Geocoders\Geocoder; | ||
|
||
/** | ||
* @since 3.8 | ||
* | ||
* @licence GNU GPL v2+ | ||
* @author Jeroen De Dauw < jeroendedauw@gmail.com > | ||
*/ | ||
final class MapsDecoratedGeocoder extends \Maps\Geocoder { | ||
|
||
private $geocoder; | ||
|
||
/** | ||
* @param Geocoder $geocoder | ||
* @param string $identifier | ||
*/ | ||
public function __construct( Geocoder $geocoder, $identifier ) { | ||
$this->geocoder = $geocoder; | ||
|
||
parent::__construct( $identifier ); | ||
} | ||
|
||
public function geocode( $address ) { | ||
$result = $this->geocoder->geocode( $address ); | ||
|
||
if ( $result === null ) { | ||
return false; | ||
} | ||
|
||
return [ | ||
'lat' => $result->getLatitude(), | ||
'lon' => $result->getLongitude(), | ||
]; | ||
} | ||
|
||
protected function getRequestUrl( $address ) {} | ||
|
||
protected function parseResponse( $response ) {} | ||
|
||
} |
This file was deleted.
Oops, something went wrong.
This file contains 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,24 @@ | ||
<?php | ||
|
||
namespace Maps\Geocoders; | ||
|
||
use DataValues\Geo\Values\LatLongValue; | ||
|
||
/** | ||
* @since 3.8 | ||
* | ||
* @licence GNU GPL v2+ | ||
* @author Jeroen De Dauw < jeroendedauw@gmail.com > | ||
*/ | ||
interface Geocoder { | ||
|
||
/** | ||
* @param string $address | ||
* | ||
* @return LatLongValue|null | ||
* | ||
* TODO: specify failure behaviour. Exception or null? | ||
*/ | ||
public function geocode( $address ); | ||
|
||
} |
This file contains 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,37 @@ | ||
<?php | ||
|
||
namespace Maps\Geocoders; | ||
|
||
use DataValues\Geo\Values\LatLongValue; | ||
|
||
/** | ||
* @since 3.8 | ||
* | ||
* @licence GNU GPL v2+ | ||
* @author Jeroen De Dauw < jeroendedauw@gmail.com > | ||
*/ | ||
class InMemoryGeocoder implements Geocoder { | ||
|
||
private $locations; | ||
|
||
/** | ||
* @param LatLongValue[] $locations | ||
*/ | ||
public function __construct( array $locations ) { | ||
$this->locations = $locations; | ||
} | ||
|
||
/** | ||
* @param string $address | ||
* | ||
* @return LatLongValue|null | ||
*/ | ||
public function geocode( $address ) { | ||
if ( array_key_exists( $address, $this->locations ) ) { | ||
return $this->locations[$address]; | ||
} | ||
|
||
return null; | ||
} | ||
|
||
} |
This file contains 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,55 @@ | ||
<?php | ||
|
||
namespace Maps\Geocoders; | ||
|
||
use DataValues\Geo\Values\LatLongValue; | ||
use FileFetcher\FileFetcher; | ||
|
||
/** | ||
* Webservice documentation: http://wiki.openstreetmap.org/wiki/Nominatim | ||
* | ||
* @since 3.8 | ||
* | ||
* @licence GNU GPL v2+ | ||
* @author Peter Grassberger < petertheone@gmail.com > | ||
* @author Jeroen De Dauw < jeroendedauw@gmail.com > | ||
*/ | ||
class NominatimGeocoder implements Geocoder { | ||
|
||
private $fileFetcher; | ||
|
||
public function __construct( FileFetcher $fileFetcher ) { | ||
$this->fileFetcher = $fileFetcher; | ||
} | ||
|
||
/** | ||
* @param string $address | ||
* | ||
* @return LatLongValue|null | ||
*/ | ||
public function geocode( $address ) { | ||
$response = $this->fileFetcher->fetchFile( $this->getRequestUrl( $address ) ); | ||
|
||
$jsonResponse = json_decode( $response ); | ||
|
||
if ( !is_array( $jsonResponse ) || count( $jsonResponse ) < 1 ) { | ||
return null; | ||
} | ||
|
||
$location = $jsonResponse[0]; | ||
|
||
if ( !$location->lat || !$location->lon ) return null; | ||
|
||
return new LatLongValue( (float)$location->lat, (float)$location->lon ); | ||
} | ||
|
||
/** | ||
* @param string $address | ||
* | ||
* @return string | ||
*/ | ||
private function getRequestUrl( $address ) { | ||
return 'https://nominatim.openstreetmap.org/search?format=jsonv2&limit=1&q=' . urlencode( $address ); | ||
} | ||
|
||
} |
This file contains 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,14 @@ | ||
<?php | ||
|
||
if ( PHP_SAPI !== 'cli' ) { | ||
die( 'Not an entry point' ); | ||
} | ||
|
||
error_reporting( -1 ); | ||
ini_set( 'display_errors', 1 ); | ||
|
||
if ( !is_readable( __DIR__ . '/../vendor/autoload.php' ) ) { | ||
die( 'You need to install this package with Composer before you can run the tests' ); | ||
} | ||
|
||
require_once __DIR__ . '/../vendor/autoload.php'; |
Oops, something went wrong.