You are here

class Geocoder in Geocoder 8.2

Same name and namespace in other branches
  1. 8.3 src/Geocoder.php \Drupal\geocoder\Geocoder
  2. 7.2 src/Geocoder.php \Drupal\geocoder\Geocoder

Provides a geocoder factory class.

Hierarchy

Expanded class hierarchy of Geocoder

7 files declare their use of Geocoder
AddressGeocodeFormatter.php in modules/geocoder_address/src/Plugin/Field/FieldFormatter/AddressGeocodeFormatter.php
FileGeocodeFormatter.php in modules/geocoder_field/src/Plugin/Field/FieldFormatter/FileGeocodeFormatter.php
GeocodeFormatterBase.php in modules/geocoder_field/src/Plugin/Field/GeocodeFormatterBase.php
GeocodeOrigin.php in src/Plugin/GeofieldProximitySource/GeocodeOrigin.php
GeocodeOriginAutocomplete.php in src/Plugin/GeofieldProximitySource/deprecated/GeocodeOriginAutocomplete.php

... See full list

2 string references to 'Geocoder'
geocoder.info.yml in ./geocoder.info.yml
geocoder.info.yml
geocoder.services.yml in ./geocoder.services.yml
geocoder.services.yml
1 service uses Geocoder
geocoder in ./geocoder.services.yml
Drupal\geocoder\Geocoder

File

src/Geocoder.php, line 11

Namespace

Drupal\geocoder
View source
class Geocoder implements GeocoderInterface {

  /**
   * The config factory service.
   *
   * @var \Drupal\Core\Config\ConfigFactoryInterface
   */
  protected $config;

  /**
   * The geocoder provider plugin manager service.
   *
   * @var \Drupal\geocoder\ProviderPluginManager
   */
  protected $providerPluginManager;

  /**
   * Constructs a geocoder factory class.
   *
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   A config factory for retrieving required config objects.
   * @param \Drupal\geocoder\ProviderPluginManager $provider_plugin_manager
   *   The geocoder provider plugin manager service.
   */
  public function __construct(ConfigFactoryInterface $config_factory, ProviderPluginManager $provider_plugin_manager) {
    $this->config = $config_factory
      ->get('geocoder.settings');
    $this->providerPluginManager = $provider_plugin_manager;
  }

  /**
   * {@inheritdoc}
   */
  public function geocode($data, array $plugins, array $options = []) {

    // Retrieve plugins options from the module configurations.
    $plugins_options = $this->config
      ->get('plugins_options') ?: [];

    // Merge possible options overrides into plugins options.
    $plugins_options = NestedArray::mergeDeep($plugins_options, $options);
    foreach ($plugins as $plugin_id) {

      // Transform in empty array a null value for the plugin id options.
      $plugins_options += [
        $plugin_id => [],
      ];
      try {
        $provider = $this->providerPluginManager
          ->createInstance($plugin_id, $plugins_options[$plugin_id]);
        return $provider
          ->geocode($data);
      } catch (\Exception $e) {
        static::log($e
          ->getMessage());
      }
    }
    return FALSE;
  }

  /**
   * {@inheritdoc}
   */
  public function reverse($latitude, $longitude, array $plugins, array $options = []) {

    // Retrieve plugins options from the module configurations.
    $plugins_options = $this->config
      ->get('plugins_options');

    // Merge possible options overrides into plugins options.
    $plugins_options = NestedArray::mergeDeep($plugins_options, $options);
    foreach ($plugins as $plugin_id) {

      // Transform in empty array a null value for the plugin id options.
      $plugins_options += [
        $plugin_id => [],
      ];
      try {
        $provider = $this->providerPluginManager
          ->createInstance($plugin_id, $plugins_options[$plugin_id]);
        return $provider
          ->reverse($latitude, $longitude);
      } catch (\Exception $e) {
        static::log($e
          ->getMessage());
      }
    }
    return FALSE;
  }

  /**
   * Log a message in the Drupal watchdog and on screen.
   *
   * @param string $message
   *   The message.
   */
  public static function log($message) {
    \Drupal::logger('geocoder')
      ->warning($message);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Geocoder::$config protected property The config factory service.
Geocoder::$providerPluginManager protected property The geocoder provider plugin manager service.
Geocoder::geocode public function Geocodes a string. Overrides GeocoderInterface::geocode
Geocoder::log public static function Log a message in the Drupal watchdog and on screen.
Geocoder::reverse public function Reverse geocode coordinates. Overrides GeocoderInterface::reverse
Geocoder::__construct public function Constructs a geocoder factory class.