You are here

Random.php in Geocoder 8.2

File

src/Plugin/Geocoder/Provider/Random.php
View source
<?php

namespace Drupal\geocoder\Plugin\Geocoder\Provider;

use Drupal\Core\Locale\CountryManager;
use Drupal\geocoder\ProviderBase;
use Geocoder\Model\AddressFactory;

/**
 * Class Random.
 *
 * @GeocoderProvider(
 *  id = "random",
 *  name = "Random"
 * )
 */
class Random extends ProviderBase {

  /**
   * The address factory.
   *
   * @var \Geocoder\Model\AddressFactory
   */
  protected $addressFactory;

  /**
   * The module handler service.
   *
   * @var \Drupal\Core\Extension\ModuleHandlerInterface
   */
  protected $moduleHandler;

  /**
   * {@inheritdoc}
   */
  protected function doGeocode($source) {
    return $this
      ->getAddressFactory()
      ->createFromArray([
      $this
        ->getRandomResult(),
    ]);
  }

  /**
   * {@inheritdoc}
   */
  public function doReverse($latitude, $longitude) {
    $result = $this
      ->getRandomResult();
    $result['latitude'] = $latitude;
    $result['longitude'] = $longitude;
    return $this
      ->getAddressFactory()
      ->createFromArray([
      $result,
    ]);
  }

  /**
   * Get Random Country info.
   *
   * @todo [cc]: Tidy-up, document, etc.
   */
  private function getRandomCountryInfo($type = NULL) {
    $manager = new CountryManager($this
      ->getModuleHandler());
    $countries = $manager
      ->getList();
    uksort($countries, function () {
      return rand() > rand();
    });
    $country = array_slice($countries, 0, 1);
    $value = [
      'code' => key($country),
      'name' => reset($country),
    ];
    if (is_null($type)) {
      return $value;
    }
    return isset($value[$type]) ? $value[$type] : $value;
  }

  /**
   * Generate a fake random address array.
   *
   * @todo [cc]: Tidi-up, document, etc.
   *
   * @return array
   *   Return array of dta such as latitude, longitude, etc.
   */
  protected function getRandomResult() {
    $country = $this
      ->getRandomCountryInfo();
    $streetTypes = [
      'street',
      'avenue',
      'square',
      'road',
      'way',
      'drive',
      'lane',
      'place',
      'hill',
      'gardens',
      'park',
    ];
    return [
      'latitude' => mt_rand(0, 90) + mt_rand() / mt_getrandmax(),
      'longitude' => mt_rand(-180, 180) + mt_rand() / mt_getrandmax(),
      'streetName' => $this
        ->getRandomCountryInfo('name') . ' ' . $streetTypes[mt_rand(0, count($streetTypes) - 1)],
      'streetNumber' => mt_rand(1, 1000),
      'postalCode' => mt_rand(1, 1000),
      'locality' => sha1(mt_rand() / mt_getrandmax()),
      'country' => $country['name'],
      'countryCode' => $country['code'],
    ];
  }

  /**
   * Returns the address factory.
   *
   * @return \Geocoder\Model\AddressFactory
   *   Return the address Factory.
   */
  protected function getAddressFactory() {
    if (!isset($this->addressFactory)) {
      $this->addressFactory = new AddressFactory();
    }
    return $this->addressFactory;
  }

  /**
   * Returns the module handler service.
   *
   * @return \Drupal\Core\Extension\ModuleHandlerInterface
   *   Return the module Handler.
   */
  protected function getModuleHandler() {
    if (!isset($this->moduleHandler)) {
      $this->moduleHandler = \Drupal::moduleHandler();
    }
    return $this->moduleHandler;
  }

}

Classes

Namesort descending Description
Random Class Random.