Skip to content
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

[WIP] Update package & getGeocodedData method #122

Closed
wants to merge 3 commits into from
Closed
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
/bin
/composer.lock
/vendor
/composer.phar

# PHPUnit
/phpunit.xml
Expand Down
10 changes: 5 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@
}
],
"require": {
"php": ">=5.3.0",
"egeloen/json-builder": "~1.0.0"
"php": ">=5.5.9",
"egeloen/json-builder": "~2.0.1"
},
"require-dev": {
"phpunit/phpunit": "~4.0",
"phpunit/phpunit": "5.3.*",
"widop/http-adapter": "~1.1.0",
"willdurand/geocoder": "~2.0",
"satooshi/php-coveralls": "~0.6"
"willdurand/geocoder": "v3.3.*",
"satooshi/php-coveralls": "~1.0.1"
},
"suggest": {
"widop/http-adapter": "Allows to use services",
Expand Down
52 changes: 44 additions & 8 deletions src/Services/Geocoding/GeocoderProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@

namespace Ivory\GoogleMap\Services\Geocoding;

use Geocoder\HttpAdapter\HttpAdapterInterface;
use Geocoder\Provider\AbstractProvider;
use Geocoder\Provider\ProviderInterface;
use Geocoder\Provider\AbstractHttpProvider;
use Geocoder\Provider\LocaleAwareProvider;
use Ivory\GoogleMap\Base\Bound;
use Ivory\GoogleMap\Base\Coordinate;
use Ivory\GoogleMap\Exception\GeocodingException;
Expand All @@ -23,13 +22,16 @@
use Ivory\GoogleMap\Services\Geocoding\Result\GeocoderResponse;
use Ivory\GoogleMap\Services\Geocoding\Result\GeocoderResult;
use Ivory\GoogleMap\Services\Utils\XmlParser;
use Ivory\HttpAdapter\HttpAdapterInterface;
use Ivory\HttpAdapter\Message\InternalRequest;
use Ivory\HttpAdapter\Message\Request;

/**
* Geocoder provider.
*
* @author GeLo <geloen.eric@gmail.com>
*/
class GeocoderProvider extends AbstractProvider implements ProviderInterface
class GeocoderProvider extends AbstractHttpProvider implements LocaleAwareProvider
{
/** @var string */
protected $url;
Expand All @@ -46,17 +48,21 @@ class GeocoderProvider extends AbstractProvider implements ProviderInterface
/** @var \Ivory\GoogleMap\Services\BusinessAccount */
protected $businessAccount;

/** @var string|null */
protected $locale;

/**
* {@inheritdoc}
*/
public function __construct(HttpAdapterInterface $adapter, $locale = null)
{
parent::__construct($adapter, $locale);
parent::__construct($adapter);

$this->setUrl('http://maps.googleapis.com/maps/api/geocode');
$this->setHttps(false);
$this->setFormat('json');
$this->setXmlParser(new XmlParser());
$this->setLocale($locale);
}

/**
Expand Down Expand Up @@ -212,7 +218,9 @@ public function getGeocodedData($request)
}

$url = $this->generateUrl($geocoderRequest);
$response = $this->getAdapter()->getContent($url);
$request = new Request($url, 'get');

$response = $this->getAdapter()->sendRequest($request);

if ($response === null) {
throw GeocodingException::invalidServiceResult();
Expand Down Expand Up @@ -314,10 +322,10 @@ protected function signUrl($url)
protected function parse($response)
{
if ($this->format == 'json') {
return $this->parseJSON($response);
return $this->parseJSON($response->getBody()->__toString());
}

return $this->parseXML($response);
return $this->parseXML($response->getBody()->__toString());
}

/**
Expand Down Expand Up @@ -453,4 +461,32 @@ protected function buildGeocoderGeometry(\stdClass $geocoderGeometry)

return new GeocoderGeometry($location, $locationType, $viewport, $bound);
}

/**
* {@inheritDoc}
*/
public function getLocale()
{
return $this->locale;
}

/**
* {@inheritDoc}
*/
public function setLocale($locale)
{
$this->locale = $locale;

return $this;
}

public function geocode($value)
{
// TODO: Implement geocode() method.
}

public function reverse($latitude, $longitude)
{
// TODO: Implement reverse() method.
}
}
6 changes: 4 additions & 2 deletions tests/Services/Geocoding/GeocoderProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@

namespace Ivory\Tests\GoogleMap\Services\Geocoding;

use Geocoder\HttpAdapter\CurlHttpAdapter;
use Ivory\GoogleMap\Services\Geocoding\GeocoderProvider;
use Ivory\GoogleMap\Services\Geocoding\GeocoderRequest;
use Ivory\GoogleMap\Services\Geocoding\Result\GeocoderStatus;
use Ivory\HttpAdapter\BuzzHttpAdapter;
use Ivory\HttpAdapter\CurlHttpAdapter;
use Ivory\HttpAdapter\Guzzle6HttpAdapter;

/**
* Geocoder provider test.
Expand Down Expand Up @@ -219,7 +221,7 @@ public function testGeocodedDataWithInvalidResult()
$httpAdapterMock = $this->getMock('Geocoder\HttpAdapter\HttpAdapterInterface');
$httpAdapterMock
->expects($this->once())
->method('getContent')
->method('sendRequest')
->will($this->returnValue(null));

$this->geocoderProvider = new GeocoderProvider($httpAdapterMock);
Expand Down