Compare commits
21 Commits
issue/13-r
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
| 0f6a3fed2b | |||
|
|
8f52f72679 | ||
|
|
555eaf4f2d | ||
|
|
d6f54eb17f | ||
| e302da0fa6 | |||
| 9246f74117 | |||
| 412c4eb579 | |||
| 9588938356 | |||
| 7cd78307f9 | |||
| 35a37a1eae | |||
| 9a7e6b01cb | |||
| 35d5c324e2 | |||
| dd2f8a76d6 | |||
| ab4bbd0574 | |||
| 2aa5cb731e | |||
| 2f54e01fdd | |||
| 40fc3f59f8 | |||
| d8ecb41ac3 | |||
|
|
214fcc2bac | ||
|
|
070279e8d4 | ||
|
|
10d1d3c318 |
19
CHANGELOG.md
Normal file
19
CHANGELOG.md
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
# Changelog
|
||||||
|
|
||||||
|
## [x.x.x] xxxx-xx-xx
|
||||||
|
|
||||||
|
## [2.0.2] 2025-05-08
|
||||||
|
- Updating composer to allow `doctrine/orm` version 2 or 3
|
||||||
|
- Update postcode in tests as the old one stopped working
|
||||||
|
|
||||||
|
## [2.0.1] 2024-10-09
|
||||||
|
- Fix bug that caused isGeocoded to return false if only one of the coordinates was zero
|
||||||
|
|
||||||
|
## [2.0.0] 2024-09-26
|
||||||
|
- Adjust how error messages are formatted
|
||||||
|
- Add getGeocodeData method to GeocodeInterface to allow for more abstraction in services
|
||||||
|
- Coordinates set to 0, 0 will cause isGeocoded to return false
|
||||||
|
|
||||||
|
## [1.0.0] 2024-08-05
|
||||||
|
- First major version release
|
||||||
|
|
||||||
13
README.md
13
README.md
@@ -1 +1,12 @@
|
|||||||
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 an instance of GeocodeInterface and attempts to return a `GeoCoordinates` object which contains the object's latitude and longitude.
|
||||||
|
|
||||||
|
# Usage
|
||||||
|
|
||||||
|
Use the `GeocodeTrait` an an entity to give it latitude and longitude fields.
|
||||||
|
|
||||||
|
If you wish to geocode an entity you must implement the `GeocodeInterface` interface, the trait will get you most of the way there but you must manually add a `getGeocodeData()` method. The simplest implementation is to return a postcode, but you can return a string of any data if it can be used to find a location.
|
||||||
|
|
||||||
|
|||||||
@@ -19,7 +19,7 @@
|
|||||||
"symfony/dependency-injection": "^7.1",
|
"symfony/dependency-injection": "^7.1",
|
||||||
"symfony/framework-bundle": "^7.1",
|
"symfony/framework-bundle": "^7.1",
|
||||||
"symfony/yaml": "^7.1",
|
"symfony/yaml": "^7.1",
|
||||||
"doctrine/orm": "^2.13"
|
"doctrine/orm": "^2|^3"
|
||||||
},
|
},
|
||||||
|
|
||||||
"require-dev": {
|
"require-dev": {
|
||||||
|
|||||||
@@ -6,11 +6,5 @@ namespace Pcm\GeocodeBundle\Exception;
|
|||||||
|
|
||||||
class GeocodeException extends \RuntimeException
|
class GeocodeException extends \RuntimeException
|
||||||
{
|
{
|
||||||
public function __construct(string $postcode, string $message)
|
|
||||||
{
|
|
||||||
$formattedMessage = sprintf("Failed to geocode a postcode \"%s\": %s", $postcode, $message);
|
|
||||||
|
|
||||||
parent::__construct($formattedMessage);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
10
src/Exception/MissingGeocodeDataException.php
Normal file
10
src/Exception/MissingGeocodeDataException.php
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Pcm\GeocodeBundle\Exception;
|
||||||
|
|
||||||
|
class MissingGeocodeDataException extends GeocodeException
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
@@ -6,11 +6,5 @@ namespace Pcm\GeocodeBundle\Exception;
|
|||||||
|
|
||||||
class NoResultsFoundException extends GeocodeException
|
class NoResultsFoundException extends GeocodeException
|
||||||
{
|
{
|
||||||
private const string MESSAGE = "No results found.";
|
|
||||||
|
|
||||||
public function __construct(string $postcode)
|
|
||||||
{
|
|
||||||
parent::__construct($postcode, self::MESSAGE);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -4,6 +4,9 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace Pcm\GeocodeBundle\Interface\Entity;
|
namespace Pcm\GeocodeBundle\Interface\Entity;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Defines latitude and longitude getters and setters
|
||||||
|
*/
|
||||||
interface GeocodeInterface
|
interface GeocodeInterface
|
||||||
{
|
{
|
||||||
public function getLatitude(): ?float;
|
public function getLatitude(): ?float;
|
||||||
@@ -14,5 +17,16 @@ interface GeocodeInterface
|
|||||||
|
|
||||||
public function setLongitude(float $lon): self;
|
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;
|
public function isGeocoded(): bool;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -5,11 +5,16 @@ declare(strict_types=1);
|
|||||||
namespace Pcm\GeocodeBundle\Service;
|
namespace Pcm\GeocodeBundle\Service;
|
||||||
|
|
||||||
use Pcm\GeocodeBundle\Exception\ApiErrorException;
|
use Pcm\GeocodeBundle\Exception\ApiErrorException;
|
||||||
|
use Pcm\GeocodeBundle\Exception\MissingGeocodeDataException;
|
||||||
use Pcm\GeocodeBundle\Exception\NoResultsFoundException;
|
use Pcm\GeocodeBundle\Exception\NoResultsFoundException;
|
||||||
|
use Pcm\GeocodeBundle\Interface\Entity\GeocodeInterface;
|
||||||
use Pcm\GeocodeBundle\Model\GeoCoordinates;
|
use Pcm\GeocodeBundle\Model\GeoCoordinates;
|
||||||
use Symfony\Contracts\HttpClient\HttpClientInterface;
|
use Symfony\Contracts\HttpClient\HttpClientInterface;
|
||||||
use Symfony\Contracts\HttpClient\ResponseInterface;
|
use Symfony\Contracts\HttpClient\ResponseInterface;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Find the geo-coordinates of a postcode
|
||||||
|
*/
|
||||||
final class Geocoder
|
final class Geocoder
|
||||||
{
|
{
|
||||||
private const string API_URL = "https://nominatim.openstreetmap.org/search";
|
private const string API_URL = "https://nominatim.openstreetmap.org/search";
|
||||||
@@ -17,23 +22,34 @@ final class Geocoder
|
|||||||
public function __construct(private HttpClientInterface $client) {}
|
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
|
* @param GeocodeInterface $entity
|
||||||
* @return null|GeoCoordinates
|
*
|
||||||
|
* @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 = trim($entity->getGeocodeData());
|
||||||
$client = $this->createClient();
|
$client = $this->createClient();
|
||||||
$response = $this->makeApiRequest($client, $postcode);
|
|
||||||
|
if ('' === $geocodeData) {
|
||||||
|
throw new MissingGeocodeDataException('No geocode data present.');
|
||||||
|
}
|
||||||
|
|
||||||
|
$response = $this->makeApiRequest($client, $geocodeData);
|
||||||
$data = $this->getDataFromResponse($response);
|
$data = $this->getDataFromResponse($response);
|
||||||
|
|
||||||
if (array_key_exists('error', $data)) {
|
if (array_key_exists('error', $data)) {
|
||||||
throw new ApiErrorException($postcode, $data['error']['message']);
|
throw new ApiErrorException($data['error']['message']);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (empty($data)) {
|
if (empty($data)) {
|
||||||
throw new NoResultsFoundException($postcode, "No results found.");
|
$message = sprintf('No results found with geocode data "%s".', $geocodeData);
|
||||||
|
throw new NoResultsFoundException($message);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this->createGeoCoordinates($data);
|
return $this->createGeoCoordinates($data);
|
||||||
@@ -46,7 +62,7 @@ final class Geocoder
|
|||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
private function makeApiRequest(HttpClientInterface $client, string $postcode): ResponseInterface
|
private function makeApiRequest(HttpClientInterface $client, string $geocodeData): ResponseInterface
|
||||||
{
|
{
|
||||||
return $client->request(
|
return $client->request(
|
||||||
method: 'GET',
|
method: 'GET',
|
||||||
@@ -54,7 +70,7 @@ final class Geocoder
|
|||||||
options: [
|
options: [
|
||||||
'query' => [
|
'query' => [
|
||||||
'format' => 'json',
|
'format' => 'json',
|
||||||
'postalcode' => $postcode
|
'q' => $geocodeData
|
||||||
]
|
]
|
||||||
]
|
]
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -43,14 +43,13 @@ trait GeocodeTrait
|
|||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns true if both latitude and longitude have been set
|
|
||||||
*
|
|
||||||
* @return bool
|
|
||||||
*/
|
|
||||||
public function isGeocoded(): bool
|
public function isGeocoded(): bool
|
||||||
{
|
{
|
||||||
return null !== $this->getLatitude() && null !== $this->getLongitude();
|
$latIsntNull = null !== $this->getLatitude();
|
||||||
|
$longIsntNull = null !== $this->getLongitude();
|
||||||
|
$bothArentZero = !(0.0 === $this->getLatitude() && 0.0 === $this->getLongitude());
|
||||||
|
|
||||||
|
return $latIsntNull && $longIsntNull && $bothArentZero;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -4,9 +4,9 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace Pcm\GeocodeBundle\Tests;
|
namespace Pcm\GeocodeBundle\Tests;
|
||||||
|
|
||||||
|
use Pcm\GeocodeBundle\Exception\MissingGeocodeDataException;
|
||||||
use Pcm\GeocodeBundle\Interface\Entity\GeocodeInterface;
|
use Pcm\GeocodeBundle\Interface\Entity\GeocodeInterface;
|
||||||
use Pcm\GeocodeBundle\Trait\Entity\GeocodeTrait;
|
use Pcm\GeocodeBundle\Trait\Entity\GeocodeTrait;
|
||||||
use Pcm\GeocodeBundle\Exception\ApiErrorException;
|
|
||||||
use Pcm\GeocodeBundle\Exception\NoResultsFoundException;
|
use Pcm\GeocodeBundle\Exception\NoResultsFoundException;
|
||||||
use Pcm\GeocodeBundle\Model\GeoCoordinates;
|
use Pcm\GeocodeBundle\Model\GeoCoordinates;
|
||||||
use Pcm\GeocodeBundle\Service\Geocoder;
|
use Pcm\GeocodeBundle\Service\Geocoder;
|
||||||
@@ -16,10 +16,10 @@ use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
|
|||||||
/**
|
/**
|
||||||
* We sleep 1 second after API calls to prevent breaking the API T&Cs.
|
* 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
|
// Yes this is a real postcode
|
||||||
private const string POSTCODE = 'SW1A 1AA';
|
private const string POSTCODE = 'ABC 123';
|
||||||
|
|
||||||
private Geocoder $geocoder;
|
private Geocoder $geocoder;
|
||||||
|
|
||||||
@@ -30,38 +30,36 @@ final class GeocodeTest extends KernelTestCase
|
|||||||
$this->geocoder = $kernel->getContainer()->get('pcm_geocode.geocoder');
|
$this->geocoder = $kernel->getContainer()->get('pcm_geocode.geocoder');
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testGeocodeInstance(): void
|
public function testGeocodeThrowsOnEmptyInput(): void
|
||||||
{
|
|
||||||
$this->assertInstanceOf(Geocoder::class, $this->geocoder);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testGeocodePostcodeThrowsOnEmptyInput(): void
|
|
||||||
{
|
{
|
||||||
sleep(1);
|
sleep(1);
|
||||||
$this->expectException(ApiErrorException::class);
|
$this->expectException(MissingGeocodeDataException::class);
|
||||||
$this->expectExceptionMessageMatches("/Nothing to search for.$/");
|
$entity = $this->createEntity('');
|
||||||
$this->geocoder->geocodePostcode('');
|
$this->expectExceptionMessageMatches("/No geocode data present./");
|
||||||
|
$this->geocoder->geocode($entity);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testGeocodePostcodeThrowsOnInvalidPostcode(): void
|
public function testGeocodeThrowsOnInvalidPostcode(): void
|
||||||
{
|
{
|
||||||
sleep(1);
|
sleep(1);
|
||||||
$this->expectException(NoResultsFoundException::class);
|
$this->expectException(NoResultsFoundException::class);
|
||||||
$this->expectExceptionMessageMatches("/No results found.$/");
|
$this->expectExceptionMessageMatches("/No results found with geocode data \"Invalid Postcode\"./");
|
||||||
$this->geocoder->geocodePostcode('ZZZZZZZZZZZZ');
|
$entity = $this->createEntity('Invalid Postcode');
|
||||||
|
$this->geocoder->geocode($entity);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testGeocodePostcodeReturnsGeoCoordinates(): GeoCoordinates
|
public function testGeocodeReturnsGeoCoordinates(): GeoCoordinates
|
||||||
{
|
{
|
||||||
sleep(1);
|
sleep(1);
|
||||||
$geoCoords = $this->geocoder->geocodePostcode(self::POSTCODE);
|
$entity = $this->createEntity(self::POSTCODE);
|
||||||
|
$geoCoords = $this->geocoder->geocode($entity);
|
||||||
$this->assertNotNull($geoCoords);
|
$this->assertNotNull($geoCoords);
|
||||||
|
|
||||||
return $geoCoords;
|
return $geoCoords;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @depends testGeocodePostcodeReturnsGeoCoordinates
|
* @depends testGeocodeReturnsGeoCoordinates
|
||||||
*/
|
*/
|
||||||
public function testCoordinatesAreSet(GeoCoordinates $geoCoordinates): void
|
public function testCoordinatesAreSet(GeoCoordinates $geoCoordinates): void
|
||||||
{
|
{
|
||||||
@@ -76,5 +74,23 @@ final class GeocodeTest extends KernelTestCase
|
|||||||
use GeocodeTrait;
|
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,10 +75,31 @@ final class GeocodeTraitTest extends TestCase
|
|||||||
$this->assertTrue($this->obj->isGeocoded());
|
$this->assertTrue($this->obj->isGeocoded());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testIsGeocodeReturnsTrueIfLatAndLonAreBothZero(): void
|
public function testIsGeocodedReturnsFalseIfLatAndLonAreBothZero(): void
|
||||||
{
|
{
|
||||||
$this->obj->setLatitude(0.000);
|
$this->obj->setLatitude(0.000);
|
||||||
$this->obj->setLongitude(0.000);
|
$this->obj->setLongitude(0.000);
|
||||||
|
$this->assertFalse($this->obj->isGeocoded());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testIsGeocodedReturnsFalseIfLatAndLonAreBothZeroInts(): void
|
||||||
|
{
|
||||||
|
$this->obj->setLatitude(0);
|
||||||
|
$this->obj->setLongitude(0);
|
||||||
|
$this->assertFalse($this->obj->isGeocoded());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testIsGeocodedReturnsTrueIfLongitudeIsZeroAndLatIsNot(): void
|
||||||
|
{
|
||||||
|
$this->obj->setLatitude(0.1);
|
||||||
|
$this->obj->setLongitude(0);
|
||||||
|
$this->assertTrue($this->obj->isGeocoded());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testIsGeocodedReturnsTrueIfLatitudeIsZeroAndLongIsNot(): void
|
||||||
|
{
|
||||||
|
$this->obj->setLatitude(0);
|
||||||
|
$this->obj->setLongitude(0.2);
|
||||||
$this->assertTrue($this->obj->isGeocoded());
|
$this->assertTrue($this->obj->isGeocoded());
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -87,6 +108,11 @@ final class GeocodeTraitTest extends TestCase
|
|||||||
return new class implements GeocodeInterface
|
return new class implements GeocodeInterface
|
||||||
{
|
{
|
||||||
use GeocodeTrait;
|
use GeocodeTrait;
|
||||||
|
|
||||||
|
public function getGeocodeData(): string
|
||||||
|
{
|
||||||
|
return '';
|
||||||
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user