Compare commits

..

4 Commits

Author SHA1 Message Date
brabli
214fcc2bac Add changelog 2024-08-05 14:58:58 +01:00
brabli
070279e8d4 Update readme 2024-08-05 14:55:00 +01:00
brabli
10d1d3c318 Update docblocks 2024-08-05 14:50:26 +01:00
brabli
8b662ebfa3 Rename Mappable stuff to Geocode stuff, change dir structure 2024-08-05 14:25:03 +01:00
7 changed files with 48 additions and 30 deletions

5
CHANGELOG.md Normal file
View File

@@ -0,0 +1,5 @@
# Changelog
## [1.0.0] 2024-08-05
- First major version release

View File

@@ -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.

View File

@@ -2,9 +2,12 @@
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;
@@ -14,5 +17,9 @@ interface MappableInterface
public function setLongitude(float $lon): self;
/**
* @return bool True if both latitude and longitude is set
*/
public function isGeocoded(): bool;
}

View File

@@ -10,6 +10,9 @@ 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,12 +20,16 @@ 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 a 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();
$response = $this->makeApiRequest($client, $postcode);

View File

@@ -2,18 +2,16 @@
declare(strict_types=1);
namespace Pcm\GeocodeBundle\Entity\Trait;
namespace Pcm\GeocodeBundle\Trait\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Allows an entity to be mapped via latitude and longitude coordinates
*
* Use MappableInterface
* Implementation of {@see Pcm\GeocodeBundle\Interface\Entity\GeocodeInterface}
*
* @package Pcm\GeocodeBundle
*/
trait MappableTrait
trait GeocodeTrait
{
#[ORM\Column(type: 'decimal', precision: 10, scale: 6, nullable: true)]
private ?float $latitude = null;
@@ -45,13 +43,9 @@ trait MappableTrait
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();
}
}

View File

@@ -4,8 +4,8 @@ declare(strict_types=1);
namespace Pcm\GeocodeBundle\Tests;
use Pcm\GeocodeBundle\Entity\Interface\MappableInterface;
use Pcm\GeocodeBundle\Entity\Trait\MappableTrait;
use Pcm\GeocodeBundle\Interface\Entity\GeocodeInterface;
use Pcm\GeocodeBundle\Trait\Entity\GeocodeTrait;
use Pcm\GeocodeBundle\Exception\ApiErrorException;
use Pcm\GeocodeBundle\Exception\NoResultsFoundException;
use Pcm\GeocodeBundle\Model\GeoCoordinates;
@@ -69,11 +69,11 @@ final class GeocodeTest extends KernelTestCase
$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;
};
}
}

View File

@@ -4,15 +4,15 @@ declare(strict_types=1);
namespace Pcm\GeocodeBundle\Tests;
use Pcm\GeocodeBundle\Entity\Interface\MappableInterface;
use Pcm\GeocodeBundle\Entity\Trait\MappableTrait;
use Pcm\GeocodeBundle\Interface\Entity\GeocodeInterface;
use Pcm\GeocodeBundle\Trait\Entity\GeocodeTrait;
use PHPUnit\Framework\TestCase;
final class MappableTraitTest extends TestCase
final class GeocodeTraitTest extends TestCase
{
private const float COORD = 123.456;
private MappableInterface $obj;
private GeocodeInterface $obj;
protected function setUp(): void
{
@@ -21,7 +21,7 @@ final class MappableTraitTest extends TestCase
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
@@ -37,7 +37,7 @@ final class MappableTraitTest extends TestCase
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
@@ -82,11 +82,11 @@ final class MappableTraitTest extends TestCase
$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;
};
}
}