/** * Match a given address to the most accurate Destination * @param string $country * @param string $state * @param string $zip * @return object :: The matching destination */ public static function LoadMatching($country, $state, $zip) { //We may get id numbers instead of text strings so convert here if (is_numeric($country)) { $country = Country::CodeById($country); } if (is_numeric($state)) { $state = State::CodeById($state); } $arrDestinations = Destination::LoadByCountry($country); if (is_array($arrDestinations) === false && $arrDestinations instanceof Traversable === false) { return null; } $objState = State::LoadByCode($state, $country); $zip = preg_replace('/[^A-Z0-9]/', '', strtoupper($zip)); foreach ($arrDestinations as $objDestination) { if ($objDestination->state == null || $objState !== null && $objState->id == $objDestination->state) { $zipStart = $objDestination->Zipcode1; $zipEnd = $objDestination->Zipcode2; if ($zipStart <= $zip && $zipEnd >= $zip || $zipStart == '' || $zipStart == '*' || $zip == '') { return $objDestination; } } } return null; }