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.

CitiesComponent.php 4.1KB

3 yıl önce
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. $provider = $this->getGeocoderProvider() ;;
  72. $query = GeocodeQuery::create($query)->withData('type', 'housenumber');
  73. $results = $provider->geocodeQuery($query);
  74. $resultsToReturn = array();
  75. foreach($results as $result) {
  76. if ($result->getStreetNumber() && strlen($result->getStreetNumber()) > 0) {
  77. $resultsToReturn[] = $result;
  78. }
  79. }
  80. return $resultsToReturn;
  81. }
  82. public function callReverseAddressApi($latitude, $longitude)
  83. {
  84. $provider = $this->getGeocoderProvider() ;;
  85. $query = ReverseQuery::create(new Coordinates($latitude, $longitude));
  86. $results = $provider->reverseQuery($query);
  87. return $results->all() ;
  88. }
  89. public function getZipByCity($city, $code = null)
  90. {
  91. $zip = null;
  92. $paramsSearch = [
  93. 'nom' => $city,
  94. 'fields' => 'nom,codesPostaux'
  95. ];
  96. if ($code != null && $code != 0) {
  97. $paramsSearch['code'] = $code;
  98. }
  99. $returnCitiesSearchZip = json_decode($this->callCitiesApi('get', 'communes', $paramsSearch));
  100. if ($returnCitiesSearchZip) {
  101. foreach ($returnCitiesSearchZip as $citySearchZip) {
  102. if (strtolower(trim($city)) == strtolower(trim($citySearchZip->nom))) {
  103. $zip = $citySearchZip->codesPostaux[0];
  104. }
  105. }
  106. }
  107. return $zip;
  108. }
  109. }