You are here

public function DumperPluginManager::setCountryFromGeojson in Geocoder 8.3

Same name and namespace in other branches
  1. 8.2 src/DumperPluginManager.php \Drupal\geocoder\DumperPluginManager::setCountryFromGeojson()

Define a Country value from a Geojson string.

Parameters

string $geojson: The GeoJson place string.

Return value

string A country code.

1 call to DumperPluginManager::setCountryFromGeojson()
DumperPluginManager::setAddressFieldFromGeojson in src/DumperPluginManager.php
Define an Address field value from a Geojson string.

File

src/DumperPluginManager.php, line 112

Class

DumperPluginManager
Provides a plugin manager for geocoder dumpers.

Namespace

Drupal\geocoder

Code

public function setCountryFromGeojson($geojson) : string {
  $geojson_array = Json::decode($geojson);
  $country_code = isset($geojson_array['properties']['countryCode']) ? strtoupper(substr($geojson_array['properties']['countryCode'], 0, 2)) : '';

  // Some provider (like MapQuest) might not return the 2 digits countryCode
  // but just the country name or a 3 digits code,
  // so try to convert it into countryCode,
  // as it seems to be mandatory in Address Field Entity API.
  if (!array_key_exists($country_code, $this->countryManager
    ->getList()) && isset($geojson_array['properties']['country'])) {
    $country_code = strtoupper(substr($geojson_array['properties']['country'], 0, 2));
  }

  // Allow others modules to adjust the country_code at the end.
  $this->moduleHandler
    ->alter('geocode_country_code', $country_code, $geojson_array);
  return $country_code;
}