Compare commits

...

12 Commits

Author SHA1 Message Date
brabli
cb16158eb3 Add text to license file 2024-08-05 12:05:02 +01:00
brabli
58d0e93fc9 Add type to const, change const name 2024-08-05 12:01:40 +01:00
brabli
5c87bf35d5 Add types to consts 2024-08-05 12:01:33 +01:00
brabli
84ba22f287 Update php version to 8.3 2024-08-05 11:56:05 +01:00
brabli
eaa1c0a173 Update packages to symfony 7 2024-08-05 11:55:32 +01:00
brabli
af13393765 Add check to make test pass 2024-08-05 11:17:22 +01:00
brabli
7e0c231d88 Add newline 2024-08-05 11:17:09 +01:00
Brabli
57796a6fdb Return null instead of throwing 2022-10-17 15:46:19 +01:00
Brabli
31d29f2460 Fix broken latlong model 2022-10-17 15:38:10 +01:00
Brabli
6edbed9dfe Add attributes above lat long properties 2022-10-17 12:21:35 +01:00
Brabli
3c23d5e88d Require doctrine orm 2022-10-17 12:21:07 +01:00
Brabli
ee418a4844 Improvements 2022-09-26 11:50:04 +01:00
8 changed files with 78 additions and 45 deletions

View File

@@ -1,4 +1,4 @@
FROM php:8.1-alpine
FROM php:8.3-alpine
WORKDIR /code
COPY --from=composer:2 /usr/bin/composer /usr/bin/composer

View File

@@ -1 +1,7 @@
License
Copyright (c) 2024 PCM
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

View File

@@ -14,16 +14,17 @@
}
],
"require": {
"php": "^8.1.0",
"symfony/http-client": "^6.1",
"symfony/dependency-injection": "^6.1",
"symfony/framework-bundle": "^6.1",
"symfony/yaml": "^6.1"
"php": "^8.3.0",
"symfony/http-client": "^7.1",
"symfony/dependency-injection": "^7.1",
"symfony/framework-bundle": "^7.1",
"symfony/yaml": "^7.1",
"doctrine/orm": "^2.13"
},
"require-dev": {
"phpunit/phpunit": "^9.5",
"symfony/phpunit-bridge": "^6.1"
"symfony/phpunit-bridge": "^7.1"
},
"autoload": {

View File

@@ -4,6 +4,8 @@ declare(strict_types=1);
namespace Pcm\GeocodeBundle\Entity\Trait;
use Doctrine\ORM\Mapping as ORM;
/**
* Allows an entity to be mapped via latitude and longitude coordinates
*
@@ -13,8 +15,10 @@ namespace Pcm\GeocodeBundle\Entity\Trait;
*/
trait MappableTrait
{
#[ORM\Column(type: 'decimal', precision: 10, scale: 6, nullable: true)]
private ?float $latitude = null;
#[ORM\Column(type: 'decimal', precision: 10, scale: 6, nullable: true)]
private ?float $longitude = null;
public function getLatitude(): ?float

View File

@@ -9,17 +9,18 @@ namespace Pcm\GeocodeBundle\Model;
*
* @package Pcm\GeocodeBundle
*/
class LatLonModel
class LatLongModel
{
public function __construct(private float $latitude, private float $longitude) {}
public function getLatitude(): float
{
return $this->getLatitude();
return $this->latitude;
}
public function getLongitude(): float
{
return $this->getLongitude();
return $this->longitude;
}
}

View File

@@ -4,31 +4,37 @@ declare(strict_types=1);
namespace Pcm\GeocodeBundle\Service;
use Pcm\GeocodeBundle\Entity\GeocodeData;
use Pcm\GeocodeBundle\Model\LatLonModel;
use Pcm\GeocodeBundle\Model\LatLongModel;
use Symfony\Contracts\HttpClient\HttpClientInterface;
use Symfony\Contracts\HttpClient\ResponseInterface;
class Geocoder
{
private const API_URL = "https://nominatim.openstreetmap.org/search";
private const string API_URL = "https://nominatim.openstreetmap.org/search";
public function __construct(private HttpClientInterface $client) {}
/**
* Returns a LatLonModel object
* Convert a postcode into latitude and longitude. Returns null if conversion failed.
*
* @param string $postcode
* @return GeocodeData
* @return null|LatLongModel
*/
public function geocodePostcode(string $postcode): LatLonModel
public function geocodePostcode(string $postcode): ?LatLongModel
{
$client = $this->createClient();
$response = $this->requestGeocodeData($client, $postcode);
$data = $response->toArray();
$this->throwIfNoResponseData($data);
$response = $this->makeApiRequest($client, $postcode);
$data = $this->getDataFromResponse($response);
return $this->createLatLonObject($data);
if (empty($data)) {
return null;
}
if (array_key_exists('error', $data)) {
return null;
}
return $this->createLatLongModel($data);
}
private function createClient(): HttpClientInterface
@@ -38,7 +44,7 @@ class Geocoder
]);
}
private function requestGeocodeData(HttpClientInterface $client, string $postcode): ResponseInterface
private function makeApiRequest(HttpClientInterface $client, string $postcode): ResponseInterface
{
return $client->request(
method: 'GET',
@@ -52,14 +58,26 @@ class Geocoder
);
}
private function throwIfNoResponseData(array $data): void
private function getDataFromResponse(ResponseInterface $response): array
{
if (empty($data))
throw new \Exception("No data was received from API response! Were the arguments valid?");
return $response->toArray(false);
}
private function createLatLonObject(array $data): LatLonModel
private function createLatLongModel(array $data): LatLongModel
{
return new LatLonModel((float) $data[0]['lat'], (float) $data[0]['lon']);
$lat = $this->getLatitudeFromData($data);
$long = $this->getLongitudeFromData($data);
return new LatLongModel($lat, $long);
}
private function getLatitudeFromData(array $data): float
{
return (float) $data[0]['lat'];
}
private function getLongitudeFromData(array $data): float
{
return (float) $data[0]['lon'];
}
}

View File

@@ -6,15 +6,18 @@ namespace Pcm\GeocodeBundle\Tests;
use Pcm\GeocodeBundle\Entity\Interface\MappableInterface;
use Pcm\GeocodeBundle\Entity\Trait\MappableTrait;
use Pcm\GeocodeBundle\Model\LatLonModel;
use Pcm\GeocodeBundle\Model\LatLongModel;
use Pcm\GeocodeBundle\Service\Geocoder;
use Pcm\GeocodeBundle\Tests\AppKernel;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
/**
* We sleep 1 second after API calls to prevent breaking the API T&Cs.
*/
class GeocodeTest extends KernelTestCase
{
// Buckingham Palace
private const POSTCODE = 'SW1A 1AA';
private const string POSTCODE = 'SW1A 1AA';
private Geocoder $geocoder;
@@ -30,18 +33,17 @@ class GeocodeTest extends KernelTestCase
$this->assertInstanceOf(Geocoder::class, $this->geocoder);
}
public function testGeocodePostcodeThrowsOnInvalidInput(): void
public function testGeocodePostcodeReturnsNullOnInvalidInput(): void
{
sleep(1);
$this->expectException(\Exception::class);
$this->geocoder->geocodePostcode('aaaaaaaa');
$this->assertNull($this->geocoder->geocodePostcode(''));
}
public function testGeocodePostcodeReturnsLatLonModel(): void
{
sleep(1);
$result = $this->geocoder->geocodePostcode(self::POSTCODE);
$this->assertInstanceOf(LatLonModel::class, $result);
$this->assertInstanceOf(LatLongModel::class, $result);
}
private function getMappableEntity(): MappableInterface
@@ -52,3 +54,4 @@ class GeocodeTest extends KernelTestCase
};
}
}

View File

@@ -10,7 +10,7 @@ use PHPUnit\Framework\TestCase;
class MappableTraitTest extends TestCase
{
private const FLOAT = 123.456;
private const float COORD = 123.456;
private MappableInterface $obj;
@@ -21,7 +21,7 @@ class MappableTraitTest extends TestCase
public function testSetLatitude(): void
{
$this->assertInstanceOf(MappableInterface::class, $this->obj->setLatitude(self::FLOAT));
$this->assertInstanceOf(MappableInterface::class, $this->obj->setLatitude(self::COORD));
}
public function testGetLatitudeReturnsNull(): void
@@ -31,13 +31,13 @@ class MappableTraitTest extends TestCase
public function testGetLatitude(): void
{
$this->obj->setLatitude(self::FLOAT);
$this->assertSame(self::FLOAT, $this->obj->getLatitude());
$this->obj->setLatitude(self::COORD);
$this->assertSame(self::COORD, $this->obj->getLatitude());
}
public function testSetLongitude(): void
{
$this->assertInstanceOf(MappableInterface::class, $this->obj->setLongitude(self::FLOAT));
$this->assertInstanceOf(MappableInterface::class, $this->obj->setLongitude(self::COORD));
}
public function testGetLongitudeReturnsNull(): void
@@ -47,8 +47,8 @@ class MappableTraitTest extends TestCase
public function testGetLongitude(): void
{
$this->obj->setLongitude(self::FLOAT);
$this->assertSame(self::FLOAT, $this->obj->getLongitude());
$this->obj->setLongitude(self::COORD);
$this->assertSame(self::COORD, $this->obj->getLongitude());
}
public function testIsGeocodedReturnsFalse(): void
@@ -58,20 +58,20 @@ class MappableTraitTest extends TestCase
public function testIsGeocodedReturnsFalseIfLatIsSet(): void
{
$this->obj->setLatitude(self::FLOAT);
$this->obj->setLatitude(self::COORD);
$this->assertFalse($this->obj->isGeocoded());
}
public function testIsGeocodedReturnsFalseIfLonIsSet(): void
{
$this->obj->setLongitude(self::FLOAT);
$this->obj->setLongitude(self::COORD);
$this->assertFalse($this->obj->isGeocoded());
}
public function testIsGeocodedReturnsTrueIfLatAndLonAreSet(): void
{
$this->obj->setLatitude(self::FLOAT);
$this->obj->setLongitude(self::FLOAT);
$this->obj->setLatitude(self::COORD);
$this->obj->setLongitude(self::COORD);
$this->assertTrue($this->obj->isGeocoded());
}
@@ -89,5 +89,5 @@ class MappableTraitTest extends TestCase
use MappableTrait;
};
}
}