Skip to content

Commit

Permalink
Use version 1.0.x as SDK base, better tests and documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
Bertware committed Jun 12, 2019
1 parent d3ffb9d commit fd28042
Show file tree
Hide file tree
Showing 12 changed files with 1,109 additions and 935 deletions.
64 changes: 59 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,28 +66,82 @@ If you don't want to send a user agent, you can just leave out this line.
Detailed information about SL request parameters can be found at [the Trafiklab website](https://www.trafiklab.se/api/).
Only the most important/most used request parameters are implemented in the SDK, in order to reduce clutter, and to ensure that we can keep the SDK unchanged in case of changes to the API.
If you believe we have missed an important field, please create an issue so we can review this.

##### Response

In order to use the data returned by your request, you can simply call getTimeTable() on the response object.
This method returns an array of TimeTableEntry instances, each of which describes one departure or arrival.
You can look at the code and PHPDoc in order to get up-to-date information on which fields are available.
Detailed information about SL responses can be found at [the Trafiklab website](https://www.trafiklab.se/api/).
Detailed information about ResRobot responses can be found at [the Trafiklab website](https://www.trafiklab.se/api/).

The following code gives a quick idea on how the SDK is used.
```
$response->getTimetable()
$entry = $response->getTimetable()[0]; // Get the first result
// Type of transport, one of the constants in Trafiklab\Common\Model\Enum\TransportType
$entry->getTransportType();
// The name of the stop location
$stop = $timeTableEntry->getStopName()
// The number of the line
$lineNumber = $timeTableEntry->getLineNumber();
// The direction of the vehicle
$direction = $timeTableEntry->getDirection();
// The scheduled departure time at the stop
$scheduledStopTime = $timeTableEntry->getScheduledStopTime();
```

#### Routeplanning
##### Request
The following code example illustrates how you can plan a route from A to B

```
// TODO: add example
$queryTime = new DateTime();
$queryTime->setTime(18, 0);
$wrapper = new SlWrapper();
// Create a new routeplanning object. The wrapper will instantiate an object of the interface type.
$wrapper = $wrapper->createRoutePlanningRequestObject();
$wrapper->setOriginId("740000001");
$wrapper->setDestinationId("740000002");
$wrapper->setDateTime($queryTime);
$wrapper->setUserAgent(("<YOUR_USER_AGENT>");
$wrapper->setRoutePlanningApiKey("<YOUR_API_KEY>");
$response = $resRobotWrapper->getRoutePlanning($routePlanningRequest);
```
##### Response

In order to use the data returned by your request, you can simply call getTrips() on the response object.
This method returns an array of Trip instances, each of which describes one departure or arrival.
You can look at the code and PHPDoc in order to get up-to-date information on which fields are available.
Detailed information about ResRobot responses can be found at the [ResRobot departures/arrivals API page](https://www.trafiklab.se/api/resrobot-reseplanerare).

The following code gives a quick idea on how the SDK is used.

```
// TODO: add example
$trip = $response->getTrips()[0]; // Get the first result
// Tell the user about every leg in their journey.
foreach ($trip->getLegs() as $leg) {
// There are two types of legs (at this moment): Vehicle journeys, where a vehicle is used, or walking parts
// where a user walks between two stations. Not all fields are available for walking parts, so we need to handle them differently.
if ($leg->getType() == RoutePlanningLegType::VEHICLE_JOURNEY) {
$leg->getVehicle()->getType();
$leg->getVehicle()->getNumber();
$leg->getDirection();
$leg->getDeparture()->getStopName();
$leg->getDeparture()->getScheduledDepartureTime()->format("H:i");
$leg->getDeparture()->getScheduledDepartureTime()->getTimestamp();
$leg->getArrival()->getScheduledArrivalTime()->getTimestamp();
$leg->getArrival()->getStopName();
// More fields are available
} else if ($leg->getType() == RoutePlanningLegType::WALKING) {
// Limited fields when walking!
$leg->getDeparture()->getStopName(); // origin
$leg->getArrival()->getStopName(); // destination
}
}
```

## Contributing
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"require": {
"php": ">=7.1",
"ext-json": "*",
"trafiklab/php-sdk-commons": "0.4.0"
"trafiklab/php-sdk-commons": "1.0.*"
},
"require-dev": {
"phpunit/phpunit": "^5.5",
Expand Down
2 changes: 1 addition & 1 deletion src/Trafiklab/Sl/Internal/SlClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public function getRoutePlanning(string $key, RoutePlanningRequest $request): Sl
"destExtId" => $request->getDestinationStopId(),
"date" => $request->getDateTime()->format("Y-m-d"),
"time" => $request->getDateTime()->format("H:i"),
"lang" => $request->getLang(),
"lang" => $request->getLanguage(),
"searchForArrival" => $searchForArrival,
"passlist" => "1",
];
Expand Down
19 changes: 17 additions & 2 deletions src/Trafiklab/Sl/Model/SlLeg.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function __construct(array $json)
*
* @return VehicleStopWithRealtime The stoplocation at which this leg starts.
*/
public function getOrigin(): VehicleStop
public function getDeparture(): VehicleStop
{
return $this->_origin;
}
Expand All @@ -54,11 +54,26 @@ public function getOrigin(): VehicleStop
*
* @return VehicleStopWithRealtime The stoplocation at which this leg ends.
*/
public function getDestination(): VehicleStop
public function getArrival(): VehicleStop
{
return $this->_destination;
}

/**
* Get the duration of this leg in seconds.
*
* @return int
*/
public function getDuration(): int
{
if ($this->getArrival()->getScheduledArrivalTime() == null
|| $this->getDeparture()->getScheduledDepartureTime() == null) {
return 0;
}
return $this->getArrival()->getScheduledArrivalTime()->getTimestamp() -
$this->getDeparture()->getScheduledDepartureTime()->getTimestamp();
}

/**
* Remarks about this leg, for example describing facilities on board of a train, or possible disturbances on the
* route.
Expand Down
4 changes: 2 additions & 2 deletions src/Trafiklab/Sl/Model/SlRoutePlanningRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,15 @@ public function setViaStopId(?string $viaId): void
/**
* @return string
*/
public function getLang(): string
public function getLanguage(): string
{
return $this->_lang;
}

/**
* @param string $lang
*/
public function setLang(string $lang): void
public function setLanguage(string $lang): void
{
$this->_lang = $lang;
}
Expand Down
38 changes: 38 additions & 0 deletions src/Trafiklab/Sl/Model/SlTrip.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

use Trafiklab\Common\Model\Contract\RoutePlanningLeg;
use Trafiklab\Common\Model\Contract\Trip;
use Trafiklab\Common\Model\Contract\VehicleStop;

/**
* A Trip, often also called Journey, describes one possibility for travelling between two locations. A Trip can
Expand Down Expand Up @@ -41,6 +42,43 @@ public function getLegs(): array
return $this->_legs;
}

/**
* Get the duration of this trip in seconds.
*
* @return int
*/
public function getDuration(): int
{
return $this->getArrival()->getScheduledArrivalTime()->getTimestamp() -
$this->getDeparture()->getScheduledDepartureTime()->getTimestamp();
}

/**
* Get the departure for the first leg.
*
* @return VehicleStop
*/
public function getDeparture(): VehicleStop
{
if (count($this->_legs) < 1) {
return null;
}
return $this->_legs[0]->getDeparture();
}

/**
* Get the arrival for the last leg.
*
* @return VehicleStop
*/
public function getArrival(): VehicleStop
{
if (count($this->_legs) < 1) {
return null;
}
return end($this->_legs)->getArrival();
}

private function parseApiResponse(array $json)
{
$this->_legs = [];
Expand Down
Loading

0 comments on commit fd28042

Please sign in to comment.