/** * Get the shipment status history. * * @param DOMXPath $xpath * * @return Track * @throws \Exception */ protected function getTrack(DOMXPath $xpath) { $rows = $xpath->query("//table[@class='dataTable']//tr"); if (!$rows) { throw new \Exception("Unable to parse UPS tracking data for [{$this->parcelNumber}]."); } $track = new Track(); $lastLocation = ''; foreach ($rows as $index => $row) { if ($index == 0) { continue; // skip the heading row } $eventData = $this->parseRow($row, $xpath); if (!empty($eventData['location'])) { $lastLocation = $eventData['location']; } else { $eventData['location'] = $lastLocation; } $event = Event::fromArray($eventData); $track->addEvent($event); if (array_key_exists('recipient', $eventData)) { $track->setRecipient($eventData['recipient']); } } return $track->sortEvents(); }
/** * Get the shipment status history. * * @param DOMXPath $xpath * * @return Track * @throws \Exception */ protected function getTrack(DOMXPath $xpath) { $rows = $xpath->query("//table[@id='tc-hits']//tbody//tr[contains(@class,'detail-wrapper')]"); if (!$rows) { throw new \Exception("Unable to parse USPS tracking data for [{$this->parcelNumber}]."); } $track = new Track(); $lastLocation = ''; $lastDate = ''; foreach ($rows as $row) { $eventData = $this->parseRow($row); if (!empty($eventData['location'])) { $lastLocation = $eventData['location']; } else { $eventData['location'] = $lastLocation; } if (!empty($eventData['date'])) { $lastDate = $eventData['date']; } else { $eventData['date'] = $lastDate; } $track->addEvent(Event::fromArray($eventData)); } return $track->sortEvents(); }
/** @test */ public function it_can_build_an_event_from_array() { $data = ['location' => 'some location', 'date' => Carbon::parse('2016-01-31'), 'description' => 'some description', 'status' => 'delivered']; $event = Event::fromArray($data); $this->assertInstanceOf(Event::class, $event); $this->assertSame('some location', $event->getLocation()); $this->assertSame('2016-01-31', $event->getDate()->toDateString()); $this->assertSame('some description', $event->getDescription()); $this->assertSame('delivered', $event->getStatus()); }
/** * @param int $count * * @return Event|Event[] */ protected function getEvents($count = 1) { $eventData = ['location' => 'some location', 'date' => '2016-01-31', 'description' => 'some description', 'status' => 'delivered']; if ($count == 1) { return Event::fromArray($eventData); } $events = []; for ($x = 1; $x <= $count; $x++) { $events[] = Event::fromArray($eventData); } return $events; }
/** * Get the shipment status history. * * @param DOMXPath $xpath * * @return Track * @throws \Exception */ protected function getTrack(DOMXPath $xpath) { $rows = $xpath->query("//div[@id='pieceEvents0']/div/div/div/table/tbody/tr"); if (!$rows) { throw new \Exception("Unable to parse DHL tracking data for [{$this->parcelNumber}]."); } $track = new Track(); foreach ($rows as $row) { $eventData = $this->parseEvent($row, $xpath); $track->addEvent(Event::fromArray($eventData)); if (array_key_exists('recipient', $eventData)) { $track->setRecipient($eventData['recipient']); } } return $track->sortEvents(); }
/** * Get the shipment status history. * * @param DOMXPath $xpath * * @return Track * @throws \Exception */ protected function getTrack(DOMXPath $xpath) { $rows = $xpath->query("//table[@class='events_view fullview_tabledata']//tbody//tr"); if (!$rows) { throw new \Exception("Unable to parse Swiss Post tracking data for [{$this->parcelNumber}]."); } $track = new Track(); $lastLocation = ''; foreach ($rows as $row) { $eventData = $this->parseRow($row); if (!empty($eventData['location'])) { $lastLocation = $eventData['location']; } else { $eventData['location'] = $lastLocation; } $event = Event::fromArray($eventData); $track->addEvent($event); } return $track->sortEvents(); }
/** * Get the shipment status history. * * @param array $response * * @return Track */ protected function getTrack(array $response) { $track = new Track(); foreach ($response['tuStatus'][0]['history'] as $index => $historyItem) { $event = new Event(); $status = $this->resolveStatus($response, $index); $event->setStatus($status); $event->setLocation($this->getLocation($historyItem)); $event->setDescription($historyItem['evtDscr']); $event->setDate($this->getDate($historyItem)); $event->addAdditionalDetails('eventNumber', $response['tuStatus'][0]['progressBar']['evtNos'][$index]); $track->addEvent($event); if ($status == Track::STATUS_DELIVERED) { $track->setRecipient($this->getRecipient($response)); } if ($status == Track::STATUS_PICKUP) { $track->addAdditionalDetails('parcelShop', $this->getParcelShopDetails($response)); } } return $track->sortEvents(); }