1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Afonso\Emt; |
4
|
|
|
|
5
|
|
|
use DateTime; |
6
|
|
|
use RuntimeException; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* The SDK client for the EMT OpenData API BUS service. |
10
|
|
|
* |
11
|
|
|
* @author Carlos Afonso Pérez <[email protected]> |
12
|
|
|
*/ |
13
|
|
|
class BusClient extends Client |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* Return the itinerary of one or more lines. |
17
|
|
|
* |
18
|
|
|
* @param int[] $lines |
19
|
|
|
* @param \DateTime $date |
20
|
|
|
* @return \stdClass |
21
|
|
|
* @throws \RuntimeException |
22
|
|
|
*/ |
23
|
3 |
|
public function getRouteLines(array $lines, DateTime $date) |
24
|
|
|
{ |
25
|
|
|
$params = [ |
26
|
3 |
|
'Lines' => join('|', $lines), |
27
|
3 |
|
'SelectDate' => $date->format('d/m/Y'), |
28
|
1 |
|
]; |
29
|
3 |
|
return $this->callBusService('GetRouteLines.php', $params); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Return calendar details for the given date interval. |
34
|
|
|
* |
35
|
|
|
* @param \DateTime $startDate |
36
|
|
|
* @param \DateTime $endDate |
37
|
|
|
* @return \stdClass |
38
|
|
|
* @throws \RuntimeException |
39
|
|
|
*/ |
40
|
3 |
|
public function getCalendar(DateTime $startDate, DateTime $endDate) |
41
|
|
|
{ |
42
|
|
|
$params = [ |
43
|
3 |
|
'SelectDateBegin' => $startDate->format('d/m/Y'), |
44
|
3 |
|
'SelectDateEnd' => $endDate->format('d/m/Y'), |
45
|
1 |
|
]; |
46
|
3 |
|
return $this->callBusService('GetCalendar.php', $params); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Return a list with line details. |
51
|
|
|
* |
52
|
|
|
* @param int[] $lines |
53
|
|
|
* @param \DateTime $date |
54
|
|
|
* @return \stdClass |
55
|
|
|
* @throws \RuntimeException |
56
|
|
|
*/ |
57
|
3 |
View Code Duplication |
public function getListLines(array $lines, DateTime $date) |
|
|
|
|
58
|
|
|
{ |
59
|
|
|
$params = [ |
60
|
3 |
|
'Lines' => join('|', $lines), |
61
|
3 |
|
'SelectDate' => $date->format('d/m/Y'), |
62
|
1 |
|
]; |
63
|
3 |
|
return $this->callBusService('GetListLines.php', $params); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Return details about all line groups. |
68
|
|
|
* |
69
|
|
|
* @return \stdClass |
70
|
|
|
* @throws \RuntimeException |
71
|
|
|
*/ |
72
|
3 |
|
public function getGroups() |
73
|
|
|
{ |
74
|
3 |
|
return $this->callBusService('GetGroups.php'); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Return start and end operation times of one or more lines. |
79
|
|
|
* |
80
|
|
|
* @param int[] $lines |
81
|
|
|
* @param \DateTime $date |
82
|
|
|
* @return \stdClass |
83
|
|
|
* @throws \RuntimeException |
84
|
|
|
*/ |
85
|
3 |
View Code Duplication |
public function getTimesLines(array $lines, DateTime $date) |
|
|
|
|
86
|
|
|
{ |
87
|
|
|
$params = [ |
88
|
3 |
|
'Lines' => join('|', $lines), |
89
|
3 |
|
'SelectDate' => $date->format('d/m/Y'), |
90
|
1 |
|
]; |
91
|
3 |
|
return $this->callBusService('GetTimesLines.php', $params); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Return timetable details of one or more lines. |
96
|
|
|
* |
97
|
|
|
* @param int[] $lines |
98
|
|
|
* @param \DateTime $date |
99
|
|
|
* @return \stdClass |
100
|
|
|
* @throws \RuntimeException |
101
|
|
|
*/ |
102
|
3 |
View Code Duplication |
public function getTimeTableLines(array $lines, DateTime $date) |
|
|
|
|
103
|
|
|
{ |
104
|
|
|
$params = [ |
105
|
3 |
|
'Lines' => join('|', $lines), |
106
|
3 |
|
'SelectDate' => $date->format('d/m/Y'), |
107
|
1 |
|
]; |
108
|
3 |
|
return $this->callBusService('GetTimeTableLines.php', $params); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Return details of one or more bus stops including name, served lines |
113
|
|
|
* and geographic coordinates. |
114
|
|
|
* |
115
|
|
|
* @param int[] $stopIds |
116
|
|
|
* @return \stdClass |
117
|
|
|
* @throws \RuntimeException |
118
|
|
|
*/ |
119
|
3 |
|
public function getNodesLines(array $stopIds) |
120
|
|
|
{ |
121
|
|
|
$params = [ |
122
|
3 |
|
'Nodes' => join('|', $stopIds), |
123
|
1 |
|
]; |
124
|
3 |
|
return $this->callBusService('GetNodesLines.php', $params); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Make an arbitrary call to the BUS service. |
129
|
|
|
* |
130
|
|
|
* @param string $endpoint |
131
|
|
|
* @param array $params |
132
|
|
|
* @return \stdClass |
133
|
|
|
*/ |
134
|
21 |
|
protected function callBusService($endpoint, array $params = []) |
135
|
|
|
{ |
136
|
21 |
|
$url = self::ENDPOINT . '/emt-proxy-server/last/bus/' . $endpoint; |
137
|
21 |
|
return $this->launcher->launchRequest($url, $params)->resultValues; |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.