Compare commits

...

9 Commits

Author SHA1 Message Date
0f6a3fed2b - Updating composer to allow version 2 or 3
- Update postcode in tests as the old one stopped working
2025-05-08 17:19:40 +01:00
brabli
8f52f72679 Update postcode in tests as the old one stopped working for some reason 2025-03-12 14:29:52 +00:00
brabli
555eaf4f2d Update changelog 2024-10-09 16:04:55 +01:00
brabli
d6f54eb17f Add tests and make them pass 2024-10-09 16:03:18 +01:00
e302da0fa6 Update to 2.0.0 2024-09-26 10:43:51 +01:00
9246f74117 Update changelog and readme 2024-09-26 10:41:56 +01:00
412c4eb579 Tweak how errors are thrown 2024-09-26 10:35:22 +01:00
9588938356 Add new exception 2024-09-26 10:35:04 +01:00
7cd78307f9 Remove custom messages 2024-09-26 10:34:55 +01:00
10 changed files with 64 additions and 24 deletions

View File

@@ -1,6 +1,17 @@
# Changelog
## [x.x.x] xxxx-xx-xx
## [2.0.2] 2025-05-08
- Updating composer to allow `doctrine/orm` version 2 or 3
- Update postcode in tests as the old one stopped working
## [2.0.1] 2024-10-09
- Fix bug that caused isGeocoded to return false if only one of the coordinates was zero
## [2.0.0] 2024-09-26
- Adjust how error messages are formatted
- Add getGeocodeData method to GeocodeInterface to allow for more abstraction in services
- Coordinates set to 0, 0 will cause isGeocoded to return false
## [1.0.0] 2024-08-05

View File

@@ -2,5 +2,11 @@
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.
Also included is a `Geocoder` service which accepts an instance of GeocodeInterface and attempts to return a `GeoCoordinates` object which contains the object's latitude and longitude.
# Usage
Use the `GeocodeTrait` an an entity to give it latitude and longitude fields.
If you wish to geocode an entity you must implement the `GeocodeInterface` interface, the trait will get you most of the way there but you must manually add a `getGeocodeData()` method. The simplest implementation is to return a postcode, but you can return a string of any data if it can be used to find a location.

View File

@@ -19,7 +19,7 @@
"symfony/dependency-injection": "^7.1",
"symfony/framework-bundle": "^7.1",
"symfony/yaml": "^7.1",
"doctrine/orm": "^2.13"
"doctrine/orm": "^2|^3"
},
"require-dev": {

View File

@@ -6,11 +6,5 @@ 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);
}
}

View File

@@ -0,0 +1,10 @@
<?php
declare(strict_types=1);
namespace Pcm\GeocodeBundle\Exception;
class MissingGeocodeDataException extends GeocodeException
{
}

View File

@@ -6,11 +6,5 @@ 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);
}
}

View File

@@ -5,6 +5,7 @@ declare(strict_types=1);
namespace Pcm\GeocodeBundle\Service;
use Pcm\GeocodeBundle\Exception\ApiErrorException;
use Pcm\GeocodeBundle\Exception\MissingGeocodeDataException;
use Pcm\GeocodeBundle\Exception\NoResultsFoundException;
use Pcm\GeocodeBundle\Interface\Entity\GeocodeInterface;
use Pcm\GeocodeBundle\Model\GeoCoordinates;
@@ -32,17 +33,23 @@ final class Geocoder
*/
public function geocode(GeocodeInterface $entity): GeoCoordinates
{
$geocodeData = $entity->getGeocodeData();
$geocodeData = trim($entity->getGeocodeData());
$client = $this->createClient();
if ('' === $geocodeData) {
throw new MissingGeocodeDataException('No geocode data present.');
}
$response = $this->makeApiRequest($client, $geocodeData);
$data = $this->getDataFromResponse($response);
if (array_key_exists('error', $data)) {
throw new ApiErrorException($geocodeData, $data['error']['message']);
throw new ApiErrorException($data['error']['message']);
}
if (empty($data)) {
throw new NoResultsFoundException($geocodeData, "No results found.");
$message = sprintf('No results found with geocode data "%s".', $geocodeData);
throw new NoResultsFoundException($message);
}
return $this->createGeoCoordinates($data);

View File

@@ -45,7 +45,11 @@ trait GeocodeTrait
public function isGeocoded(): bool
{
return null !== $this->getLatitude() && null !== $this->getLongitude() && (0.0 !== $this->getLatitude() && 0.0 !== $this->getLongitude());
$latIsntNull = null !== $this->getLatitude();
$longIsntNull = null !== $this->getLongitude();
$bothArentZero = !(0.0 === $this->getLatitude() && 0.0 === $this->getLongitude());
return $latIsntNull && $longIsntNull && $bothArentZero;
}
}

View File

@@ -4,9 +4,9 @@ declare(strict_types=1);
namespace Pcm\GeocodeBundle\Tests;
use Pcm\GeocodeBundle\Exception\MissingGeocodeDataException;
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;
use Pcm\GeocodeBundle\Service\Geocoder;
@@ -18,8 +18,8 @@ use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
*/
final class GeocoderTest extends KernelTestCase
{
// Buckingham Palace
private const string POSTCODE = 'SW1A 1AA';
// Yes this is a real postcode
private const string POSTCODE = 'ABC 123';
private Geocoder $geocoder;
@@ -33,9 +33,9 @@ final class GeocoderTest extends KernelTestCase
public function testGeocodeThrowsOnEmptyInput(): void
{
sleep(1);
$this->expectException(ApiErrorException::class);
$this->expectException(MissingGeocodeDataException::class);
$entity = $this->createEntity('');
$this->expectExceptionMessageMatches("/Nothing to search for.$/");
$this->expectExceptionMessageMatches("/No geocode data present./");
$this->geocoder->geocode($entity);
}
@@ -43,7 +43,7 @@ final class GeocoderTest extends KernelTestCase
{
sleep(1);
$this->expectException(NoResultsFoundException::class);
$this->expectExceptionMessageMatches("/No results found.$/");
$this->expectExceptionMessageMatches("/No results found with geocode data \"Invalid Postcode\"./");
$entity = $this->createEntity('Invalid Postcode');
$this->geocoder->geocode($entity);
}

View File

@@ -89,6 +89,20 @@ final class GeocodeTraitTest extends TestCase
$this->assertFalse($this->obj->isGeocoded());
}
public function testIsGeocodedReturnsTrueIfLongitudeIsZeroAndLatIsNot(): void
{
$this->obj->setLatitude(0.1);
$this->obj->setLongitude(0);
$this->assertTrue($this->obj->isGeocoded());
}
public function testIsGeocodedReturnsTrueIfLatitudeIsZeroAndLongIsNot(): void
{
$this->obj->setLatitude(0);
$this->obj->setLongitude(0.2);
$this->assertTrue($this->obj->isGeocoded());
}
private function getTraitObject(): GeocodeInterface
{
return new class implements GeocodeInterface