Skip to content

Commit

Permalink
Add transport type parsing and throw an exception on invalid results
Browse files Browse the repository at this point in the history
  • Loading branch information
Bertware committed Jun 18, 2019
1 parent c56c077 commit c2e244c
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 1 deletion.
55 changes: 54 additions & 1 deletion src/Trafiklab/ResRobot/Model/ResRobotTimeTableEntry.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
use DateTime;
use Trafiklab\Common\Model\Contract\TimeTableEntry;
use Trafiklab\Common\Model\Enum\TimeTableType;
use Trafiklab\Common\Model\Enum\TransportType;
use Trafiklab\Common\Model\Exceptions\TrafiklabSdkException;

/**
* An entry in a timetable, describing a single departure or arrival of a vehicle at a stoplocation.
Expand All @@ -35,6 +37,7 @@ class ResRobotTimeTableEntry implements TimeTableEntry
* @param int $type
*
* @internal
* @throws TrafiklabSdkException
*/
public function __construct(array $json, int $type)
{
Expand Down Expand Up @@ -153,9 +156,16 @@ public function getTransportType(): string
* Parse (a part of) an API response and store the result in this object.
*
* @param array $json
*
* @throws TrafiklabSdkException
*/
private function parseApiResponse(array $json): void
{
if (!key_exists('stopExtId', $json)) {
throw new TrafiklabSdkException("The server responded with an invalid JSON response.
stopExtId is missing.", 500);
}

$this->_stopId = $json['stopExtId'];
$this->_stopName = $json['stop'];
$this->_lineName = $json['name'];
Expand All @@ -171,7 +181,50 @@ private function parseApiResponse(array $json): void
$json['date'] . ' ' . $json['time']);

$this->_operator = $json['Product']['operator'];
$this->parseTransportType($json);

// Todo: parse transport type;
}

/**
* Parse the transport type for this entry.
*
* @param array $json
*/
private function parseTransportType(array $json): void
{
/*
* Possible transport categories:
* 'BLT': Local bus
* 'BAX': Airport bus
* 'BXB': Express bus
* 'BRE': Regional bus
* 'SLT': Local tram
* 'JLT': Local train
* 'JRE': Regional train
* 'JAX': Airport train
* 'JIC': IC train
* 'JBL':
* 'JNT': Night train
* 'ULT': Local metro
* 'FLT': Local ferry
*/

switch (substr($json['transportCategory'], 0, 1)) {
case 'B':
$this->_transportType = TransportType::BUS;
break;
case 'S':
$this->_transportType = TransportType::TRAM;
break;
case 'J':
$this->_transportType = TransportType::TRAIN;
break;
case 'U':
$this->_transportType = TransportType::METRO;
break;
case 'F':
$this->_transportType = TransportType::SHIP;
break;
}
}
}
8 changes: 8 additions & 0 deletions tests/Trafiklab/Resrobot/Model/ResRobotTimeTableEntryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,13 @@

use PHPUnit_Framework_TestCase;
use Trafiklab\Common\Model\Enum\TimeTableType;
use Trafiklab\Common\Model\Enum\TransportType;

class ResRobotTimeTableEntryTest extends PHPUnit_Framework_TestCase
{
/**
* @throws \Trafiklab\Common\Model\Exceptions\TrafiklabSdkException
*/
function testConstructor_validDepartureBoardEntryJson_shouldReturnCorrectObjectRepresentation()
{
$validDepartures = json_decode(
Expand All @@ -27,8 +31,12 @@ function testConstructor_validDepartureBoardEntryJson_shouldReturnCorrectObjectR
$departureBoardEntry->getScheduledStopTime());
self::assertEquals("Alby T-bana (Botkyrka kn)", $departureBoardEntry->getDirection());
self::assertEquals(TimeTableType::DEPARTURES, $departureBoardEntry->getTimeTableType());
self::assertEquals(TransportType::METRO, $departureBoardEntry->getTransportType());
}

/**
* @throws \Trafiklab\Common\Model\Exceptions\TrafiklabSdkException
*/
function testConstructor_validArrivalBoardEntryJson_shouldReturnCorrectObjectRepresentation()
{
$validDepartures = json_decode(
Expand Down

0 comments on commit c2e244c

Please sign in to comment.