Compare commits
6 Commits
issue/8-th
...
issue/14-c
| Author | SHA1 | Date | |
|---|---|---|---|
| 40fc3f59f8 | |||
| d8ecb41ac3 | |||
|
|
214fcc2bac | ||
|
|
070279e8d4 | ||
|
|
10d1d3c318 | ||
|
|
8b662ebfa3 |
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.
|
||||||
|
|
||||||
|
|||||||
@@ -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;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -10,6 +10,9 @@ 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,12 +20,16 @@ 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|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 geocodePostcode(string $postcode): GeoCoordinates
|
||||||
{
|
{
|
||||||
$client = $this->createClient();
|
$client = $this->createClient();
|
||||||
$response = $this->makeApiRequest($client, $postcode);
|
$response = $this->makeApiRequest($client, $postcode);
|
||||||
|
|||||||
@@ -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() && (0.0 !== $this->getLatitude() && 0.0 !== $this->getLongitude());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -4,8 +4,8 @@ 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\Exception\ApiErrorException;
|
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;
|
||||||
@@ -69,11 +69,11 @@ final class GeocodeTest extends KernelTestCase
|
|||||||
$this->assertIsFloat($geoCoordinates->longitude);
|
$this->assertIsFloat($geoCoordinates->longitude);
|
||||||
}
|
}
|
||||||
|
|
||||||
private function getMappableEntity(): MappableInterface
|
private function getGeocodableEntity(): GeocodeInterface
|
||||||
{
|
{
|
||||||
return new class implements MappableInterface
|
return new class implements GeocodeInterface
|
||||||
{
|
{
|
||||||
use MappableTrait;
|
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
|
||||||
@@ -75,18 +75,25 @@ final class MappableTraitTest 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->assertTrue($this->obj->isGeocoded());
|
$this->assertFalse($this->obj->isGeocoded());
|
||||||
}
|
}
|
||||||
|
|
||||||
private function getTraitObject(): MappableInterface
|
public function testIsGeocodedReturnsFalseIfLatAndLonAreBothZeroInts(): void
|
||||||
{
|
{
|
||||||
return new class implements MappableInterface
|
$this->obj->setLatitude(0);
|
||||||
|
$this->obj->setLongitude(0);
|
||||||
|
$this->assertFalse($this->obj->isGeocoded());
|
||||||
|
}
|
||||||
|
|
||||||
|
private function getTraitObject(): GeocodeInterface
|
||||||
|
{
|
||||||
|
return new class implements GeocodeInterface
|
||||||
{
|
{
|
||||||
use MappableTrait;
|
use GeocodeTrait;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user