You are here

public function GeocoderArgument::getParsedReferenceLocation in Geolocation Field 8.2

Same name and namespace in other branches
  1. 8.3 src/Plugin/views/argument/GeocoderArgument.php \Drupal\geolocation\Plugin\views\argument\GeocoderArgument::getParsedReferenceLocation()

Processes the passed argument into an array of relevant geolocation data.

Return value

array|bool The calculated values.

Overrides ProximityArgument::getParsedReferenceLocation

File

src/Plugin/views/argument/GeocoderArgument.php, line 136

Class

GeocoderArgument
Argument handler for geolocation.

Namespace

Drupal\geolocation\Plugin\views\argument

Code

public function getParsedReferenceLocation() {

  // Cache the vales so this only gets processed once.
  static $values;
  if (!isset($values)) {
    $matches = [];
    preg_match('/^([^<>=]+)([<>=]+)([0-9.]+)(.*$)/', $this
      ->getValue(), $matches);
    if (count($matches) < 4) {
      return [];
    }
    $geocoder = $this->geocoderManager
      ->getGeocoder($this->options['geocoder'], $this->options['geocoder_settings']);
    if (empty($geocoder)) {
      return [];
    }
    $coordinates = $geocoder
      ->geocode($matches[1]);
    if (empty($coordinates)) {
      return [];
    }
    $values = $coordinates['location'];
    if (in_array($matches[2], [
      '<>',
      '=',
      '>=',
      '<=',
      '>',
      '<',
    ])) {
      $values['operator'] = $matches[2];
    }
    else {
      return [];
    }
    $values['distance'] = floatval($matches[3]);
    $values['unit'] = empty($matches[4]) ? 'km' : $matches[4];
  }
  return $values;
}