Skip to content
This repository has been archived by the owner on Aug 28, 2024. It is now read-only.

Commit

Permalink
Refactor address parser to support variable keys + fully support emai…
Browse files Browse the repository at this point in the history
…l. (#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.
  • Loading branch information
NilsDelaguardia authored Sep 12, 2023
1 parent 402c59a commit 663a588
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 14 deletions.
19 changes: 19 additions & 0 deletions src/Address/Address.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -204,6 +214,14 @@ public function getPhone()
return $this->phone;
}

/**
* @return string|null
*/
public function getEmail()
{
return $this->email;
}

/**
* @return string|null
*/
Expand Down Expand Up @@ -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(),
Expand Down
1 change: 1 addition & 0 deletions src/Address/ArrayFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'] ?? '',
Expand Down
1 change: 1 addition & 0 deletions src/Address/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions src/AddressVerification/AddressMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class AddressMessage
const PROPERTY_NAMES = [
'name',
'phone',
'email',
'company_name',
'address_line1',
'address_line2',
Expand Down
26 changes: 12 additions & 14 deletions src/AddressVerification/MatchedAddress.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'] ?? '';
}
}

0 comments on commit 663a588

Please sign in to comment.