Skip to content

Commit

Permalink
REFACTOR Geocoder libraries (#57)
Browse files Browse the repository at this point in the history
* COMPOSER remove branch alias

* README updates

* REFACTOR rework geocoder libraries

* guzzle adapter

* REFACTOR Geocoder libraries

reworked how coordintes are retrieved from the response

ping #56

* phpcs

* Revert "phpcs"

This reverts commit 18e557b.

* phpcs
  • Loading branch information
jsirish authored Sep 16, 2022
1 parent d412a4b commit 93476c7
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 18 deletions.
9 changes: 3 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
"license": "BSD-3-Clause",
"require": {
"dynamic/silverstripe-country-dropdown-field": "^1.0",
"willdurand/geocoder": "^4.0"
"geocoder-php/google-maps-provider": "^4.7",
"guzzlehttp/guzzle": "^7.4",
"php-http/guzzle7-adapter": "^1.0"
},
"require-dev": {
"silverstripe/recipe-testing": "^2",
Expand All @@ -37,11 +39,6 @@
},
"minimum-stability": "dev",
"prefer-stable": true,
"extra": {
"branch-alias": {
"dev-master": "1.x-dev"
}
},
"scripts": {
"lint": "vendor/bin/phpcs src/ tests/",
"lint-clean": "vendor/bin/phpcbf src/ tests/"
Expand Down
6 changes: 4 additions & 2 deletions src/AddressDataExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -297,8 +297,10 @@ public function onBeforeWrite()
if ($address = $this->getFullAddress()) {
$geocoder = new GoogleGeocoder($address);
$response = $geocoder->getResult();
$this->owner->Lat = $response->getLatitude();
$this->owner->Lng = $response->getLongitude();
$dumper = new \Geocoder\Dumper\GeoArray();
$result = $dumper->dump($response);
$this->owner->Lat = $result['geometry']['coordinates'][0];
$this->owner->Lng = $result['geometry']['coordinates'][1];
}
}
}
Expand Down
6 changes: 4 additions & 2 deletions src/DistanceDataExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,10 @@ public function augmentSQL(SQLSelect $query, DataQuery $dataQuery = null)
if ($address) { // on frontend
$geocoder = new GoogleGeocoder($address);
$response = $geocoder->getResult();
$Lat = $response->getLatitude();
$Lng = $response->getLongitude();
$dumper = new \Geocoder\Dumper\GeoArray();
$result = $dumper->dump($response);
$Lat = $this->owner->Lat = $result['geometry']['coordinates'][0];
$Lng = $this->owner->Lng = $result['geometry']['coordinates'][1];

$query
->addSelect(array(
Expand Down
2 changes: 1 addition & 1 deletion src/GeocoderAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class GeocoderAdapter
public function __construct($adapter = null)
{
if ($adapter === null) {
$adapter = new CurlHttpAdapter();
$adapter = new \GuzzleHttp\Client();
}
$this->setAdapter($adapter);
}
Expand Down
13 changes: 6 additions & 7 deletions src/GoogleGeocoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Dynamic\SilverStripeGeocoder;

use \Geocoder\Provider\GoogleMaps;
use Geocoder\Provider\GoogleMaps\GoogleMaps;
use \SilverStripe\Core\Config\Config;

/**
Expand All @@ -27,15 +27,14 @@ class GoogleGeocoder
*/
public function __construct($address)
{
$adapter = new GeocoderAdapter();
$adapter = $adapter->getAdapter();
$geocoder = new GoogleMaps(
$adapter,
$httpClient = new GeocoderAdapter();
$httpClient = $httpClient->getAdapter();
$provider = new \Geocoder\Provider\GoogleMaps\GoogleMaps(
$httpClient,
null,
null,
true,
Config::inst()->get(GoogleGeocoder::class, 'geocoder_api_key')
);
$geocoder = new \Geocoder\StatefulGeocoder($provider, 'en');

$this->setClient($geocoder);
$this->setResults($address);
Expand Down

0 comments on commit 93476c7

Please sign in to comment.