Skip to content

Commit

Permalink
citylink completed
Browse files Browse the repository at this point in the history
  • Loading branch information
afiqiqmal committed May 4, 2018
1 parent 10efbcd commit c0a6363
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 37 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Currently available
3. [ABX Express](http://www.abxexpress.com.my/)
4. [DHL Express](https://www.logistics.dhl/my-en/home.html)
5. [SkyNet Express](http://www.skynet.com.my/)
5. [CityLink Express](http://www.citylinkexpress.com/MY/Consignment.aspx)

Tested in PHP 7.1

Expand Down Expand Up @@ -103,6 +104,11 @@ $data = parcel_track()
<td></td>
<td>SkyNet Express Courier</td>
</tr>
<tr>
<td>cityLink()</td>
<td></td>
<td>City Link Express Courier</td>
</tr>
<tr>
<td>setTrackingNumber($refNumber)</td>
<td><code>String</code></td>
Expand Down
10 changes: 5 additions & 5 deletions src/Tracker/BaseTracker.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ protected function buildResponse($result, $data, $status_code = 200, $reverse =
$tracker['checkpoints'] = $reverse ? array_reverse($data) : $data;

return [
'code' => $result['status_code'],
'error' => false,
'code' => $result['status_code'] ? $result['status_code'] : 400,
'error' => $result['status_code'] && $result['status_code'] >= 300 ? true : false,
'tracker' => $tracker,
'generated_at' => Carbon::now()->toDateTimeString(),
'footer' => $result['footer']
Expand All @@ -83,11 +83,11 @@ protected function distinguishProcess($process, $isFirstPosition = false)
}

$process = strtolower($process);
if (preg_match('(counter|outbound|transhipment|collection)', $process)) {
if (preg_match('(counter|outbound|transhipment|collection|collected)', $process)) {
return "item_received";
}

if (preg_match('(dispatch|picked up)', $process)) {
if (preg_match('(dispatch|picked up|depart)', $process)) {
return "dispatch";
}

Expand All @@ -99,7 +99,7 @@ protected function distinguishProcess($process, $isFirstPosition = false)
return "facility_process";
}

if (preg_match('(delivery)', $process)) {
if (preg_match('(delivery|with courier)', $process)) {
return "out_for_delivery";
}

Expand Down
99 changes: 67 additions & 32 deletions src/Tracker/CityLink.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,38 +29,73 @@ public function setTrackingNumber($refNum)

public function startCrawl($result)
{
// $crawler = new Crawler($result['body']);
// $crawlerResult = $crawler->filter('#tr4'.$this->getTrackingNumber())
// ->filter('table tr')->each(function (Crawler $node, $i) {
// $result = $node->filter('td')->each(function (Crawler $node, $i) {
// return trim_spaces($node->text());
// });
// $data = [];
// foreach ($result as $key => $item) {
// if ($key == 0) {
// $data['date'] = $item;
// }
// if ($key == 1) {
// $data['process'] = $item;
// $data['type'] = $this->distinguishProcess($item);
// }
//
// if ($key == 2) {
// $parcel = Carbon::createFromFormat("d M Y h:i a", $data['date']." ".$item);
// $data['date'] = $parcel->toDateTimeString();
// $data['timestamp'] = $parcel->timestamp;
// }
//
// if ($key == 3) {
// $data['event'] = $item;
// }
// }
//
// return $data;
// });
echo $result['body'];
die();
$crawler = new Crawler($result['body']);
// $crawlerResult = $crawler->filter('#btmPanel')->html();
$crawlerResult = $crawler->filter('#btmPanel tr')->each(function (Crawler $node, $i) {
if (strpos($node->html(), 'tabletitle') !== false) {
$result = $node->filter('td')->each(function (Crawler $node, $i) {
$value = trim_spaces($node->text());
if (strlen($value) > 2) {
return $value;
}
});

// return $this->buildResponse($result, $crawlerResult);
return $this->removeAllNullInArray($result)[0];
}

if (strpos($node->html(), 'table_detail') !== false) {
$result = $node->filter('table tr')->each(function (Crawler $node, $i) {
$result = $node->filter('td')->each(function (Crawler $node, $i) {
$value = trim_spaces($node->text());
if (strlen($value) > 2) {
return $value;
}
});

return $this->removeAllNullInArray($result);
});
return $this->removeAllNullInArray($result);
}
});

$crawlerResult = $this->removeAllNullInArray($crawlerResult);
$currentDate = null;
$finalResult = [];
foreach ($crawlerResult as $key => $item) {
//header
if ($key % 2 == 0) {
$currentDate = $item;
}

//detail
if ($key % 2 == 1) {
if (is_array($item)) {
foreach ($item as $checkpoint) {
$data = [];
$parcel = Carbon::createFromFormat('l, F d, Y H:i a', $currentDate." ".$checkpoint[1]);
$data['date'] = $parcel->toDateTimeString();
$data['timestamp'] = $parcel->timestamp;
$data['process'] = isset($checkpoint[0]) ? $checkpoint[0] : null;
$data['type'] = $this->distinguishProcess($data['process']);
$data['event'] = isset($checkpoint[2]) ? $checkpoint[2] : null;
$finalResult[] = $data;
}
}
}
}

return $this->buildResponse($result, $finalResult);
}

private function removeAllNullInArray($array)
{
$newData = [];
foreach ($array as $item) {
if ($item != null) {
$newData[] = $item;
}
}

return $newData;
}
}
35 changes: 35 additions & 0 deletions tests/CityLinkTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php
namespace Tests;

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

use PHPUnit\Framework\TestCase;
/**
* RequestTest.php
* to test function in Request class
*/
class CityLinkTest extends TestCase
{
function testCityLinkSuccess()
{
$result = parcel_track()->cityLink()->setTrackingNumber("960307804711915")->fetch();

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

function testCityLinkEmptySuccess()
{
$result = parcel_track()->cityLink()->setTrackingNumber("")->fetch();

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

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

0 comments on commit c0a6363

Please sign in to comment.