From f363893f264e5d94b9df7d054744373c2b795e05 Mon Sep 17 00:00:00 2001 From: Luke Sheppard Date: Mon, 6 Mar 2023 16:26:28 +0000 Subject: [PATCH] Add ability to fetch translated Loqate address When fetching an address by ID, Loqate provides multiple versions of this address in different languages, such as the native language for the address and English. It is sometimes required to have the English version of the address, such as for FedEx shipments to China where FedEx only supports ASCII characters. --- src/AddressFinder.php | 5 +++-- src/Drivers/DriverContract.php | 3 ++- src/Drivers/LoqateDriver.php | 25 +++++++++++++------------ src/Drivers/MockDriver.php | 20 ++++++++++---------- src/Facades/Address.php | 2 +- 5 files changed, 29 insertions(+), 26 deletions(-) diff --git a/src/AddressFinder.php b/src/AddressFinder.php index 791c1aa..9649c56 100644 --- a/src/AddressFinder.php +++ b/src/AddressFinder.php @@ -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); } /** diff --git a/src/Drivers/DriverContract.php b/src/Drivers/DriverContract.php index 65fa8d6..85e4c29 100644 --- a/src/Drivers/DriverContract.php +++ b/src/Drivers/DriverContract.php @@ -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); } diff --git a/src/Drivers/LoqateDriver.php b/src/Drivers/LoqateDriver.php index 2d21e46..75c17be 100644 --- a/src/Drivers/LoqateDriver.php +++ b/src/Drivers/LoqateDriver.php @@ -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'], @@ -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) { @@ -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; diff --git a/src/Drivers/MockDriver.php b/src/Drivers/MockDriver.php index c3433e0..eda6f1f 100644 --- a/src/Drivers/MockDriver.php +++ b/src/Drivers/MockDriver.php @@ -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) { @@ -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' => [ @@ -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; } diff --git a/src/Facades/Address.php b/src/Facades/Address.php index 3b1b851..003292b 100644 --- a/src/Facades/Address.php +++ b/src/Facades/Address.php @@ -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 *