Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[LocationIQ] Add autocomplete + countrycodes & tag filters #1118

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
92 changes: 87 additions & 5 deletions src/Provider/LocationIQ/LocationIQ.php
Expand Up @@ -32,7 +32,7 @@ final class LocationIQ extends AbstractHttpProvider implements Provider
/**
* @var string
*/
const BASE_API_URL = 'https://locationiq.org/v1';
const BASE_API_URL = 'https://api.locationiq.com/v1';

/**
* @var string
Expand All @@ -58,15 +58,49 @@ public function __construct(HttpClient $client, string $apiKey)
* {@inheritdoc}
*/
public function geocodeQuery(GeocodeQuery $query): Collection
{
if ($query->getData('autocomplete') == true) {
return $this->autocompleteQuery($query);
} else {
return $this->searchQuery($query);
}
}

/**
* {@inheritdoc}
*/
private function autocompleteQuery(GeocodeQuery $query): Collection
{
$address = $query->getText();
$countrycodes = $query->getData('countrycodes');
$tag = $query->getData('tag');
$url = sprintf($this->getGeocodeAutocompleteEndpointUrl(), urlencode($address), $query->getLimit(), $countrycodes, $tag);
Copy link
Member

Choose a reason for hiding this comment

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

What if countrycodes and/or tag is not set ?


$content = $this->executeQuery($url, $query->getLocale());
$places = json_decode($content, true);

$url = sprintf($this->getGeocodeEndpointUrl(), urlencode($address), $query->getLimit());
$results = [];
foreach ($places as $place) {
$results[] = $this->arrayResultToArray($place);
}

return new AddressCollection($results);
}

/**
* {@inheritdoc}
*/
private function searchQuery(GeocodeQuery $query): Collection
{
$address = $query->getText();
$countrycodes = $query->getData('countrycodes');
$tag = $query->getData('tag');
$url = sprintf($this->getGeocodeSearchEndpointUrl(), urlencode($address), $query->getLimit(), $countrycodes, $tag);

$content = $this->executeQuery($url, $query->getLocale());

$doc = new \DOMDocument();
if (!@$doc->loadXML($content) || null === $doc->getElementsByTagName('searchresults')->item(0)) {
if (!@$doc->loadXML($content) || null === $doc->getElementsByTagName('searchresults')-> item(0)) {
throw InvalidServerResponse::create($url);
}

Expand Down Expand Up @@ -108,6 +142,49 @@ public function reverseQuery(ReverseQuery $query): Collection
return new AddressCollection([$this->xmlResultToArray($result, $addressParts)]);
}

/**
* @param array $arrayResult
* @return Location
*/
private function arrayResultToArray(array $arrayResult): Location
Copy link
Member

Choose a reason for hiding this comment

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

Name of the function is confusing I think. I would name it something like resultToLocation().

{
$builder = new AddressBuilder($this->getName());

$builder->setPostalCode($arrayResult['address']['postcode'] ?? null);
$builder->setSubLocality($arrayResult['address']['suburb'] ?? null);
$builder->setCountry($arrayResult['address']['country'] ?? null);
$builder->setCoordinates($arrayResult['lat'], $arrayResult['lon']);

if ($arrayResult['osm_type'] == 'way') {
$builder->setStreetName($arrayResult['address']['name']);
$builder->setStreetNumber($arrayResult['address']['house_number'] ?? null);
}

if ($countryCode = $arrayResult['address']['country_code']) {
$builder->setCountryCode(strtoupper($countryCode));
}

if (!empty($arrayResult['boundingbox'])) {
$builder->setBounds($arrayResult['boundingbox'][0], $arrayResult['boundingbox'][1], $arrayResult['boundingbox'][2], $arrayResult['boundingbox'][3]);
}

if (in_array($arrayResult['type'], ['city', 'town', 'administrative'])) {
$builder->setLocality($arrayResult['address']['name']);
$builder->addAdminLevel(2, $arrayResult['address']['name']);
} else if (!empty($arrayResult['address']['city'])) {
$builder->setLocality($arrayResult['address']['city']);
$builder->addAdminLevel(2, $arrayResult['address']['city']);
}

if ($arrayResult['type'] == 'state') {
$builder->addAdminLevel(1, $arrayResult['address']['name']);
} else if(!empty($arrayResult['address']['state'])) {
$builder->addAdminLevel(1, $arrayResult['address']['state']);
}

return $builder->build();
}

/**
* @param \DOMElement $resultNode
* @param \DOMElement $addressNode
Expand Down Expand Up @@ -175,9 +252,14 @@ private function executeQuery(string $url, string $locale = null): string
return $this->getUrlContents($url);
}

private function getGeocodeEndpointUrl(): string
private function getGeocodeSearchEndpointUrl(): string
{
return self::BASE_API_URL.'/search.php?q=%s&format=xmlv1.1&addressdetails=1&normalizecity=1&limit=%d&countrycodes=%s&tag=%s&key='.$this->apiKey;
}

private function getGeocodeAutocompleteEndpointUrl(): string
{
return self::BASE_API_URL.'/search.php?q=%s&format=xmlv1.1&addressdetails=1&normalizecity=1&limit=%d&key='.$this->apiKey;
return self::BASE_API_URL.'/autocomplete.php?q=%s&addressdetails=1&normalizecity=1&limit=%d&countrycodes=%s&tag=%s&key='.$this->apiKey;
}

private function getReverseEndpointUrl(): string
Expand Down