From 663a5885749c28129f6cc20bf42579c65e39220b Mon Sep 17 00:00:00 2001 From: NilsDelaguardia <69813293+NilsDelaguardia@users.noreply.github.com> Date: Tue, 12 Sep 2023 15:23:59 -0400 Subject: [PATCH] Refactor address parser to support variable keys + fully support email. (#29) * update: Use keys to assign matched address. * update: Add fallback for if fields aren't returned from ShipEngine * update: Add email to other appropriate places. --- src/Address/Address.php | 19 ++++++++++++++++ src/Address/ArrayFormatter.php | 1 + src/Address/Factory.php | 1 + src/AddressVerification/AddressMessage.php | 1 + src/AddressVerification/MatchedAddress.php | 26 ++++++++++------------ 5 files changed, 34 insertions(+), 14 deletions(-) diff --git a/src/Address/Address.php b/src/Address/Address.php index dc8d2c5..c6f725a 100644 --- a/src/Address/Address.php +++ b/src/Address/Address.php @@ -98,6 +98,16 @@ public function setPhone($phone) return $this; } + /** + * @param null|string $email + * @return Address + */ + public function setEmail($email) + { + $this->email = $email; + return $this; + } + /** * @param null|string $companyName * @return Address @@ -204,6 +214,14 @@ public function getPhone() return $this->phone; } + /** + * @return string|null + */ + public function getEmail() + { + return $this->email; + } + /** * @return string|null */ @@ -284,6 +302,7 @@ public function toArray() return [ 'name' => $this->getName(), 'phone' => $this->getPhone(), + 'email' => $this->getEmail(), 'company_name' => $this->getCompanyName(), 'address_line1' => $this->getAddressLine1(), 'address_line2' => $this->getAddressLine2(), diff --git a/src/Address/ArrayFormatter.php b/src/Address/ArrayFormatter.php index bddd515..815cb5d 100644 --- a/src/Address/ArrayFormatter.php +++ b/src/Address/ArrayFormatter.php @@ -19,6 +19,7 @@ public function format($address) return [ 'name' => $address['name'] ?? '', 'phone' => $address['phone'] ?? '', + 'email' => $address['email'] ?? '', 'company_name' => $address['company_name'] ?? '', 'address_line1' => $address['address_line1'] ?? '', 'address_line2' => $address['address_line2'] ?? '', diff --git a/src/Address/Factory.php b/src/Address/Factory.php index 95662fa..d3039f6 100644 --- a/src/Address/Factory.php +++ b/src/Address/Factory.php @@ -43,6 +43,7 @@ public function factory($address) return (new Address) ->setName($formatted['name'] ?? '') ->setPhone($formatted['phone'] ?? '') + ->setEmail($formatted['email'] ?? '') ->setCompanyName($formatted['company_name'] ?? '') ->setAddressLine1($formatted['address_line1'] ?? '') ->setAddressLine2($formatted['address_line2'] ?? null) diff --git a/src/AddressVerification/AddressMessage.php b/src/AddressVerification/AddressMessage.php index 50c389b..df33fb1 100644 --- a/src/AddressVerification/AddressMessage.php +++ b/src/AddressVerification/AddressMessage.php @@ -21,6 +21,7 @@ class AddressMessage const PROPERTY_NAMES = [ 'name', 'phone', + 'email', 'company_name', 'address_line1', 'address_line2', diff --git a/src/AddressVerification/MatchedAddress.php b/src/AddressVerification/MatchedAddress.php index 25b9b80..7dd0798 100644 --- a/src/AddressVerification/MatchedAddress.php +++ b/src/AddressVerification/MatchedAddress.php @@ -22,19 +22,17 @@ class MatchedAddress extends Address */ public function __construct(array $matchedAddress) { - list( - $this->name, - $this->phone, - $this->email, - $this->companyName, - $this->addressLine1, - $this->addressLine2, - $this->addressLine3, - $this->cityLocality, - $this->stateProvince, - $this->postalCode, - $this->countryCode, - $this->addressResidentialIndicator - ) = array_values($matchedAddress); + $this->name = $matchedAddress['name'] ?? null; + $this->phone = $matchedAddress['phone'] ?? null; + $this->email = $matchedAddress['email'] ?? null; + $this->companyName = $matchedAddress['company_name'] ?? null; + $this->addressLine1 = $matchedAddress['address_line1'] ?? ''; + $this->addressLine2 = $matchedAddress['address_line2'] ?? null; + $this->addressLine3 = $matchedAddress['address_line3'] ?? null; + $this->cityLocality = $matchedAddress['city_locality'] ?? ''; + $this->stateProvince = $matchedAddress['state_province'] ?? ''; + $this->postalCode = $matchedAddress['postal_code'] ?? ''; + $this->countryCode = $matchedAddress['country_code'] ?? ''; + $this->addressResidentialIndicator = $matchedAddress['address_residential_indicator'] ?? ''; } }