You are here

class Random in Geocoder 7.2

Same name and namespace in other branches
  1. 8.3 src/Plugin/Geocoder/Provider/Random.php \Drupal\geocoder\Plugin\Geocoder\Provider\Random
  2. 8.2 src/Plugin/Geocoder/Provider/Random.php \Drupal\geocoder\Plugin\Geocoder\Provider\Random

Class Random.

Plugin annotation


@GeocoderPlugin(
 id = "random",
 name = "Random",
 type = "Provider",
 arguments = {
  "@geocoder.http_adapter",
  "@logger.channel.default",
  "@messenger"
 }
)

Hierarchy

Expanded class hierarchy of Random

File

src/Plugin/Geocoder/Provider/Random.php, line 32
The TomTom plugin.

Namespace

Drupal\geocoder\Plugin\Geocoder\Provider
View source
class Random extends Provider implements ProviderInterface {

  /**
   * @var AddressFactory
   */
  private $factory;
  public function init() {
    $this->factory = new AddressFactory();
  }
  public function geocode($data) {
    $cid = $this
      ->getCacheCid($data);
    if ($value = $this
      ->cache_get($cid)) {
      return $value;
    }
    try {
      $value = $this->factory
        ->createFromArray(array(
        $this
          ->getRandomResult(),
      ));
    } catch (\Exception $e) {
      $this->loggerChannel
        ->error($e
        ->getMessage(), array(
        'channel' => 'geocoder',
      ));
      $this->messenger
        ->addMessage($e
        ->getMessage(), 'error', FALSE);
      $value = FALSE;
    }
    $this
      ->cache_set($cid, $value);
    return $value;
  }
  public function reverse($latitude, $longitude) {
    $cid = $this
      ->getCacheCid($latitude, $longitude);
    if ($value = $this
      ->cache_get($cid)) {
      return $value;
    }
    try {
      $result = $this
        ->getRandomResult();
      $result['latitude'] = $latitude;
      $result['longitude'] = $longitude;
      $value = $this->factory
        ->createFromArray(array(
        $result,
      ));
      $this
        ->cache_set($cid, $value);
    } catch (\Exception $e) {
      $this->loggerChannel
        ->error($e
        ->getMessage(), array(
        'channel' => 'geocoder',
      ));
      $this->messenger
        ->addMessage($e
        ->getMessage(), 'error', FALSE);
      $value = FALSE;
    }
    return $value;
  }
  private function getRandomCountryInfo($type = NULL) {
    include_once DRUPAL_ROOT . '/includes/locale.inc';
    $countries = country_get_list();
    uksort($countries, function () {
      return rand() > rand();
    });
    $country = array_slice($countries, 0, 1);
    $value = array(
      '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.
   *
   * @return array
   */
  private function getRandomResult() {
    $country = $this
      ->getRandomCountryInfo();
    $streetTypes = array(
      'street',
      'avenue',
      'square',
      'road',
      'way',
      'drive',
      'lane',
      'place',
      'hill',
      'gardens',
      'park',
    );
    return array(
      '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'],
    );
  }

}

Members

Namesort descending Modifiers Type Description Overrides
GeocoderPlugin::cache_get public function Get a cache object based on the cache ID. Overrides GeocoderPluginInterface::cache_get
GeocoderPlugin::cache_set public function Stores data in the persistent cache. Overrides GeocoderPluginInterface::cache_set
GeocoderPlugin::getCacheCid public function Generates a cache ID based on the arguments. Overrides GeocoderPluginInterface::getCacheCid
GeocoderPlugin::getConfiguration public function Get the object's configuration. Overrides GeocoderPluginInterface::getConfiguration
GeocoderPlugin::setConfiguration public function Set the object's configuration. Overrides GeocoderPluginInterface::setConfiguration
Provider::$adapter private property
Provider::$handler private property
Provider::$loggerChannel protected property The loggerChannel service.
Provider::$messenger protected property The messenger service.
Provider::getAdapter public function Returns the HTTP adapter. Overrides ProviderInterface::getAdapter
Provider::getHandler public function Get the Geocoder handler. Overrides ProviderInterface::getHandler
Provider::setHandler public function The Geocoder Provider. Overrides ProviderInterface::setHandler
Provider::__construct public function GeocoderPlugin constructor. Overrides GeocoderPlugin::__construct
Random::$factory private property
Random::geocode public function Geocode data Overrides Provider::geocode
Random::getRandomCountryInfo private function
Random::getRandomResult private function Generate a fake random address array.
Random::init public function Init method launched after object initialization. Overrides GeocoderPlugin::init
Random::reverse public function Reverse geocode latitude and longitude. Overrides Provider::reverse