Skip to content

Commit

Permalink
Merge c7783e2 into 3a97edf
Browse files Browse the repository at this point in the history
  • Loading branch information
bell2041 committed Oct 18, 2019
2 parents 3a97edf + c7783e2 commit 6418dab
Showing 1 changed file with 29 additions and 4 deletions.
33 changes: 29 additions & 4 deletions src/Package.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ class Package
*/
public $tracking_code;

/**
* Returns Web URL for a specific tracking/carrier
*
* @var string
*/
public $tracking_url;

/**
* Carrier data.
*
Expand Down Expand Up @@ -119,6 +126,16 @@ public function getTrackingCode(bool $return_original = false): string
return $return_original ? $this->tracking_code_original : $this->tracking_code;
}

/**
* Gets the Web URL to open tracking status
*
* @return string
*/
public function getTrackingUrl(): string
{
return $this->tracking_url;
}

/**
* Determines the package's shipping details based on its tracking code.
*
Expand All @@ -129,17 +146,24 @@ private function deduceTrackingCode(): Package
$tracking_code = $this->tracking_code;
$carrier_code = null;
$provider_code = null;
$web_link = null;

if (preg_match('/^[0-9]{2}[0-9]{4}[0-9]{4}$/', $tracking_code, $matches)) {
$carrier_code = Carrier::CODE_DHL;
} elseif (preg_match('/^[1-9]{4}[0-9]{4}[0-9]{4}$/', $tracking_code, $matches)) {
$web_link = "https://www.dhl.com/en/express/tracking.html?AWB=".$matches[0];
} elseif (preg_match('/^[0-9]{12}$|^[0-9]{15}$/', $tracking_code, $matches)) {
$carrier_code = Carrier::CODE_FEDEX;
} elseif (preg_match('/^1Z[A-Z0-9]{3}[A-Z0-9]{3}[0-9]{2}[0-9]{4}[0-9]{4}$/i', $tracking_code)) {
$web_link = "https://www.fedex.com/fedextrack/?tracknumbers=" . $matches[0];
} elseif (preg_match('/^1Z[A-Z0-9]{3}[A-Z0-9]{3}[0-9]{2}[0-9]{4}[0-9]{4}$/i', $tracking_code, $matches)) {
$carrier_code = Carrier::CODE_UPS;
} elseif (preg_match('/^[0-9]{4}[0-9]{4}[0-9]{4}[0-9]{4}[0-9]{4}[0-9]{2}$/', $tracking_code)) {
$web_link = "http://wwwapps.ups.com/WebTracking/track?loc=en_US&trackNums=" . $matches[0] .
"&track.x=track";
} elseif (preg_match('/^[0-9]{4}[0-9]{4}[0-9]{4}[0-9]{4}[0-9]{4}[0-9]{2}$/', $tracking_code, $matches)) {
$carrier_code = Carrier::CODE_USPS;
} elseif (preg_match('/^LX[0-9]{8}$/', $tracking_code)) {
$web_link = "https://tools.usps.com/go/TrackConfirmAction_input?qtc_tLabels1=".$matches[0];
} elseif (preg_match('/^LX[0-9]{8}$/', $tracking_code, $matches)) {
$carrier_code = Carrier::CODE_LASER_SHIP;
$web_link = "https://www.lasership.com/track.php?track_number_input=".$matches[0];
} elseif (preg_match(
'/^420[0-9]{5}([0-9]{4}[0-9]{4}[0-9]{4}[0-9]{4}[0-9]{4}[0-9]{2})$/',
$tracking_code,
Expand All @@ -152,6 +176,7 @@ private function deduceTrackingCode(): Package
}

if (!empty($carrier_code)) {
$this->tracking_url = $web_link;
$this->carrier = new Carrier($carrier_code);
$this->provider = new Provider($provider_code ?: $carrier_code);
}
Expand Down

0 comments on commit 6418dab

Please sign in to comment.