From 8ff991bd0178d305f610de8f32cc11437f034364 Mon Sep 17 00:00:00 2001 From: Hafiq Date: Sat, 26 May 2018 23:40:23 +0800 Subject: [PATCH] new ups --- README.md | 6 +++ changelog.md | 4 ++ example/index.php | 3 +- src/Contract/BaseParcelTrack.php | 13 +++++- src/Tracker/UPS.php | 76 ++++++++++++++++++++++++++++++++ tests/DHLCommerceTest.php | 2 +- tests/UPSTest.php | 43 ++++++++++++++++++ 7 files changed, 144 insertions(+), 3 deletions(-) create mode 100644 src/Tracker/UPS.php create mode 100644 tests/UPSTest.php diff --git a/README.md b/README.md index 1db8596..ef61f69 100644 --- a/README.md +++ b/README.md @@ -20,6 +20,7 @@ BTW, Currently available (Successfully Scraped) 8. [FedEx Express](https://www.fedex.com/my/) 9. [LEL Express](http://www.lex.com.my/) 10. [KTM Distribution Sdn Bhd](http://www.ktmd.com.my/tracking/) +11. [UPS](https://wwwapps.ups.com/WebTracking/track) Tested in PHP 7.1 Only @@ -141,6 +142,11 @@ $data = parcel_track() ktmd() KTM Distribution Sdn Bhd + + + ups() + + United Parcel Service Courier/td> setTrackingNumber($refNumber) diff --git a/changelog.md b/changelog.md index ca1edea..12c1d24 100644 --- a/changelog.md +++ b/changelog.md @@ -2,6 +2,10 @@ All notable changes to this project will be documented in this file. +## 1.11 +Add New Courier UPS Express +- add United Parcel Service (M) Sdn Bhd (Main) + ## 1.10 Add New Courier KTMD - add KTM Distribution Sdn Bhd diff --git a/example/index.php b/example/index.php index d1d309a..940aad9 100644 --- a/example/index.php +++ b/example/index.php @@ -19,7 +19,8 @@ //$response = parcel_track()->postLaju()->setTrackingNumber("ER287051644MY")->fetch(); //$response = parcel_track()->lelExpress()->setTrackingNumber("MYMP000000573505")->fetch(); //$response = parcel_track()->dhlECommerce()->setTrackingNumber("5218031053514008AAAA")->fetch(); -$response = parcel_track()->ktmd()->setTrackingNumber("103154269")->fetch(); +//$response = parcel_track()->ktmd()->setTrackingNumber("103154269")->fetch(); +$response = parcel_track()->ups()->setTrackingNumber("1Z0V255F0498628539")->fetch(); //$response = parcel_track()->setTrackingNumber("EZP843055940197")->checkCourier(); diff --git a/src/Contract/BaseParcelTrack.php b/src/Contract/BaseParcelTrack.php index e79119b..392e167 100644 --- a/src/Contract/BaseParcelTrack.php +++ b/src/Contract/BaseParcelTrack.php @@ -19,6 +19,7 @@ use afiqiqmal\ParcelTrack\Tracker\LELExpress; use afiqiqmal\ParcelTrack\Tracker\PosLaju; use afiqiqmal\ParcelTrack\Tracker\SkyNet; +use afiqiqmal\ParcelTrack\Tracker\UPS; use Carbon\Carbon; class BaseParcelTrack @@ -87,6 +88,12 @@ public function ktmd() return $this; } + public function ups() + { + $this->source = new UPS(); + return $this; + } + protected function getWhichCourier() { $courier_matched = []; @@ -102,6 +109,10 @@ protected function getWhichCourier() $courier_matched[] = (new LELExpress())->getSourceName(); } + if (preg_match('/(1Z.\d{15})|\T\d{10}|\d{9,12}/', $this->trackingCode)) { + $courier_matched[] = (new UPS())->getSourceName(); + } + if (preg_match('/^\d{8,13}$/', $this->trackingCode)) { $courier_matched[] = (new Gdex())->getSourceName(); $courier_matched[] = (new DHL())->getSourceName(); @@ -110,7 +121,7 @@ protected function getWhichCourier() $courier_matched[] = (new KTMD())->getSourceName(); } - if (strlen($this->trackingCode) >= 14) { + if (preg_match('/^\d*$/', $this->trackingCode) && strlen($this->trackingCode) >= 14) { $courier_matched[] = (new CityLink())->getSourceName(); $courier_matched[] = (new DHLCommerce())->getSourceName(); } diff --git a/src/Tracker/UPS.php b/src/Tracker/UPS.php new file mode 100644 index 0000000..d60fb8c --- /dev/null +++ b/src/Tracker/UPS.php @@ -0,0 +1,76 @@ + $refNum, + 'track.x' => 'Track' + ]; + } + + public function startCrawl($result) + { + if (isset($result['body'])) { + $crawler = new Crawler($result['body']); + + $count = $crawler->filter('.module3 table tr:not(:first-child)')->count(); + $crawlerResult = $crawler->filter('.module3 table tr:not(:first-child)')->each(function (Crawler $node, $i) use ($count) { + $result = $node->filter('td')->each(function (Crawler $node, $x) use ($i, $count) { + return trim_spaces($node->text()); + }); + + $data = []; + $currentDate = null; + foreach ($result as $key => $item) { + if ($key == 0) { + $data['event'] = $item; + } + if ($key == 1) { + $currentDate = $item; + } + if ($key == 2) { + try { + $dates = Carbon::createFromFormat("d/m/Y H:i", $currentDate . ' ' . $item); + $data['date'] = $dates->toDateTimeString(); + $data['timestamp'] = $dates->timestamp; + } catch (\Exception $exception) { + $data['date'] = null; + $data['timestamp'] = 0; + } + } + + if ($key == 3) { + $data['process'] = $item; + $data['type'] = $this->distinguishProcess($item, $i == ($count - 1)); + } + } + + return $data; + }); + + return $this->buildResponse($result, $crawlerResult); + } + + return $this->buildResponse($result, []); + } +} \ No newline at end of file diff --git a/tests/DHLCommerceTest.php b/tests/DHLCommerceTest.php index 6d06b63..7b84b67 100644 --- a/tests/DHLCommerceTest.php +++ b/tests/DHLCommerceTest.php @@ -23,7 +23,7 @@ function testDHLCommerceSuccess() function testDHLCommerceEmptySuccess() { - $result = parcel_track()->dhlECommerce()->setTrackingNumber("5218031053514008AAAA")->fetch(); + $result = parcel_track()->dhlECommerce()->setTrackingNumber("521803105351400")->fetch(); $this->assertTrue(count($result['tracker']['checkpoints']) == 0); $this->assertEquals(200, $result['code']); diff --git a/tests/UPSTest.php b/tests/UPSTest.php new file mode 100644 index 0000000..3c068b4 --- /dev/null +++ b/tests/UPSTest.php @@ -0,0 +1,43 @@ +ups()->setTrackingNumber("1Z0V255F0498628539")->fetch(); + + $this->assertTrue(true); + $this->assertEquals(200, $result['code']); + } + + function testUPSEmptySuccess() + { + $result = parcel_track()->ups()->setTrackingNumber("")->fetch(); + + $this->assertTrue(count($result['tracker']['checkpoints']) == 0); + $this->assertEquals(200, $result['code']); + } + + function testUPSFailed() + { + $result = parcel_track()->setTrackingNumber("1Z0V255F04986285")->fetch(); + $this->assertTrue($result['error']); + $this->assertEquals(400, $result['code']); + } + + function testUPSCheckCarrier() + { + $result = parcel_track()->setTrackingNumber("1Z0V255F0498628539")->checkCourier(); + $this->assertFalse($result['error']); + $this->assertTrue(in_array((new UPS())->getSourceName(), $result['possible_courier'])); + } +} \ No newline at end of file