|
|
@@ -0,0 +1,37 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+namespace App;
|
|
|
+
|
|
|
+use RuntimeException;
|
|
|
+
|
|
|
+class Geolocation {
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @param string $address
|
|
|
+ * @param string $region
|
|
|
+ * @return array
|
|
|
+ * @throws RuntimeException
|
|
|
+ */
|
|
|
+ public static function geocode(string $address, string $region = "gb"): array {
|
|
|
+ $query = [
|
|
|
+ "address" => $address,
|
|
|
+ "key" => $_ENV["GOOGLE_SERVER_API_KEY"],
|
|
|
+ "region" => $region,
|
|
|
+ ];
|
|
|
+
|
|
|
+ $url = "https://maps.googleapis.com/maps/api/geocode/json?" . http_build_query($query);
|
|
|
+
|
|
|
+ $string = @file_get_contents($url);
|
|
|
+
|
|
|
+ $object = json_decode($string);
|
|
|
+ if(!$object) {
|
|
|
+ throw new RuntimeException("Failed to validate address.");
|
|
|
+ }
|
|
|
+ if($object->status !== "OK") {
|
|
|
+ throw new RuntimeException("Failed to validate address.");
|
|
|
+ }
|
|
|
+
|
|
|
+ return $object->results;
|
|
|
+ }
|
|
|
+}
|