You are here

protected function GeocoderApiEnpoints::getAddressCollectionResponse in Geocoder 8.3

Same name and namespace in other branches
  1. 8.2 src/Controller/GeocoderApiEnpoints.php \Drupal\geocoder\Controller\GeocoderApiEnpoints::getAddressCollectionResponse()

Get Address Collection Response.

Parameters

\Geocoder\Model\AddressCollection $geo_collection: The Address Collection.

\Drupal\geocoder\DumperInterface|null $dumper: The Dumper or null.

string|null $address_format: The specific @GeocoderFormatter id to be used.

2 calls to GeocoderApiEnpoints::getAddressCollectionResponse()
GeocoderApiEnpoints::geocode in src/Controller/GeocoderApiEnpoints.php
GeocoderApiEnpoints::reverseGeocode in src/Controller/GeocoderApiEnpoints.php

File

src/Controller/GeocoderApiEnpoints.php, line 139

Class

GeocoderApiEnpoints
Class GeocoderApiEnpoints.

Namespace

Drupal\geocoder\Controller

Code

protected function getAddressCollectionResponse(AddressCollection $geo_collection, $dumper = NULL, $address_format = NULL) : void {
  $result = [];

  /** @var \Geocoder\Model\Address $geo_address **/
  foreach ($geo_collection
    ->all() as $k => $geo_address) {
    if (isset($dumper)) {
      $result[$k] = $dumper
        ->dump($geo_address);
    }
    else {
      $result[$k] = $geo_address
        ->toArray();

      // If a formatted_address property is not defined (as Google Maps
      // Geocoding does), then create it with our own formatter.
      if (!isset($result[$k]['formatted_address'])) {
        try {
          $result[$k]['formatted_address'] = $this->geocoderFormatterPluginManager
            ->createInstance($this
            ->getAddressFormatter($address_format))
            ->format($geo_address);
        } catch (\Exception $e) {
          watchdog_exception('geocoder', $e);
        }
      }

      // If a geometry property is not defined
      // (as Google Maps Geocoding does), then create it with our own dumper.
      if (!isset($result[$k]['geometry'])) {
        $result[$k]['geometry'] = $this
          ->addGeometryProperty($geo_address);
      }
    }
  }
  $this->response = new CacheableJsonResponse($result, 200);
  $this->response
    ->addCacheableDependency(CacheableMetadata::createFromObject($result));
}