From d623fb7dfac53b1fd6de22ac570afd43be350f09 Mon Sep 17 00:00:00 2001 From: Skylar Ittner Date: Thu, 10 Nov 2022 23:43:07 -0700 Subject: [PATCH] Fill out tracking location from a city lookup sometimes (just see comments) --- lib/Tracking_USPS.lib.php | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/lib/Tracking_USPS.lib.php b/lib/Tracking_USPS.lib.php index 8f79aac..6405735 100644 --- a/lib/Tracking_USPS.lib.php +++ b/lib/Tracking_USPS.lib.php @@ -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.