Compare commits
12 Commits
issue/13-r
...
issue/15-a
| Author | SHA1 | Date | |
|---|---|---|---|
| 35a37a1eae | |||
| 9a7e6b01cb | |||
| 35d5c324e2 | |||
| dd2f8a76d6 | |||
| ab4bbd0574 | |||
| 2aa5cb731e | |||
| 2f54e01fdd | |||
| 40fc3f59f8 | |||
| d8ecb41ac3 | |||
|
|
214fcc2bac | ||
|
|
070279e8d4 | ||
|
|
10d1d3c318 |
8
CHANGELOG.md
Normal file
8
CHANGELOG.md
Normal file
@@ -0,0 +1,8 @@
|
||||
# Changelog
|
||||
|
||||
## [x.x.x] xxxx-xx-xx
|
||||
- Coordinates set to 0, 0 will cause isGeocoded to return false
|
||||
|
||||
## [1.0.0] 2024-08-05
|
||||
- First major version release
|
||||
|
||||
@@ -1 +1,6 @@
|
||||
Readme
|
||||
# PCM Geocode Bundle
|
||||
|
||||
Provides an interface/trait combo to add latitude and longitude fields to an entity.
|
||||
|
||||
Also included is a `Geocoder` service which accepts a postcode and attempts to return a `GeoCoordinates` object which contains the latitude and longitude of the postcode.
|
||||
|
||||
|
||||
@@ -4,6 +4,9 @@ declare(strict_types=1);
|
||||
|
||||
namespace Pcm\GeocodeBundle\Interface\Entity;
|
||||
|
||||
/**
|
||||
* Defines latitude and longitude getters and setters
|
||||
*/
|
||||
interface GeocodeInterface
|
||||
{
|
||||
public function getLatitude(): ?float;
|
||||
@@ -14,5 +17,16 @@ interface GeocodeInterface
|
||||
|
||||
public function setLongitude(float $lon): self;
|
||||
|
||||
/**
|
||||
* Used by {@see Pcm\GeocodeBundle\Service\Geocoder} to retrieve raw data for geocoding.
|
||||
*
|
||||
* @return string Data to use when geocodind an entity
|
||||
*/
|
||||
public function getGeocodeData(): string;
|
||||
|
||||
/**
|
||||
* @return bool True if both latitude and longitude are set and are not (0, 0)
|
||||
*/
|
||||
public function isGeocoded(): bool;
|
||||
}
|
||||
|
||||
|
||||
@@ -6,10 +6,14 @@ namespace Pcm\GeocodeBundle\Service;
|
||||
|
||||
use Pcm\GeocodeBundle\Exception\ApiErrorException;
|
||||
use Pcm\GeocodeBundle\Exception\NoResultsFoundException;
|
||||
use Pcm\GeocodeBundle\Interface\Entity\GeocodeInterface;
|
||||
use Pcm\GeocodeBundle\Model\GeoCoordinates;
|
||||
use Symfony\Contracts\HttpClient\HttpClientInterface;
|
||||
use Symfony\Contracts\HttpClient\ResponseInterface;
|
||||
|
||||
/**
|
||||
* Find the geo-coordinates of a postcode
|
||||
*/
|
||||
final class Geocoder
|
||||
{
|
||||
private const string API_URL = "https://nominatim.openstreetmap.org/search";
|
||||
@@ -17,23 +21,28 @@ final class Geocoder
|
||||
public function __construct(private HttpClientInterface $client) {}
|
||||
|
||||
/**
|
||||
* Convert a postcode into latitude and longitude. Returns null if conversion failed.
|
||||
* Find and return the geo-coordinates of an entity
|
||||
*
|
||||
* @param string $postcode
|
||||
* @return null|GeoCoordinates
|
||||
* @param GeocodeInterface $entity
|
||||
*
|
||||
* @return GeoCoordinates
|
||||
*
|
||||
* @throws NoResultsFoundException when no results were found for the provided postcode
|
||||
* @throws ApiErrorException when the API response contains an error
|
||||
*/
|
||||
public function geocodePostcode(string $postcode): ?GeoCoordinates
|
||||
public function geocode(GeocodeInterface $entity): GeoCoordinates
|
||||
{
|
||||
$geocodeData = $entity->getGeocodeData();
|
||||
$client = $this->createClient();
|
||||
$response = $this->makeApiRequest($client, $postcode);
|
||||
$response = $this->makeApiRequest($client, $geocodeData);
|
||||
$data = $this->getDataFromResponse($response);
|
||||
|
||||
if (array_key_exists('error', $data)) {
|
||||
throw new ApiErrorException($postcode, $data['error']['message']);
|
||||
throw new ApiErrorException($geocodeData, $data['error']['message']);
|
||||
}
|
||||
|
||||
if (empty($data)) {
|
||||
throw new NoResultsFoundException($postcode, "No results found.");
|
||||
throw new NoResultsFoundException($geocodeData, "No results found.");
|
||||
}
|
||||
|
||||
return $this->createGeoCoordinates($data);
|
||||
@@ -46,7 +55,7 @@ final class Geocoder
|
||||
]);
|
||||
}
|
||||
|
||||
private function makeApiRequest(HttpClientInterface $client, string $postcode): ResponseInterface
|
||||
private function makeApiRequest(HttpClientInterface $client, string $geocodeData): ResponseInterface
|
||||
{
|
||||
return $client->request(
|
||||
method: 'GET',
|
||||
@@ -54,7 +63,7 @@ final class Geocoder
|
||||
options: [
|
||||
'query' => [
|
||||
'format' => 'json',
|
||||
'postalcode' => $postcode
|
||||
'q' => $geocodeData
|
||||
]
|
||||
]
|
||||
);
|
||||
|
||||
@@ -43,14 +43,9 @@ trait GeocodeTrait
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if both latitude and longitude have been set
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isGeocoded(): bool
|
||||
{
|
||||
return null !== $this->getLatitude() && null !== $this->getLongitude();
|
||||
return null !== $this->getLatitude() && null !== $this->getLongitude() && (0.0 !== $this->getLatitude() && 0.0 !== $this->getLongitude());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -16,7 +16,7 @@ use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
|
||||
/**
|
||||
* We sleep 1 second after API calls to prevent breaking the API T&Cs.
|
||||
*/
|
||||
final class GeocodeTest extends KernelTestCase
|
||||
final class GeocoderTest extends KernelTestCase
|
||||
{
|
||||
// Buckingham Palace
|
||||
private const string POSTCODE = 'SW1A 1AA';
|
||||
@@ -30,38 +30,36 @@ final class GeocodeTest extends KernelTestCase
|
||||
$this->geocoder = $kernel->getContainer()->get('pcm_geocode.geocoder');
|
||||
}
|
||||
|
||||
public function testGeocodeInstance(): void
|
||||
{
|
||||
$this->assertInstanceOf(Geocoder::class, $this->geocoder);
|
||||
}
|
||||
|
||||
public function testGeocodePostcodeThrowsOnEmptyInput(): void
|
||||
public function testGeocodeThrowsOnEmptyInput(): void
|
||||
{
|
||||
sleep(1);
|
||||
$this->expectException(ApiErrorException::class);
|
||||
$entity = $this->createEntity('');
|
||||
$this->expectExceptionMessageMatches("/Nothing to search for.$/");
|
||||
$this->geocoder->geocodePostcode('');
|
||||
$this->geocoder->geocode($entity);
|
||||
}
|
||||
|
||||
public function testGeocodePostcodeThrowsOnInvalidPostcode(): void
|
||||
public function testGeocodeThrowsOnInvalidPostcode(): void
|
||||
{
|
||||
sleep(1);
|
||||
$this->expectException(NoResultsFoundException::class);
|
||||
$this->expectExceptionMessageMatches("/No results found.$/");
|
||||
$this->geocoder->geocodePostcode('ZZZZZZZZZZZZ');
|
||||
$entity = $this->createEntity('Invalid Postcode');
|
||||
$this->geocoder->geocode($entity);
|
||||
}
|
||||
|
||||
public function testGeocodePostcodeReturnsGeoCoordinates(): GeoCoordinates
|
||||
public function testGeocodeReturnsGeoCoordinates(): GeoCoordinates
|
||||
{
|
||||
sleep(1);
|
||||
$geoCoords = $this->geocoder->geocodePostcode(self::POSTCODE);
|
||||
$entity = $this->createEntity(self::POSTCODE);
|
||||
$geoCoords = $this->geocoder->geocode($entity);
|
||||
$this->assertNotNull($geoCoords);
|
||||
|
||||
return $geoCoords;
|
||||
}
|
||||
|
||||
/**
|
||||
* @depends testGeocodePostcodeReturnsGeoCoordinates
|
||||
* @depends testGeocodeReturnsGeoCoordinates
|
||||
*/
|
||||
public function testCoordinatesAreSet(GeoCoordinates $geoCoordinates): void
|
||||
{
|
||||
@@ -76,5 +74,23 @@ final class GeocodeTest extends KernelTestCase
|
||||
use GeocodeTrait;
|
||||
};
|
||||
}
|
||||
|
||||
private function createEntity(string $data): GeocodeInterface
|
||||
{
|
||||
$entity = new class implements GeocodeInterface {
|
||||
use GeocodeTrait;
|
||||
|
||||
public string $data;
|
||||
|
||||
public function getGeocodeData(): string
|
||||
{
|
||||
return $this->data;
|
||||
}
|
||||
};
|
||||
|
||||
$entity->data = $data;
|
||||
|
||||
return $entity;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -75,11 +75,18 @@ final class GeocodeTraitTest extends TestCase
|
||||
$this->assertTrue($this->obj->isGeocoded());
|
||||
}
|
||||
|
||||
public function testIsGeocodeReturnsTrueIfLatAndLonAreBothZero(): void
|
||||
public function testIsGeocodedReturnsFalseIfLatAndLonAreBothZero(): void
|
||||
{
|
||||
$this->obj->setLatitude(0.000);
|
||||
$this->obj->setLongitude(0.000);
|
||||
$this->assertTrue($this->obj->isGeocoded());
|
||||
$this->assertFalse($this->obj->isGeocoded());
|
||||
}
|
||||
|
||||
public function testIsGeocodedReturnsFalseIfLatAndLonAreBothZeroInts(): void
|
||||
{
|
||||
$this->obj->setLatitude(0);
|
||||
$this->obj->setLongitude(0);
|
||||
$this->assertFalse($this->obj->isGeocoded());
|
||||
}
|
||||
|
||||
private function getTraitObject(): GeocodeInterface
|
||||
@@ -87,6 +94,11 @@ final class GeocodeTraitTest extends TestCase
|
||||
return new class implements GeocodeInterface
|
||||
{
|
||||
use GeocodeTrait;
|
||||
|
||||
public function getGeocodeData(): string
|
||||
{
|
||||
return '';
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user