Compare commits
6 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
af13393765 | ||
|
|
7e0c231d88 | ||
|
|
57796a6fdb | ||
|
|
31d29f2460 | ||
|
|
6edbed9dfe | ||
|
|
3c23d5e88d |
@@ -18,7 +18,8 @@
|
|||||||
"symfony/http-client": "^6.1",
|
"symfony/http-client": "^6.1",
|
||||||
"symfony/dependency-injection": "^6.1",
|
"symfony/dependency-injection": "^6.1",
|
||||||
"symfony/framework-bundle": "^6.1",
|
"symfony/framework-bundle": "^6.1",
|
||||||
"symfony/yaml": "^6.1"
|
"symfony/yaml": "^6.1",
|
||||||
|
"doctrine/orm": "^2.13"
|
||||||
},
|
},
|
||||||
|
|
||||||
"require-dev": {
|
"require-dev": {
|
||||||
|
|||||||
@@ -4,6 +4,8 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace Pcm\GeocodeBundle\Entity\Trait;
|
namespace Pcm\GeocodeBundle\Entity\Trait;
|
||||||
|
|
||||||
|
use Doctrine\ORM\Mapping as ORM;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Allows an entity to be mapped via latitude and longitude coordinates
|
* Allows an entity to be mapped via latitude and longitude coordinates
|
||||||
*
|
*
|
||||||
@@ -13,8 +15,10 @@ namespace Pcm\GeocodeBundle\Entity\Trait;
|
|||||||
*/
|
*/
|
||||||
trait MappableTrait
|
trait MappableTrait
|
||||||
{
|
{
|
||||||
|
#[ORM\Column(type: 'decimal', precision: 10, scale: 6, nullable: true)]
|
||||||
private ?float $latitude = null;
|
private ?float $latitude = null;
|
||||||
|
|
||||||
|
#[ORM\Column(type: 'decimal', precision: 10, scale: 6, nullable: true)]
|
||||||
private ?float $longitude = null;
|
private ?float $longitude = null;
|
||||||
|
|
||||||
public function getLatitude(): ?float
|
public function getLatitude(): ?float
|
||||||
|
|||||||
@@ -15,11 +15,12 @@ class LatLongModel
|
|||||||
|
|
||||||
public function getLatitude(): float
|
public function getLatitude(): float
|
||||||
{
|
{
|
||||||
return $this->getLatitude();
|
return $this->latitude;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getLongitude(): float
|
public function getLongitude(): float
|
||||||
{
|
{
|
||||||
return $this->getLongitude();
|
return $this->longitude;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -15,17 +15,24 @@ class Geocoder
|
|||||||
public function __construct(private HttpClientInterface $client) {}
|
public function __construct(private HttpClientInterface $client) {}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Convert a postcode into latitude and longitude
|
* Convert a postcode into latitude and longitude. Returns null if conversion failed.
|
||||||
*
|
*
|
||||||
* @param string $postcode
|
* @param string $postcode
|
||||||
* @return LatLongModel
|
* @return null|LatLongModel
|
||||||
*/
|
*/
|
||||||
public function geocodePostcode(string $postcode): LatLongModel
|
public function geocodePostcode(string $postcode): ?LatLongModel
|
||||||
{
|
{
|
||||||
$client = $this->createClient();
|
$client = $this->createClient();
|
||||||
$response = $this->makeApiRequest($client, $postcode);
|
$response = $this->makeApiRequest($client, $postcode);
|
||||||
$data = $this->getDataFromResponse($response);
|
$data = $this->getDataFromResponse($response);
|
||||||
$this->throwIfDataIsEmpty($data);
|
|
||||||
|
if (empty($data)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (array_key_exists('error', $data)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
return $this->createLatLongModel($data);
|
return $this->createLatLongModel($data);
|
||||||
}
|
}
|
||||||
@@ -56,12 +63,6 @@ class Geocoder
|
|||||||
return $response->toArray(false);
|
return $response->toArray(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
private function throwIfDataIsEmpty(array $data): void
|
|
||||||
{
|
|
||||||
if (empty($data))
|
|
||||||
throw new \Exception("No data was received from API response! Were the arguments valid?");
|
|
||||||
}
|
|
||||||
|
|
||||||
private function createLatLongModel(array $data): LatLongModel
|
private function createLatLongModel(array $data): LatLongModel
|
||||||
{
|
{
|
||||||
$lat = $this->getLatitudeFromData($data);
|
$lat = $this->getLatitudeFromData($data);
|
||||||
|
|||||||
@@ -33,11 +33,10 @@ class GeocodeTest extends KernelTestCase
|
|||||||
$this->assertInstanceOf(Geocoder::class, $this->geocoder);
|
$this->assertInstanceOf(Geocoder::class, $this->geocoder);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testGeocodePostcodeThrowsOnInvalidInput(): void
|
public function testGeocodePostcodeReturnsNullOnInvalidInput(): void
|
||||||
{
|
{
|
||||||
sleep(1);
|
sleep(1);
|
||||||
$this->expectException(\Exception::class);
|
$this->assertNull($this->geocoder->geocodePostcode(''));
|
||||||
$this->geocoder->geocodePostcode('aaaaaaaa');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testGeocodePostcodeReturnsLatLonModel(): void
|
public function testGeocodePostcodeReturnsLatLonModel(): void
|
||||||
|
|||||||
Reference in New Issue
Block a user