|
|
@@ -0,0 +1,94 @@
|
|
|
+<?php
|
|
|
+declare(strict_types=1);
|
|
|
+
|
|
|
+namespace App\Service\Utils;
|
|
|
+
|
|
|
+use App\ApiResources\Utils\GpsCoordinate;
|
|
|
+use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
|
|
+use Symfony\Contracts\HttpClient\HttpClientInterface;
|
|
|
+
|
|
|
+/**
|
|
|
+ * Class GpsCoordinateSearching : service aidant à la manipulation de l'API OpenStreetMap
|
|
|
+ * @package App\Service\Utils
|
|
|
+ */
|
|
|
+class GpsCoordinateUtils
|
|
|
+{
|
|
|
+ private HttpClientInterface $clientOpenStreetMap;
|
|
|
+
|
|
|
+ public function __construct(
|
|
|
+ HttpClientInterface $openstreetmap
|
|
|
+ )
|
|
|
+ {
|
|
|
+ $this->clientOpenStreetMap = $openstreetmap;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Renvoi un tableau d'adresse existante correspondant à la recherche d'adresse demandée
|
|
|
+ * @param string|null $street
|
|
|
+ * @param string|null $cp
|
|
|
+ * @param string|null $city
|
|
|
+ * @return array
|
|
|
+ * @throws \Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface
|
|
|
+ * @throws \Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface
|
|
|
+ * @throws \Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface
|
|
|
+ * @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface
|
|
|
+ */
|
|
|
+ public function searchGpsCoordinates(?string $street, ?string $cp, ?string $city): array {
|
|
|
+ $url = sprintf('search?addressdetails=1&format=json&limit=10&street=%s&postalcode=%s&city=%s', $street, $cp, $city);
|
|
|
+ $response = $this->clientOpenStreetMap->request('GET', $url)->getContent();
|
|
|
+ if(empty($response))
|
|
|
+ throw new NotFoundHttpException('no_cooresponding_gps_coordinate');
|
|
|
+
|
|
|
+ return json_decode($response, true);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Renvoi l'adresse correspondant à la latitude et longitude demandée
|
|
|
+ * @param $latitude
|
|
|
+ * @param $longitude
|
|
|
+ * @return array
|
|
|
+ * @throws \Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface
|
|
|
+ * @throws \Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface
|
|
|
+ * @throws \Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface
|
|
|
+ * @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface
|
|
|
+ */
|
|
|
+ public function reverseGpsCoordinates(float $latitude, float $longitude): array {
|
|
|
+ $url = sprintf('reverse?addressdetails=1&format=json&lat=%s&lon=%s', $latitude, $longitude);
|
|
|
+ $response = $this->clientOpenStreetMap->request('GET', $url)->getContent();
|
|
|
+ if(empty($response))
|
|
|
+ throw new NotFoundHttpException('no_reverse_gps_coordinate');
|
|
|
+
|
|
|
+ return json_decode($response, true);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Transforme une réponse d'API en ressource GpsCoordinate
|
|
|
+ * @param array $gpsApiResponse
|
|
|
+ * @return GpsCoordinate
|
|
|
+ */
|
|
|
+ public function createGpsCoordinate(array $gpsApiResponse):GpsCoordinate{
|
|
|
+ $address = $this->transformAddress($gpsApiResponse['address']);
|
|
|
+ $gpsCoordinate = new GpsCoordinate();
|
|
|
+ $gpsCoordinate
|
|
|
+ ->setLatitude(floatval($gpsApiResponse['lat']))
|
|
|
+ ->setLongitude(floatval($gpsApiResponse['lon']))
|
|
|
+ ->setCity($address['city'])
|
|
|
+ ->setCp($address['cp'])
|
|
|
+ ->setStreetAddress($address['streetAddress'])
|
|
|
+ ->setStreetAddressSecond($address['streetAddressSecond']);
|
|
|
+ return $gpsCoordinate;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Permet de faire correspondre le bloc adresse renvoyé par l'API avec des éléments plus communs
|
|
|
+ * @param array $address
|
|
|
+ * @return array
|
|
|
+ */
|
|
|
+ public function transformAddress(array $address): array{
|
|
|
+ $addressTransformed['streetAddress'] = key_exists('road', $address) ? $address['road'] : (key_exists('hamlet', $address) ? $address['hamlet'] : null);
|
|
|
+ $addressTransformed['streetAddressSecond'] = key_exists('road', $address) && key_exists('hamlet', $address) ? $address['hamlet'] : null;
|
|
|
+ $addressTransformed['city'] = key_exists('town', $address) ? $address['town'] : (key_exists('village', $address) ? $address['village'] : null);
|
|
|
+ $addressTransformed['cp'] = key_exists('postcode', $address) ? $address['postcode'] : null;
|
|
|
+ return $addressTransformed;
|
|
|
+ }
|
|
|
+}
|