-
Notifications
You must be signed in to change notification settings - Fork 1
/
ListTrains.php
65 lines (48 loc) · 1.84 KB
/
ListTrains.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<?php
require_once "ApiRequest.php";
require_once "Train.php";
require_once "ApiToken.php";
class ListTrains{
private $trains = [];
function __construct($stationId, $date, $hour){
$atr = "@attributes";
$url = "https://api.deutschebahn.com/timetables/v1/plan/" . $stationId . "/" . $date . "/" . $hour;
$apiToken = new ApiToken();
$api = new ApiRequest($url, $apiToken->getApiToken());
$api->request();
$response = $api->getResponse();
if($api->getStatusCode() == 200){
$response = json_decode($response, true);
foreach($response["s"] as $item){
$departure = "Endstation";
if(isset($item["dp"])){
$departure = $item["dp"][$atr]["pt"];
$departure = substr($departure, 6, strlen($departure));
$departure = substr($departure, 0, 2) . ":" . substr($departure, 2, 4);
}
$destination = null;
if (isset($item['dp'][$atr]['ppth'])) {
$destinationPath = $item['dp'][$atr]['ppth'];
$destination = explode('|', $destinationPath);
$destination = $destination[count($destination) - 1];
}
$platform = null;
if (isset($item['dp'][$atr]['pp'])) {
$platform = $item['dp'][$atr]['pp'];
}
array_push(
$this->trains,
new Train($departure, $item["tl"][$atr]["c"], $item["tl"][$atr]["n"], $item[$atr]["id"], $destination, $platform)
);
}
}
}
/**
* Returns all trains that holds on a station in a hour.
* @return Train[]
*/
public function getTrains(): array
{
return $this->trains;
}
}