Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
afiqiqmal committed May 24, 2018
2 parents fa2c946 + 11bc0c3 commit 900b22d
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 2 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ BTW, Currently available (Successfully Scraped)
7. [CityLink Express](http://www.citylinkexpress.com/MY/Consignment.aspx)
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/)


Tested in PHP 7.1 Only
Expand Down Expand Up @@ -136,6 +137,11 @@ $data = parcel_track()
<td></td>
<td>Lazada E-Logistic Courier</td>
</tr>
<tr>
<td>ktmd()</td>
<td></td>
<td>KTM Distribution Sdn Bhd</td>
</tr>
<tr>
<td>setTrackingNumber($refNumber)</td>
<td><code>String</code></td>
Expand Down
6 changes: 5 additions & 1 deletion 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.10
Add New Courier KTMD
- add KTM Distribution Sdn Bhd

## 1.9
Add New Courier DHL E-Commerce
- add dhl e-commerce
Expand All @@ -28,7 +32,7 @@ Fix Carbon Time
## 1.7
Add LeLExpress courier

- Add lel lazada courier
- Add LEL Lazada courier
- Fix ApiRequest
- Fix Abx Request Body

Expand Down
3 changes: 2 additions & 1 deletion example/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
//$response = parcel_track()->lelExpress()->setTrackingNumber("MYMP000000573505")->fetch();
//$response = parcel_track()->postLaju()->setTrackingNumber("ER287051644MY")->fetch();
//$response = parcel_track()->lelExpress()->setTrackingNumber("MYMP000000573505")->fetch();
$response = parcel_track()->dhlECommerce()->setTrackingNumber("5218031053514008AAAA")->fetch();
//$response = parcel_track()->dhlECommerce()->setTrackingNumber("5218031053514008AAAA")->fetch();
$response = parcel_track()->ktmd()->setTrackingNumber("103154269")->fetch();

//$response = parcel_track()->setTrackingNumber("EZP843055940197")->checkCourier();

Expand Down
8 changes: 8 additions & 0 deletions src/Contract/BaseParcelTrack.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use afiqiqmal\ParcelTrack\Tracker\DHLCommerce;
use afiqiqmal\ParcelTrack\Tracker\FedEx;
use afiqiqmal\ParcelTrack\Tracker\Gdex;
use afiqiqmal\ParcelTrack\Tracker\KTMD;
use afiqiqmal\ParcelTrack\Tracker\LELExpress;
use afiqiqmal\ParcelTrack\Tracker\PosLaju;
use afiqiqmal\ParcelTrack\Tracker\SkyNet;
Expand Down Expand Up @@ -80,6 +81,12 @@ public function lelExpress()
return $this;
}

public function ktmd()
{
$this->source = new KTMD();
return $this;
}

protected function getWhichCourier()
{
$courier_matched = [];
Expand All @@ -100,6 +107,7 @@ protected function getWhichCourier()
$courier_matched[] = (new DHL())->getSourceName();
$courier_matched[] = (new FedEx())->getSourceName();
$courier_matched[] = (new SkyNet())->getSourceName();
$courier_matched[] = (new KTMD())->getSourceName();
}

if (strlen($this->trackingCode) >= 14) {
Expand Down
68 changes: 68 additions & 0 deletions src/Tracker/KTMD.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php
/**
* Created by PhpStorm.
* User: hafiq
* Date: 01/05/2018
* Time: 11:02 PM
*/

namespace afiqiqmal\ParcelTrack\Tracker;

use Carbon\Carbon;

class KTMD extends BaseTracker
{
protected $url = "http://parcel.ktmd.com.my/ops/ws/track_consignment.php";
protected $source = "KTM Distribution Sdn Bhd";
protected $code = "ktmd";
protected $method = PARCEL_METHOD_POST;

public function setTrackingNumber($refNum)
{
parent::setTrackingNumber($refNum);
return [
'consignments' => $refNum,
];
}

public function getHeader()
{
return [
'Accept' => 'application/json'
];
}

public function rawOutput()
{
return false;
}

public function startCrawl($result)
{
$finalOutput = [];
if (isset($result['body'])) {
$output = $result['body'];
if ($output[0]['date'] == '-') {
return $this->buildResponse($result, []);
}
$output = array_reverse($output);
foreach ($output as $key => $item) {
$data = [];
$date = trim($item['date']);
$parcel = Carbon::createFromFormat("Y-m-d", $date);

$data['date'] = $parcel->toDateTimeString();
$data['timestamp'] = $parcel->timestamp;
$data['process'] = trim_spaces($item['description']);
$data['type'] = $this->distinguishProcess(trim_spaces($item['description']), $item == reset($output));
$data['event'] = isset($item['location']) ? trim_spaces($item['location']) : null;

$finalOutput[] = $data;
}

return $this->buildResponse($result, $finalOutput, 200, false);
}

return $this->buildResponse($result, []);
}
}
44 changes: 44 additions & 0 deletions tests/KTMBTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php
namespace Tests;

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

use afiqiqmal\ParcelTrack\Tracker\CityLink;
use afiqiqmal\ParcelTrack\Tracker\DHL;
use PHPUnit\Framework\TestCase;
/**
* RequestTest.php
* to test function in Request class
*/
class KTMBTest extends TestCase
{
function testKTMBSuccess()
{
$result = parcel_track()->dhlExpress()->setTrackingNumber("103154269")->fetch();

$this->assertTrue(true);
$this->assertEquals(200, $result['code']);
}

function testKTMBEmptySuccess()
{
$result = parcel_track()->dhlExpress()->setTrackingNumber("103154269AAAA")->fetch();

$this->assertTrue(count($result['tracker']['checkpoints']) == 0);
$this->assertEquals(200, $result['code']);
}

function testKTMBFailed()
{
$result = parcel_track()->setTrackingNumber("103154269")->fetch();
$this->assertTrue($result['error']);
$this->assertEquals(400, $result['code']);
}

function testKTMBCheckCarrier()
{
$result = parcel_track()->setTrackingNumber("103154269")->checkCourier();
$this->assertFalse($result['error']);
$this->assertTrue(in_array((new DHL())->getSourceName(), $result['possible_courier']));
}
}

0 comments on commit 900b22d

Please sign in to comment.