Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 56 additions & 4 deletions src/Provider/Geonames/Geonames.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,38 @@ final class Geonames extends AbstractHttpProvider implements Provider
*/
private $username;

/**
* @var string|null
*/
private $token;

/**
* @var string
*/
private $baseUrl;

/**
* @param ClientInterface $client An HTTP adapter
* @param string $username Username login (Free registration at http://www.geonames.org/login)
* @param string|null $token Optional token for premium accounts
* @param bool $secure Use secure endpoint (https://secure.geonames.net) for premium accounts
*/
public function __construct(ClientInterface $client, string $username)
public function __construct(ClientInterface $client, string $username, ?string $token = null, bool $secure = false)
{
if (empty($username)) {
throw new InvalidCredentials('No username provided.');
}

$this->username = $username;
$this->token = $token;

// Determine base URL based on secure flag
if ($secure) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use strict comparison.

Suggested change
if ($secure) {
if ($secure === true) {

$this->baseUrl = 'https://secure.geonames.net';
} else {
$this->baseUrl = 'http://api.geonames.org';
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please keep the use of constants. You seem to have made the existing constants irrelevant.


parent::__construct($client);
}

Expand All @@ -75,7 +96,15 @@ public function geocodeQuery(GeocodeQuery $query): Collection
throw new UnsupportedOperation('The Geonames provider does not support IP addresses.');
}

$url = sprintf(self::GEOCODE_ENDPOINT_URL, urlencode($address), $query->getLimit(), $this->username);
$url = sprintf(
'%s/searchJSON?q=%s&maxRows=%d&style=full&username=%s',
$this->baseUrl,
urlencode($address),
$query->getLimit(),
$this->username
);

$url = $this->appendToken($url);

return $this->executeQuery($url, $query->getLocale());
}
Expand All @@ -86,7 +115,16 @@ public function reverseQuery(ReverseQuery $query): Collection
$longitude = $coordinates->getLongitude();
$latitude = $coordinates->getLatitude();

$url = sprintf(self::REVERSE_ENDPOINT_URL, $latitude, $longitude, $query->getLimit(), $this->username);
$url = sprintf(
'%s/findNearbyPlaceNameJSON?lat=%F&lng=%F&style=full&maxRows=%d&username=%s',
$this->baseUrl,
$latitude,
$longitude,
$query->getLimit(),
$this->username
);

$url = $this->appendToken($url);

return $this->executeQuery($url, $query->getLocale());
}
Expand All @@ -96,7 +134,7 @@ public function reverseQuery(ReverseQuery $query): Collection
*/
public function getCountryInfo(?string $country = null, ?string $locale = null): array
{
$url = sprintf(self::BASE_ENDPOINT_URL, 'countryInfoJSON', $this->username);
$url = sprintf('%s/countryInfoJSON?username=%s', $this->baseUrl, $this->username);

if (isset($country)) {
$url = sprintf('%s&country=%s', $url, $country);
Expand All @@ -109,6 +147,8 @@ public function getCountryInfo(?string $country = null, ?string $locale = null):
$url = sprintf('%s&lang=%s', $url, substr($locale, 0, 2));
}

$url = $this->appendToken($url);

$content = $this->getUrlContents($url);
if (null === $json = json_decode($content)) {
throw InvalidServerResponse::create($url);
Expand Down Expand Up @@ -217,4 +257,16 @@ private function executeQuery(string $url, ?string $locale = null): AddressColle

return new AddressCollection($results);
}

/**
* Append token parameter to URL if token is provided.
*/
private function appendToken(string $url): string
{
if (null !== $this->token && '' !== $this->token) {
$url = sprintf('%s&token=%s', $url, $this->token);
}

return $url;
}
}
Loading