From c0a6363b0b427f78a9dd7a6f4d36bfebeb7f4c0b Mon Sep 17 00:00:00 2001 From: Hafiq Date: Fri, 4 May 2018 20:16:13 +0800 Subject: [PATCH] citylink completed --- README.md | 6 +++ src/Tracker/BaseTracker.php | 10 ++-- src/Tracker/CityLink.php | 99 +++++++++++++++++++++++++------------ tests/CityLinkTest.php | 35 +++++++++++++ 4 files changed, 113 insertions(+), 37 deletions(-) create mode 100644 tests/CityLinkTest.php diff --git a/README.md b/README.md index 6729f15..88b9bc8 100644 --- a/README.md +++ b/README.md @@ -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 @@ -103,6 +104,11 @@ $data = parcel_track() SkyNet Express Courier + + cityLink() + + City Link Express Courier + setTrackingNumber($refNumber) String diff --git a/src/Tracker/BaseTracker.php b/src/Tracker/BaseTracker.php index 10720d4..dd0a81c 100644 --- a/src/Tracker/BaseTracker.php +++ b/src/Tracker/BaseTracker.php @@ -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'] @@ -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"; } @@ -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"; } diff --git a/src/Tracker/CityLink.php b/src/Tracker/CityLink.php index fcafab3..10496f9 100644 --- a/src/Tracker/CityLink.php +++ b/src/Tracker/CityLink.php @@ -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; } } \ No newline at end of file diff --git a/tests/CityLinkTest.php b/tests/CityLinkTest.php new file mode 100644 index 0000000..f906e6f --- /dev/null +++ b/tests/CityLinkTest.php @@ -0,0 +1,35 @@ +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']); + } +} \ No newline at end of file