class Random in Geocoder 7.2
Same name and namespace in other branches
- 8.3 src/Plugin/Geocoder/Provider/Random.php \Drupal\geocoder\Plugin\Geocoder\Provider\Random
- 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
- class \Drupal\geocoder\Plugin\GeocoderPlugin extends \Drupal\Component\Plugin\PluginBase implements GeocoderPluginInterface- class \Drupal\geocoder\Plugin\Geocoder\Provider implements ProviderInterface- class \Drupal\geocoder\Plugin\Geocoder\Provider\Random implements ProviderInterface
 
 
- class \Drupal\geocoder\Plugin\Geocoder\Provider implements ProviderInterface
Expanded class hierarchy of Random
File
- src/Plugin/ Geocoder/ Provider/ Random.php, line 32 
- The TomTom plugin.
Namespace
Drupal\geocoder\Plugin\Geocoder\ProviderView 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
| Name   | Modifiers | Type | Description | Overrides | 
|---|---|---|---|---|
| GeocoderPlugin:: | public | function | Get a cache object based on the cache ID. Overrides GeocoderPluginInterface:: | |
| GeocoderPlugin:: | public | function | Stores data in the persistent cache. Overrides GeocoderPluginInterface:: | |
| GeocoderPlugin:: | public | function | Generates a cache ID based on the arguments. Overrides GeocoderPluginInterface:: | |
| GeocoderPlugin:: | public | function | Get the object's configuration. Overrides GeocoderPluginInterface:: | |
| GeocoderPlugin:: | public | function | Set the object's configuration. Overrides GeocoderPluginInterface:: | |
| Provider:: | private | property | ||
| Provider:: | private | property | ||
| Provider:: | protected | property | The loggerChannel service. | |
| Provider:: | protected | property | The messenger service. | |
| Provider:: | public | function | Returns the HTTP adapter. Overrides ProviderInterface:: | |
| Provider:: | public | function | Get the Geocoder handler. Overrides ProviderInterface:: | |
| Provider:: | public | function | The Geocoder Provider. Overrides ProviderInterface:: | |
| Provider:: | public | function | GeocoderPlugin constructor. Overrides GeocoderPlugin:: | |
| Random:: | private | property | ||
| Random:: | public | function | Geocode data Overrides Provider:: | |
| Random:: | private | function | ||
| Random:: | private | function | Generate a fake random address array. | |
| Random:: | public | function | Init method launched after object initialization. Overrides GeocoderPlugin:: | |
| Random:: | public | function | Reverse geocode latitude and longitude. Overrides Provider:: | 
