View source
<?php
namespace Drupal\geocoder\Plugin\Geocoder\Provider;
use Drupal\Core\Locale\CountryManager;
use Drupal\geocoder\ProviderBase;
use Geocoder\Model\Address;
use Geocoder\Model\AddressCollection;
use Geocoder\Model\AdminLevelCollection;
class Random extends ProviderBase {
protected $addressFactory;
protected $moduleHandler;
protected function doGeocode($source) {
return new AddressCollection([
$this
->getAddressFactory()
->createFromArray($this
->getRandomResult()),
]);
}
public function doReverse($latitude, $longitude) {
$result = $this
->getRandomResult();
$result['latitude'] = $latitude;
$result['longitude'] = $longitude;
return new AddressCollection([
$this
->getAddressFactory()
->createFromArray($result),
]);
}
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;
}
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' => (string) mt_rand(1, 1000),
'postalCode' => (string) mt_rand(1, 1000),
'locality' => sha1(mt_rand() / mt_getrandmax()),
'country' => (string) $country['name'],
'countryCode' => $country['code'],
];
}
protected function getAddressFactory() {
if (!isset($this->addressFactory)) {
$this->addressFactory = new Address('', new AdminLevelCollection());
}
return $this->addressFactory;
}
protected function getModuleHandler() {
if (!isset($this->moduleHandler)) {
$this->moduleHandler = \Drupal::moduleHandler();
}
return $this->moduleHandler;
}
}