You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

139 lines
4.1KB

  1. <?php
  2. namespace Lc\SovBundle\Component;
  3. use Geocoder\Model\Coordinates;
  4. use Geocoder\Provider\Addok\Addok;
  5. use Geocoder\Provider\GoogleMaps\GoogleMaps;
  6. use Geocoder\Provider\Nominatim\Nominatim;
  7. use Geocoder\Query\GeocodeQuery;
  8. use Geocoder\Query\ReverseQuery;
  9. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  10. use Symfony\Component\HttpClient\HttplugClient;
  11. class CitiesComponent
  12. {
  13. protected ParameterBagInterface $parameterBag;
  14. public function __construct(ParameterBagInterface $parameterBag)
  15. {
  16. $this->parameterBag = $parameterBag;
  17. }
  18. public function callCitiesApi($method, $url, $data = false)
  19. {
  20. $url = 'https://geo.api.gouv.fr/' . $url;
  21. $curl = curl_init();
  22. switch ($method) {
  23. case "POST":
  24. curl_setopt($curl, CURLOPT_POST, 1);
  25. if ($data) {
  26. curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
  27. }
  28. break;
  29. case "PUT":
  30. curl_setopt($curl, CURLOPT_PUT, 1);
  31. break;
  32. default:
  33. if ($data) {
  34. $url = sprintf("%s?%s", $url, http_build_query($data));
  35. }
  36. }
  37. // Optional Authentication:
  38. curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
  39. curl_setopt($curl, CURLOPT_USERPWD, "username:password");
  40. curl_setopt($curl, CURLOPT_URL, $url);
  41. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  42. $result = curl_exec($curl);
  43. curl_close($curl);
  44. return $result;
  45. }
  46. public function getGeocoderProvider()
  47. {
  48. $provider = false;
  49. $symfonyClient = new HttplugClient();
  50. $configGeocoderProvider = $this->parameterBag->get('geocoder.provider');
  51. /* API du gouvernement */
  52. if ($configGeocoderProvider == 'addok') {
  53. $provider = new Addok($symfonyClient, 'https://api-adresse.data.gouv.fr');
  54. } /* Google Maps */
  55. elseif ($configGeocoderProvider == 'googlemaps') {
  56. $provider = new GoogleMaps($symfonyClient, null, $this->parameterBag->get('geocoder.api_key'));
  57. } /* Nominatim : OpenStreetMap */
  58. elseif ($configGeocoderProvider == 'nominatim') {
  59. $provider = Nominatim::withOpenStreetMapServer(
  60. $symfonyClient,
  61. 'Mozilla/5.0 (platform; rv:geckoversion) Gecko/geckotrail Firefox/firefoxversion'
  62. );
  63. }
  64. if (!$provider) {
  65. throw new \ErrorException('Aucun provider (geocoding) défini');
  66. }
  67. return $provider;
  68. }
  69. public function callAddressApi($query)
  70. {
  71. $resultsToReturn = [];
  72. if(!is_null($query)) {
  73. $provider = $this->getGeocoderProvider() ;
  74. $query = GeocodeQuery::create($query)->withData('type', 'housenumber');
  75. $results = $provider->geocodeQuery($query);
  76. foreach($results as $result) {
  77. if ($result->getStreetNumber() && strlen($result->getStreetNumber()) > 0) {
  78. $resultsToReturn[] = $result;
  79. }
  80. }
  81. }
  82. return $resultsToReturn;
  83. }
  84. public function callReverseAddressApi($latitude, $longitude)
  85. {
  86. $provider = $this->getGeocoderProvider() ;;
  87. $query = ReverseQuery::create(new Coordinates($latitude, $longitude));
  88. $results = $provider->reverseQuery($query);
  89. return $results->all() ;
  90. }
  91. public function getZipByCity($city, $code = null)
  92. {
  93. $zip = null;
  94. $paramsSearch = [
  95. 'nom' => $city,
  96. 'fields' => 'nom,codesPostaux'
  97. ];
  98. if ($code != null && $code != 0) {
  99. $paramsSearch['code'] = $code;
  100. }
  101. $returnCitiesSearchZip = json_decode($this->callCitiesApi('get', 'communes', $paramsSearch));
  102. if ($returnCitiesSearchZip) {
  103. foreach ($returnCitiesSearchZip as $citySearchZip) {
  104. if (strtolower(trim($city)) == strtolower(trim($citySearchZip->nom))) {
  105. $zip = $citySearchZip->codesPostaux[0];
  106. }
  107. }
  108. }
  109. return $zip;
  110. }
  111. }