Compare commits

..

5 Commits

Author SHA1 Message Date
40fc3f59f8 Update changelog 2024-09-26 09:42:45 +01:00
d8ecb41ac3 Add test to return false on is geocoded if coords are 0 0 2024-09-26 09:41:54 +01:00
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
6 changed files with 41 additions and 12 deletions

8
CHANGELOG.md Normal file
View 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

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

@@ -4,6 +4,9 @@ declare(strict_types=1);
namespace Pcm\GeocodeBundle\Interface\Entity;
/**
* Defines latitude and longitude getters and setters
*/
interface GeocodeInterface
{
public function getLatitude(): ?float;
@@ -14,5 +17,9 @@ interface GeocodeInterface
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

@@ -43,14 +43,9 @@ trait GeocodeTrait
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();
return null !== $this->getLatitude() && null !== $this->getLongitude() && (0.0 !== $this->getLatitude() && 0.0 !== $this->getLongitude());
}
}

View File

@@ -75,11 +75,18 @@ final class GeocodeTraitTest extends TestCase
$this->assertTrue($this->obj->isGeocoded());
}
public function testIsGeocodeReturnsTrueIfLatAndLonAreBothZero(): void
public function testIsGeocodedReturnsFalseIfLatAndLonAreBothZero(): void
{
$this->obj->setLatitude(0.000);
$this->obj->setLongitude(0.000);
$this->assertTrue($this->obj->isGeocoded());
$this->assertFalse($this->obj->isGeocoded());
}
public function testIsGeocodedReturnsFalseIfLatAndLonAreBothZeroInts(): void
{
$this->obj->setLatitude(0);
$this->obj->setLongitude(0);
$this->assertFalse($this->obj->isGeocoded());
}
private function getTraitObject(): GeocodeInterface