Skip to content

Commit

Permalink
Merge pull request #8 from Luke-Shepp/feature/language-selection
Browse files Browse the repository at this point in the history
Add ability to fetch translated Loqate address
  • Loading branch information
tiagomctavares authored Mar 6, 2023
2 parents 01812b3 + f363893 commit e42dd11
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 26 deletions.
5 changes: 3 additions & 2 deletions src/AddressFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,12 @@ public function suggestions($query, $country, $group_id)
/**
* @param $addressId
* @param bool $raw
* @param bool $translated
* @return Details|array
*/
public function details($addressId, bool $raw = false)
public function details($addressId, bool $raw = false, bool $translated = false)
{
return $this->addressEngine()->getDetails($addressId, $raw);
return $this->addressEngine()->getDetails($addressId, $raw, $translated);
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/Drivers/DriverContract.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ public function suggestions($query, $country, $group_id): Suggestions;
/**
* @param $id
* @param bool $raw
* @param bool $translated
* @return Details
*/
public function getDetails($id, bool $raw = false);
public function getDetails($id, bool $raw = false, bool $translated = false);
}
25 changes: 13 additions & 12 deletions src/Drivers/LoqateDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function __construct()
$config = config('laravel-address-finder.loqate');
$this->suggestionsEndpoint = $config['api']['endpoints']['suggestions'];
$this->detailsEndpoint = $config['api']['endpoints']['details'];
$this->client = Http::withOptions([
$this->client = Http::withOptions([
'base_uri' => $config['api']['base_uri'],
'query' => [
'Key' => $config['api']['key'],
Expand Down Expand Up @@ -69,9 +69,7 @@ public function suggestions($query, $country, $group_id): Suggestions
*/
public function parseSuggestions($response)
{
/**
* @var $suggestions Suggestions
*/
/** @var Suggestions $suggestions */
$suggestions = app(Suggestions::class);

foreach ($response['Items'] ?? [] as $item) {
Expand All @@ -92,29 +90,32 @@ public function parseSuggestions($response)

/**
* @param $id
* @param bool $raw
* @param bool $translated
* @return Details
*/
public function getDetails($id, $raw = false)
public function getDetails($id, bool $raw = false, bool $translated = false)
{
return $this->parseDetails($this->client->get(
$this->detailsEndpoint,
['Id' => $id]
)->json(), $raw);
)->json(), $raw, $translated);
}

/**
* @param $response
* @param bool $raw
* @param bool $translated
* @return Details|array
*/
public function parseDetails($response, $raw = false)
public function parseDetails($response, bool $raw = false, bool $translated = false)
{

/**
* @var $details Details
*/
/** @var Details $details */
$details = app(Details::class);

$addressDetails = array_first($response['Items']) ?? null;
$addressDetails = array_first($response['Items'], function ($item) use ($translated) {
return ! $translated || $item['Language'] === 'ENG';
});

if (! $addressDetails) {
return $details;
Expand Down
20 changes: 10 additions & 10 deletions src/Drivers/MockDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,7 @@ private function getAddressChildrenResponse()
*/
public function parseSuggestions($response)
{
/**
* @var $suggestions Suggestions
*/
/** @var Suggestions $suggestions */
$suggestions = app(Suggestions::class);

foreach($response['Items'] ?? [] as $item) {
Expand All @@ -162,9 +160,11 @@ public function parseSuggestions($response)

/**
* @param $id
* @param bool $raw
* @param bool $translated
* @return Details
*/
public function getDetails($id, bool $raw = false)
public function getDetails($id, bool $raw = false, bool $translated = false)
{
$addresses = [
'default' => [
Expand Down Expand Up @@ -385,23 +385,23 @@ public function getDetails($id, bool $raw = false)

];

return $this->parseDetails($addresses[$id], $raw);
return $this->parseDetails($addresses[$id], $raw, $translated);
}

/**
* @param $response
* @param bool $raw
* @param bool $translated
* @return Details|array
*/
public function parseDetails($response, bool $raw = false)
public function parseDetails($response, bool $raw = false, bool $translated = false)
{
/**
* @var $details Details
*/
/** @var Details $details */
$details = app(Details::class);

$addressDetails = array_first($response['Items']) ?? null;

if (!$addressDetails) {
if (! $addressDetails) {
return $details;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Facades/Address.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

/**
* @method static Suggestions suggestions($query, $country, $group_id)
* @method static Details details($id, $raw = false)
* @method static Details details($id, bool $raw = false, bool $translated = false)
*
* @see \CyberDuck\AddressFinder\AddressFinder
*
Expand Down

0 comments on commit e42dd11

Please sign in to comment.