Fill out tracking location from a city lookup sometimes (just see comments)
This commit is contained in:
parent
9c0b2bf81d
commit
d623fb7dfa
@ -2,6 +2,15 @@
|
||||
|
||||
class Tracking_USPS {
|
||||
|
||||
/**
|
||||
* Sometimes the API returns a city but no state or ZIP. This is a lookup table of city => state
|
||||
* so the API can return a more complete response.
|
||||
*/
|
||||
public $STATELESS_CITIES = [
|
||||
"LOS ANGELES" => "CA",
|
||||
"NEW YORK" => "NY"
|
||||
];
|
||||
|
||||
/**
|
||||
*
|
||||
* @param string $code
|
||||
@ -67,6 +76,16 @@ class Tracking_USPS {
|
||||
$current_location->state = (string) $trackinfo->TrackSummary->EventState ?? "";
|
||||
$current_location->zip = (string) $trackinfo->TrackSummary->EventZIPCode ?? "";
|
||||
$current_location->country = (string) $trackinfo->TrackSummary->EventCountry ?? "";
|
||||
|
||||
/*
|
||||
* Fill in state from list above when it's missing from the API response
|
||||
*/
|
||||
if ($current_location->state == "" && $current_location->zip == "") {
|
||||
if (in_array(strtoupper($current_location->city), $STATELESS_CITIES)) {
|
||||
$current_location->state = $STATELESS_CITIES[$current_location->city];
|
||||
}
|
||||
}
|
||||
|
||||
$current_status->setLocation($current_location);
|
||||
|
||||
$info->setCurrentStatus($current_status);
|
||||
@ -89,7 +108,6 @@ class Tracking_USPS {
|
||||
|
||||
$info->setTo($to);
|
||||
|
||||
|
||||
for ($i = 0; $i < count($trackinfo->TrackDetail); $i++) {
|
||||
$history = $trackinfo->TrackDetail[$i];
|
||||
$location = new Location();
|
||||
@ -97,6 +115,14 @@ class Tracking_USPS {
|
||||
$location->state = (string) $history->EventState ?? "";
|
||||
$location->zip = (string) $history->EventZIPCode ?? "";
|
||||
$location->country = (string) $history->EventCountry ?? "";
|
||||
/*
|
||||
* Fill in state from list above when it's missing from the API response
|
||||
*/
|
||||
if ($location->state == "" && $location->zip == "") {
|
||||
if (in_array(strtoupper($location->city), $STATELESS_CITIES)) {
|
||||
$location->state = $STATELESS_CITIES[$location->city];
|
||||
}
|
||||
}
|
||||
if ((empty($history->EventDate) || empty($history->EventTime)) && $i < count($trackinfo->TrackDetail) - 1) {
|
||||
// If there's no date/time for some reason (yes this happens sometimes apparently),
|
||||
// just make it 60 seconds after the previous event.
|
||||
|
Loading…
x
Reference in New Issue
Block a user