From 346ca9b4493fa64430017d95e9302a93000c8bcf Mon Sep 17 00:00:00 2001 From: Philipp Lang Date: Tue, 16 May 2023 14:29:51 +0200 Subject: [PATCH] Initial commit --- .gitignore | 1 + composer.json | 17 ++++++++++++++ src/Address.php | 41 ++++++++++++++++++++++++++++++++ src/Coordinate.php | 10 ++++++++ src/FillCoordsJob.php | 53 ++++++++++++++++++++++++++++++++++++++++++ src/Geolocatable.php | 12 ++++++++++ src/HasGeolocation.php | 36 ++++++++++++++++++++++++++++ 7 files changed, 170 insertions(+) create mode 100644 .gitignore create mode 100644 composer.json create mode 100644 src/Address.php create mode 100644 src/Coordinate.php create mode 100644 src/FillCoordsJob.php create mode 100644 src/Geolocatable.php create mode 100644 src/HasGeolocation.php diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..61ead86 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/vendor diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..45ea3f3 --- /dev/null +++ b/composer.json @@ -0,0 +1,17 @@ +{ + "name": "zoomyboy/osm", + "description": "OSM Helper", + "type": "library", + "autoload": { + "psr-4": { + "Zoomyboy\\Osm\\": "src/" + } + }, + "authors": [ + { + "name": "Philipp Lang", + "email": "philipp@zoomyboy.de" + } + ], + "require": {} +} diff --git a/src/Address.php b/src/Address.php new file mode 100644 index 0000000..0e40f67 --- /dev/null +++ b/src/Address.php @@ -0,0 +1,41 @@ +address || !$this->zip || !$this->location) { + return null; + } + + $response = Http::get('https://nominatim.openstreetmap.org/search/'.rawurlencode($this->queryString()).'?'.http_build_query([ + 'format' => 'json', + 'addressdetails' => 1, + ])); + + if (!$response->ok()) { + return null; + } + + $lat = (float) data_get($response, '0.lat'); + $lon = (float) data_get($response, '0.lon'); + + return new Coordinate($lat, $lon); + } + + public function queryString(): string + { + return $this->address.', '.$this->zip.' '.$this->location; + } +} diff --git a/src/Coordinate.php b/src/Coordinate.php new file mode 100644 index 0000000..788c1cc --- /dev/null +++ b/src/Coordinate.php @@ -0,0 +1,10 @@ +model = $model; + } + + /** + * Execute the job. + * + * @return void + */ + public function handle() + { + $address = $this->model->getAddressForGeolocation(); + + if (!$address) { + $this->model->destroyCoordinate(); + + return; + } + + $coordinate = $address->getCoordinate(); + + if (!$coordinate) { + return; + } + + $this->model->fillCoordinate($coordinate); + } +} diff --git a/src/Geolocatable.php b/src/Geolocatable.php new file mode 100644 index 0000000..92e6412 --- /dev/null +++ b/src/Geolocatable.php @@ -0,0 +1,12 @@ +updateQuietly([ + 'lat' => null, + 'lon' => null, + ]); + } +}