Skip to content

Commit

Permalink
Merge pull request #3 from perfacilis/fix/no-results-crash
Browse files Browse the repository at this point in the history
No longer crash on empty result set
  • Loading branch information
royarisse authored Jul 6, 2021
2 parents d80b0af + 826f94f commit ab8bd1a
Showing 1 changed file with 19 additions and 5 deletions.
24 changes: 19 additions & 5 deletions src/Geocoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,7 @@ private function query(array $params): array

private function queryEndpoint(array $params): array
{
// Append api key
$params['key'] = $this->api_key;

// Build URL with query parameters
$url = self::ENDPOINT . '?' . http_build_query($params);
$url = $this->buildEndpointUrl($params);

$ch = curl_init();
curl_setopt_array($ch, [
Expand All @@ -116,9 +112,27 @@ private function queryEndpoint(array $params): array
throw new Exception(sprintf('Geocoder api returned error: %s', $result['error_message']));
}

if (empty($result['items'])) {
$status = $result['status'] ?: 'no results';
throw new Exception(sprintf(
'Geocode api returned %s for query %s',
$status,
var_export($params, true)
));
}

return $result;
}

private function buildEndpointUrl(array $params): string
{
// Append api key
$params['key'] = $this->api_key;

// Build URL with query parameters
return self::ENDPOINT . '?' . http_build_query($params);
}

/**
* Get result from cache, if cacher is set
*
Expand Down

0 comments on commit ab8bd1a

Please sign in to comment.