You are here

protected function SearchApiLocationPoint::parsePoint in Search API Location 8

Parses a point into a latitude/longitude pair.

Parameters

string $argument: A point specification in any format.

Return value

float[]|false An associative array with "lat" and "lon" representing a point. Or FALSE if the format was not recognized.

1 call to SearchApiLocationPoint::parsePoint()
SearchApiLocationPoint::query in modules/search_api_location_views/src/Plugin/views/argument/SearchApiLocationPoint.php
Set up the query for this argument.

File

modules/search_api_location_views/src/Plugin/views/argument/SearchApiLocationPoint.php, line 93

Class

SearchApiLocationPoint
Provides a contextual filter for defining a location filter point.

Namespace

Drupal\search_api_location_views\Plugin\views\argument

Code

protected function parsePoint($argument) {
  $point = [];
  if (class_exists(\geoPHP::class)) {

    // Try to use geoPHP to read type.
    try {
      $format = \geoPHP::detectFormat($argument);
      if ($format) {
        $args = explode(':', $format);
        array_unshift($args, $argument);
        $location = call_user_func_array([
          'geoPHP',
          'load',
        ], $args);
        $point['lat'] = $location
          ->y();
        $point['lon'] = $location
          ->x();
      }
    } catch (\Exception $e) {

      // GeoPHP couldn't handle type. Treat as invalid/no argument, silently.
    }
  }
  if (empty($point)) {

    // Try Solr LatLonType.
    if (preg_match("/^([+-]?[0-9]+(?:\\.[0-9]+)?),([+-]?[0-9]+(?:\\.[0-9]+)?)\$/", $argument, $match)) {
      $point['lat'] = $match[1];
      $point['lon'] = $match[2];
    }
  }
  return empty($point) ? FALSE : $point;
}