Compare commits
9 Commits
issue/3-ma
...
1.0.0
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
214fcc2bac | ||
|
|
070279e8d4 | ||
|
|
10d1d3c318 | ||
|
|
8b662ebfa3 | ||
|
|
a6c8c4cc0c | ||
|
|
fcda1e931c | ||
|
|
e4222229c4 | ||
|
|
df96a8c1ed | ||
|
|
4d936a6279 |
5
CHANGELOG.md
Normal file
5
CHANGELOG.md
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
# Changelog
|
||||||
|
|
||||||
|
## [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.
|
||||||
|
|
||||||
|
|||||||
10
src/Exception/ApiErrorException.php
Normal file
10
src/Exception/ApiErrorException.php
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Pcm\GeocodeBundle\Exception;
|
||||||
|
|
||||||
|
class ApiErrorException extends GeocodeException
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
16
src/Exception/GeocodeException.php
Normal file
16
src/Exception/GeocodeException.php
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Pcm\GeocodeBundle\Exception;
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
16
src/Exception/NoResultsFoundException.php
Normal file
16
src/Exception/NoResultsFoundException.php
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Pcm\GeocodeBundle\Exception;
|
||||||
|
|
||||||
|
class NoResultsFoundException extends GeocodeException
|
||||||
|
{
|
||||||
|
private const string MESSAGE = "No results found.";
|
||||||
|
|
||||||
|
public function __construct(string $postcode)
|
||||||
|
{
|
||||||
|
parent::__construct($postcode, self::MESSAGE);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@@ -2,9 +2,12 @@
|
|||||||
|
|
||||||
declare(strict_types=1);
|
declare(strict_types=1);
|
||||||
|
|
||||||
namespace Pcm\GeocodeBundle\Entity\Interface;
|
namespace Pcm\GeocodeBundle\Interface\Entity;
|
||||||
|
|
||||||
interface MappableInterface
|
/**
|
||||||
|
* Defines latitude and longitude getters and setters
|
||||||
|
*/
|
||||||
|
interface GeocodeInterface
|
||||||
{
|
{
|
||||||
public function getLatitude(): ?float;
|
public function getLatitude(): ?float;
|
||||||
|
|
||||||
@@ -14,5 +17,9 @@ interface MappableInterface
|
|||||||
|
|
||||||
public function setLongitude(float $lon): self;
|
public function setLongitude(float $lon): self;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return bool True if both latitude and longitude is set
|
||||||
|
*/
|
||||||
public function isGeocoded(): bool;
|
public function isGeocoded(): bool;
|
||||||
}
|
}
|
||||||
|
|
||||||
16
src/Model/GeoCoordinates.php
Normal file
16
src/Model/GeoCoordinates.php
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Pcm\GeocodeBundle\Model;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A model for accessing latitude and longitude values.
|
||||||
|
*
|
||||||
|
* @package Pcm\GeocodeBundle
|
||||||
|
*/
|
||||||
|
final class GeoCoordinates
|
||||||
|
{
|
||||||
|
public function __construct(readonly public float $latitude, readonly public float $longitude) {}
|
||||||
|
}
|
||||||
|
|
||||||
@@ -1,26 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
declare(strict_types=1);
|
|
||||||
|
|
||||||
namespace Pcm\GeocodeBundle\Model;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* A model for accessing latitude and longitude values
|
|
||||||
*
|
|
||||||
* @package Pcm\GeocodeBundle
|
|
||||||
*/
|
|
||||||
final class LatLongModel
|
|
||||||
{
|
|
||||||
public function __construct(private float $latitude, private float $longitude) {}
|
|
||||||
|
|
||||||
public function getLatitude(): float
|
|
||||||
{
|
|
||||||
return $this->latitude;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function getLongitude(): float
|
|
||||||
{
|
|
||||||
return $this->longitude;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@@ -4,10 +4,15 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace Pcm\GeocodeBundle\Service;
|
namespace Pcm\GeocodeBundle\Service;
|
||||||
|
|
||||||
use Pcm\GeocodeBundle\Model\LatLongModel;
|
use Pcm\GeocodeBundle\Exception\ApiErrorException;
|
||||||
|
use Pcm\GeocodeBundle\Exception\NoResultsFoundException;
|
||||||
|
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";
|
||||||
@@ -15,26 +20,30 @@ 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 a postcode
|
||||||
*
|
*
|
||||||
* @param string $postcode
|
* @param string $postcode
|
||||||
* @return null|LatLongModel
|
*
|
||||||
|
* @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): ?LatLongModel
|
public function geocodePostcode(string $postcode): GeoCoordinates
|
||||||
{
|
{
|
||||||
$client = $this->createClient();
|
$client = $this->createClient();
|
||||||
$response = $this->makeApiRequest($client, $postcode);
|
$response = $this->makeApiRequest($client, $postcode);
|
||||||
$data = $this->getDataFromResponse($response);
|
$data = $this->getDataFromResponse($response);
|
||||||
|
|
||||||
if (empty($data)) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (array_key_exists('error', $data)) {
|
if (array_key_exists('error', $data)) {
|
||||||
return null;
|
throw new ApiErrorException($postcode, $data['error']['message']);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this->createLatLongModel($data);
|
if (empty($data)) {
|
||||||
|
throw new NoResultsFoundException($postcode, "No results found.");
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->createGeoCoordinates($data);
|
||||||
}
|
}
|
||||||
|
|
||||||
private function createClient(): HttpClientInterface
|
private function createClient(): HttpClientInterface
|
||||||
@@ -63,12 +72,12 @@ final class Geocoder
|
|||||||
return $response->toArray(false);
|
return $response->toArray(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
private function createLatLongModel(array $data): LatLongModel
|
private function createGeoCoordinates(array $data): GeoCoordinates
|
||||||
{
|
{
|
||||||
$lat = $this->getLatitudeFromData($data);
|
$lat = $this->getLatitudeFromData($data);
|
||||||
$long = $this->getLongitudeFromData($data);
|
$long = $this->getLongitudeFromData($data);
|
||||||
|
|
||||||
return new LatLongModel($lat, $long);
|
return new GeoCoordinates($lat, $long);
|
||||||
}
|
}
|
||||||
|
|
||||||
private function getLatitudeFromData(array $data): float
|
private function getLatitudeFromData(array $data): float
|
||||||
@@ -81,3 +90,4 @@ final class Geocoder
|
|||||||
return (float) $data[0]['lon'];
|
return (float) $data[0]['lon'];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -2,18 +2,16 @@
|
|||||||
|
|
||||||
declare(strict_types=1);
|
declare(strict_types=1);
|
||||||
|
|
||||||
namespace Pcm\GeocodeBundle\Entity\Trait;
|
namespace Pcm\GeocodeBundle\Trait\Entity;
|
||||||
|
|
||||||
use Doctrine\ORM\Mapping as ORM;
|
use Doctrine\ORM\Mapping as ORM;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Allows an entity to be mapped via latitude and longitude coordinates
|
* Implementation of {@see Pcm\GeocodeBundle\Interface\Entity\GeocodeInterface}
|
||||||
*
|
|
||||||
* Use MappableInterface
|
|
||||||
*
|
*
|
||||||
* @package Pcm\GeocodeBundle
|
* @package Pcm\GeocodeBundle
|
||||||
*/
|
*/
|
||||||
trait MappableTrait
|
trait GeocodeTrait
|
||||||
{
|
{
|
||||||
#[ORM\Column(type: 'decimal', precision: 10, scale: 6, nullable: true)]
|
#[ORM\Column(type: 'decimal', precision: 10, scale: 6, nullable: true)]
|
||||||
private ?float $latitude = null;
|
private ?float $latitude = null;
|
||||||
@@ -45,13 +43,9 @@ trait MappableTrait
|
|||||||
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();
|
return null !== $this->getLatitude() && null !== $this->getLongitude();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -4,9 +4,11 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace Pcm\GeocodeBundle\Tests;
|
namespace Pcm\GeocodeBundle\Tests;
|
||||||
|
|
||||||
use Pcm\GeocodeBundle\Entity\Interface\MappableInterface;
|
use Pcm\GeocodeBundle\Interface\Entity\GeocodeInterface;
|
||||||
use Pcm\GeocodeBundle\Entity\Trait\MappableTrait;
|
use Pcm\GeocodeBundle\Trait\Entity\GeocodeTrait;
|
||||||
use Pcm\GeocodeBundle\Model\LatLongModel;
|
use Pcm\GeocodeBundle\Exception\ApiErrorException;
|
||||||
|
use Pcm\GeocodeBundle\Exception\NoResultsFoundException;
|
||||||
|
use Pcm\GeocodeBundle\Model\GeoCoordinates;
|
||||||
use Pcm\GeocodeBundle\Service\Geocoder;
|
use Pcm\GeocodeBundle\Service\Geocoder;
|
||||||
use Pcm\GeocodeBundle\Tests\AppKernel;
|
use Pcm\GeocodeBundle\Tests\AppKernel;
|
||||||
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
|
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
|
||||||
@@ -33,24 +35,45 @@ final class GeocodeTest extends KernelTestCase
|
|||||||
$this->assertInstanceOf(Geocoder::class, $this->geocoder);
|
$this->assertInstanceOf(Geocoder::class, $this->geocoder);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testGeocodePostcodeReturnsNullOnInvalidInput(): void
|
public function testGeocodePostcodeThrowsOnEmptyInput(): void
|
||||||
{
|
{
|
||||||
sleep(1);
|
sleep(1);
|
||||||
$this->assertNull($this->geocoder->geocodePostcode(''));
|
$this->expectException(ApiErrorException::class);
|
||||||
|
$this->expectExceptionMessageMatches("/Nothing to search for.$/");
|
||||||
|
$this->geocoder->geocodePostcode('');
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testGeocodePostcodeReturnsLatLonModel(): void
|
public function testGeocodePostcodeThrowsOnInvalidPostcode(): void
|
||||||
{
|
{
|
||||||
sleep(1);
|
sleep(1);
|
||||||
$result = $this->geocoder->geocodePostcode(self::POSTCODE);
|
$this->expectException(NoResultsFoundException::class);
|
||||||
$this->assertInstanceOf(LatLongModel::class, $result);
|
$this->expectExceptionMessageMatches("/No results found.$/");
|
||||||
|
$this->geocoder->geocodePostcode('ZZZZZZZZZZZZ');
|
||||||
}
|
}
|
||||||
|
|
||||||
private function getMappableEntity(): MappableInterface
|
public function testGeocodePostcodeReturnsGeoCoordinates(): GeoCoordinates
|
||||||
{
|
{
|
||||||
return new class implements MappableInterface
|
sleep(1);
|
||||||
|
$geoCoords = $this->geocoder->geocodePostcode(self::POSTCODE);
|
||||||
|
$this->assertNotNull($geoCoords);
|
||||||
|
|
||||||
|
return $geoCoords;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @depends testGeocodePostcodeReturnsGeoCoordinates
|
||||||
|
*/
|
||||||
|
public function testCoordinatesAreSet(GeoCoordinates $geoCoordinates): void
|
||||||
{
|
{
|
||||||
use MappableTrait;
|
$this->assertIsFloat($geoCoordinates->latitude);
|
||||||
|
$this->assertIsFloat($geoCoordinates->longitude);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function getGeocodableEntity(): GeocodeInterface
|
||||||
|
{
|
||||||
|
return new class implements GeocodeInterface
|
||||||
|
{
|
||||||
|
use GeocodeTrait;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,15 +4,15 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace Pcm\GeocodeBundle\Tests;
|
namespace Pcm\GeocodeBundle\Tests;
|
||||||
|
|
||||||
use Pcm\GeocodeBundle\Entity\Interface\MappableInterface;
|
use Pcm\GeocodeBundle\Interface\Entity\GeocodeInterface;
|
||||||
use Pcm\GeocodeBundle\Entity\Trait\MappableTrait;
|
use Pcm\GeocodeBundle\Trait\Entity\GeocodeTrait;
|
||||||
use PHPUnit\Framework\TestCase;
|
use PHPUnit\Framework\TestCase;
|
||||||
|
|
||||||
final class MappableTraitTest extends TestCase
|
final class GeocodeTraitTest extends TestCase
|
||||||
{
|
{
|
||||||
private const float COORD = 123.456;
|
private const float COORD = 123.456;
|
||||||
|
|
||||||
private MappableInterface $obj;
|
private GeocodeInterface $obj;
|
||||||
|
|
||||||
protected function setUp(): void
|
protected function setUp(): void
|
||||||
{
|
{
|
||||||
@@ -21,7 +21,7 @@ final class MappableTraitTest extends TestCase
|
|||||||
|
|
||||||
public function testSetLatitude(): void
|
public function testSetLatitude(): void
|
||||||
{
|
{
|
||||||
$this->assertInstanceOf(MappableInterface::class, $this->obj->setLatitude(self::COORD));
|
$this->assertInstanceOf(GeocodeInterface::class, $this->obj->setLatitude(self::COORD));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testGetLatitudeReturnsNull(): void
|
public function testGetLatitudeReturnsNull(): void
|
||||||
@@ -37,7 +37,7 @@ final class MappableTraitTest extends TestCase
|
|||||||
|
|
||||||
public function testSetLongitude(): void
|
public function testSetLongitude(): void
|
||||||
{
|
{
|
||||||
$this->assertInstanceOf(MappableInterface::class, $this->obj->setLongitude(self::COORD));
|
$this->assertInstanceOf(GeocodeInterface::class, $this->obj->setLongitude(self::COORD));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testGetLongitudeReturnsNull(): void
|
public function testGetLongitudeReturnsNull(): void
|
||||||
@@ -82,11 +82,11 @@ final class MappableTraitTest extends TestCase
|
|||||||
$this->assertTrue($this->obj->isGeocoded());
|
$this->assertTrue($this->obj->isGeocoded());
|
||||||
}
|
}
|
||||||
|
|
||||||
private function getTraitObject(): MappableInterface
|
private function getTraitObject(): GeocodeInterface
|
||||||
{
|
{
|
||||||
return new class implements MappableInterface
|
return new class implements GeocodeInterface
|
||||||
{
|
{
|
||||||
use MappableTrait;
|
use GeocodeTrait;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user