commit 346ca9b4493fa64430017d95e9302a93000c8bcf Author: Philipp Lang Date: Tue May 16 14:29:51 2023 +0200 Initial commit 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, + ]); + } +}