From e924be87df4a049f78aa420a5cbf0b04f27aee1d Mon Sep 17 00:00:00 2001 From: philipp lang Date: Tue, 9 Jul 2024 11:20:40 +0200 Subject: [PATCH] Add point data class --- src/Address.php | 12 +++++------- src/Geolocatable.php | 2 +- src/{Coordinate.php => Point.php} | 4 +++- 3 files changed, 9 insertions(+), 9 deletions(-) rename src/{Coordinate.php => Point.php} (68%) diff --git a/src/Address.php b/src/Address.php index 0e40f67..80141c4 100644 --- a/src/Address.php +++ b/src/Address.php @@ -13,13 +13,14 @@ class Address ) { } - public function getCoordinate(): ?Coordinate + public function getCoordinate(): ?Point { if (!$this->address || !$this->zip || !$this->location) { return null; } - $response = Http::get('https://nominatim.openstreetmap.org/search/'.rawurlencode($this->queryString()).'?'.http_build_query([ + $response = Http::get('https://nominatim.openstreetmap.org/search?' . http_build_query([ + 'q' => $this->queryString(), 'format' => 'json', 'addressdetails' => 1, ])); @@ -28,14 +29,11 @@ class Address return null; } - $lat = (float) data_get($response, '0.lat'); - $lon = (float) data_get($response, '0.lon'); - - return new Coordinate($lat, $lon); + return count($response->json()) ? Point::from(['lat' => (float) data_get($response, '0.lat'), 'lon' => (float) data_get($response, '0.lon')]) : null; } public function queryString(): string { - return $this->address.', '.$this->zip.' '.$this->location; + return $this->address . ', ' . $this->zip . ' ' . $this->location; } } diff --git a/src/Geolocatable.php b/src/Geolocatable.php index be57ebf..786c530 100644 --- a/src/Geolocatable.php +++ b/src/Geolocatable.php @@ -4,7 +4,7 @@ namespace Zoomyboy\Osm; interface Geolocatable { - public function fillCoordinate(Coordinate $coordinate): void; + public function fillCoordinate(Point $point): void; public function getAddressForGeolocation(): ?Address; diff --git a/src/Coordinate.php b/src/Point.php similarity index 68% rename from src/Coordinate.php rename to src/Point.php index 788c1cc..271a77e 100644 --- a/src/Coordinate.php +++ b/src/Point.php @@ -2,7 +2,9 @@ namespace Zoomyboy\Osm; -class Coordinate +use Spatie\LaravelData\Data; + +class Point extends Data { public function __construct(public float $lat, public float $lon) {