예제 #1
0
 /**
  * @inheritdoc
  */
 public function getData($value, $searchType = self::SEARCH_CONTAINER)
 {
     $jar = new CookieJar();
     $searches = [self::SEARCH_CONTAINER => 'CONTAINERNUMBER', self::SEARCH_BOL => 'BLNUMBER'];
     //we need receive cookies first
     $response = $this->getClient()->request('POST', $this->serviceUrl, ['form_params' => ['portlet_trackSimple_1wlw-select_key:{pageFlow.trackSimpleForm.type}' => $searches[$searchType], 'portlet_trackSimple_1{pageFlow.trackSimpleForm.numbers}' => $value, 'portlet_trackSimple_1wlw-select_key:{pageFlow.trackSimpleForm.type}OldValue' => true], 'cookies' => $jar, 'headers' => ['Referer' => "http://classic.maerskline.com/appmanager/maerskline/public?_nfpb=true&_nfls=false&_pageLabel=page_tracking3_trackSimple"]]);
     if ($response->getStatusCode() != 200) {
         throw new \Exception("Service GET url ={$this->serviceUrl} unavailable");
     }
     $html = (string) $response->getBody();
     $dom = new \DOMDocument();
     libxml_use_internal_errors(true);
     $dom->preserveWhiteSpace = false;
     $dom->loadHTML($html);
     $this->xpath = new \DOMXPath($dom);
     $table = $this->xpath->query('//table[@class = "lstBox"]');
     if (!$table->length) {
         throw new \Exception("Data not found for container {$value}");
     }
     $href = $this->xpath->query('./tr[2]/td[4]/a/@href', $table->item(0));
     $urlEvents = $this->getDomNodeValue($href);
     if (!$urlEvents) {
         throw new \Exception("Incorrect format for container {$value}");
     }
     $result = new DataResult();
     $item = $this->xpath->query('./tr[2]/td[8]', $table->item(0));
     $result->setLastPort($this->getDomNodeValue($item));
     $item = $this->xpath->query('./tr[2]/td[9]', $table->item(0));
     $eta = $this->getDomNodeValue($item);
     if (!$eta) {
         throw new \Exception("Incorrect format for container {$value}: ETA not found");
     }
     $result->setEta((new \DateTime($eta))->format('Y-m-d'));
     //we need receive cookies first
     $response = $this->getClient()->request('GET', $urlEvents, ['cookies' => $jar]);
     if ($response->getStatusCode() != 200) {
         throw new \Exception("Service GET url ={$this->serviceUrl} unavailable");
     }
     $html = (string) $response->getBody();
     $dom = new \DOMDocument();
     libxml_use_internal_errors(true);
     $dom->preserveWhiteSpace = false;
     $dom->loadHTML($html);
     $this->xpath = new \DOMXPath($dom);
     $eventTableHeaders = $this->xpath->query('//table[tr/td = "Location"]');
     if ($eventTableHeaders->length == 0) {
         throw new \Exception("Event table not found for {$value}");
     }
     $headers = $this->xpath->query('./tr[2]/td', $eventTableHeaders->item(0));
     $out = ['Activity', 'Location', 'Date, Time', 'Vessel/Carrier', 'Voyage'];
     foreach ($headers as $header) {
         $expected = array_shift($out);
         if ($expected != trim($header->nodeValue)) {
             throw new \Exception("Table format error, expected {$expected} got {$header->nodeValue} for {$value}");
         }
     }
     $eventsTable = $this->xpath->query('./tr[position() > 2]', $eventTableHeaders->item(0));
     $wasNowEvent = false;
     foreach ($eventsTable as $row) {
         $event = new EventResult();
         $isNowEvent = $this->xpath->query('./td[contains(@class, "lstNow")]', $row)->length;
         $event->setStatus($this->getDomNodeValue($this->xpath->query('./td[1]//span', $row)));
         $event->setLocation($this->getDomNodeValue($this->xpath->query('./td[2]//span', $row)));
         $date = $this->getDomNodeValue($this->xpath->query('./td[3]//span', $row));
         $event->setDate((new \DateTime($date))->format('Y-m-d H:i'));
         $event->setVessel($this->getDomNodeValue($this->xpath->query('./td[4]//span', $row)));
         $event->setVoyage($this->getDomNodeValue($this->xpath->query('./td[5]//span', $row)));
         if ($isNowEvent) {
             $wasNowEvent = true;
         }
         if ($wasNowEvent && !$isNowEvent) {
             $result->addFutureEvent($event);
         } else {
             $result->addEvent($event);
         }
     }
     foreach ($result->getEvents() as $event) {
         if ($event->getLocation() == $result->getLastPort() && $event->getStatus() == 'Discharge Full') {
             $result->setAta($event->getDate());
         }
         //First Load = LoadOnVesselAt
         if (!$result->getLoadOnVesselAt() && $event->getStatus() == 'Load Full') {
             $result->setPol($event->getLocation());
             $result->setLoadOnVesselAt($event->getDate());
         }
     }
     $containerDetails = $this->xpath->query('//fieldset[legend = "Container details"]/table/tr[2]')->item(0);
     $size = $this->getDomNodeValue($this->xpath->query('./td[2]', $containerDetails));
     $type = $this->getDomNodeValue($this->xpath->query('./td[3]', $containerDetails));
     $result->setContainerSize($size);
     $result->setContainerType($type);
     $mbl = $this->getDomNodeValue($this->xpath->query('//fieldset[legend = "Document details"]/table/tr[2]/td[1]'));
     $result->setBillOfLanding($mbl);
     return $result;
 }
예제 #2
0
 /**
  * @inheritdoc
  */
 public function getData($value, $searchType = self::SEARCH_CONTAINER)
 {
     $jar = new CookieJar();
     //we need receive cookies first
     $response = $this->getClient()->request('GET', $this->serviceUrl . $value, ['cookies' => $jar]);
     if ($response->getStatusCode() != 200) {
         throw new \Exception("Service GET url ={$this->serviceUrl} unavailable");
     }
     //with cookies we can get info
     $response = $this->getClient()->request('GET', $this->serviceUrlFrame, ['cookies' => $jar, 'headers' => ['Referer' => $this->serviceUrl . $value]]);
     if ($response->getStatusCode() != 200) {
         throw new \Exception("Service GET url ={$this->serviceUrlFrame} unavailable");
     }
     $html = (string) $response->getBody();
     $dom = new \DOMDocument();
     libxml_use_internal_errors(true);
     $dom->preserveWhiteSpace = false;
     $dom->loadHTML($html);
     $this->xpath = new \DOMXPath($dom);
     $checkNotFound = $this->xpath->query('//strong[text() = "Not Found:"]');
     if ($checkNotFound->length == 1) {
         throw new \Exception("Data not found for container {$value}");
     }
     $eventRows = $this->xpath->query('//h3[contains(text(), "Routing")]/following::table[1]/tbody/tr[position() > 1]');
     if ($eventRows->length == 0) {
         throw new \Exception("Cant find event tables for container {$value}!");
     }
     $result = new DataResult();
     foreach ($eventRows as $row) {
         $event = new EventResult();
         $item = $this->xpath->query("td[1]", $row);
         $route = $this->getDomNodeValue($item);
         $route = preg_replace('/[^a-zA-Z\\d ]/', '', $route);
         $item = $this->xpath->query("td[2]/a", $row);
         $location = $this->getDomNodeValue($item);
         $event->setLocation($location);
         if ($route == 'Origin') {
             $result->setPol($location);
         }
         if ($route == 'Destination') {
             $result->setLastPort($location);
         }
         $item = $this->xpath->query("td[3]/a", $row);
         $vessel = $this->getDomNodeValue($item);
         $event->setVessel($vessel);
         $item = $this->xpath->query("td[4]", $row);
         $arrivalDateStr = $this->getDomNodeValue($item);
         $arrivalDateStr = preg_replace('/([^a-zA-Z\\d ]+)/', ' ', $arrivalDateStr);
         if ($arrivalDateStr) {
             preg_match('/(?<date>\\d{1,2} \\w{3} \\d{2}) (?<type>\\w{1})/', $arrivalDateStr, $matches);
             if (isset($matches['date'])) {
                 $date = (new \DateTime($matches['date']))->format('Y-m-d');
                 $event->setDate($date);
                 $status = $route . ' arrival';
                 $event->setStatus($status);
                 $type = $matches['type'];
                 if ($route == 'Destination' && $type == 'A') {
                     $result->setAta($date);
                 }
                 if (in_array($type, ['E', 'P'])) {
                     if ($route == 'Destination') {
                         $result->setEta($date);
                     }
                     $result->addFutureEvent($event);
                 } else {
                     $result->addEvent($event);
                 }
             }
         }
         $item = $this->xpath->query("td[5]", $row);
         $departureDateStr = $this->getDomNodeValue($item);
         $departureDateStr = preg_replace('/([^a-zA-Z\\d ]+)/', ' ', $departureDateStr);
         if ($departureDateStr) {
             $matches = [];
             preg_match('/(?<date>\\d{1,2} \\w{3} \\d{2}) (?<type>\\w{1})/', $departureDateStr, $matches);
             if (isset($matches['date'])) {
                 $event = clone $event;
                 $type = $matches['type'];
                 $date = new \DateTime($matches['date']);
                 $event->setDate($date->format('Y-m-d'));
                 if ($route == 'Load Port' && $type == 'L') {
                     $result->setLoadOnVesselAt($date->format('Y-m-d'));
                 }
                 $status = $route . ' departure';
                 $event->setStatus($status);
                 if (in_array($type, ['E', 'P'])) {
                     $result->addFutureEvent($event);
                 } else {
                     $result->addEvent($event);
                 }
             }
         }
     }
     $containerData = $this->xpath->query('//h3[contains(text(), "Containers")]/following::table[1]/tbody/tr[2]');
     $item = $this->xpath->query('./td[2]', $containerData->item(0));
     $type = $this->getDomNodeValue($item);
     $result->setContainerType($type);
     $item = $this->xpath->query('./td[3]', $containerData->item(0));
     $size = $this->getDomNodeValue($item);
     $result->setContainerSize($size);
     return $result;
 }