Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
afiqiqmal committed May 26, 2018
2 parents 1a556e5 + 8ff991b commit f203c6e
Show file tree
Hide file tree
Showing 7 changed files with 144 additions and 3 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -141,6 +142,11 @@ $data = parcel_track()
<td>ktmd()</td>
<td></td>
<td>KTM Distribution Sdn Bhd</td>
</tr>
<tr>
<td>ups()</td>
<td></td>
<td>United Parcel Service Courier/td>
</tr>
<tr>
<td>setTrackingNumber($refNumber)</td>
Expand Down
4 changes: 4 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion example/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
13 changes: 12 additions & 1 deletion src/Contract/BaseParcelTrack.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -87,6 +88,12 @@ public function ktmd()
return $this;
}

public function ups()
{
$this->source = new UPS();
return $this;
}

protected function getWhichCourier()
{
$courier_matched = [];
Expand All @@ -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();
Expand All @@ -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();
}
Expand Down
76 changes: 76 additions & 0 deletions src/Tracker/UPS.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php
/**
* Created by PhpStorm.
* User: hafiq
* Date: 01/05/2018
* Time: 11:02 PM
*/

namespace afiqiqmal\ParcelTrack\Tracker;

use Carbon\Carbon;
use function foo\func;
use Symfony\Component\DomCrawler\Crawler;

class UPS extends BaseTracker
{
protected $url = "https://wwwapps.ups.com/WebTracking/track";
protected $source = "United Parcel Service (M) Sdn Bhd (Main)";
protected $code = "ups";
protected $method = PARCEL_METHOD_POST;

public function setTrackingNumber($refNum)
{
parent::setTrackingNumber($refNum);
return [
'trackNums' => $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, []);
}
}
2 changes: 1 addition & 1 deletion tests/DHLCommerceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
Expand Down
43 changes: 43 additions & 0 deletions tests/UPSTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php
namespace Tests;

require_once __DIR__ .'/../vendor/autoload.php';

use afiqiqmal\ParcelTrack\Tracker\UPS;
use PHPUnit\Framework\TestCase;
/**
* RequestTest.php
* to test function in Request class
*/
class UPSTest extends TestCase
{
function testUPSSuccess()
{
$result = parcel_track()->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']));
}
}

0 comments on commit f203c6e

Please sign in to comment.