1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sl; |
4
|
|
|
|
5
|
|
|
use Sl\Mappers\StationMapper; |
6
|
|
|
use Sl\Mappers\DepartureMapper; |
7
|
|
|
use Sl\Collections\StationCollection; |
8
|
|
|
use Sl\Collections\DepartureCollection; |
9
|
|
|
use Sl\Contracts\PublicTransportSystemInterface; |
10
|
|
|
use Sl\Contracts\Foundation\StationInterface; |
11
|
|
|
|
12
|
|
|
class Sl implements PublicTransportSystemInterface |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* Sl Http API base uri. |
16
|
|
|
*/ |
17
|
|
|
const BASE_URI = 'http://sl.se/api/'; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Search station API uri. |
21
|
|
|
*/ |
22
|
|
|
const SEARCH_STATION_URI = 'TypeAhead/Find/'; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Departures API uri. |
26
|
|
|
*/ |
27
|
|
|
const DEPARTURES_FROM_URI = 'sv/RealTime/GetDepartures/'; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Http client instance. |
31
|
|
|
* |
32
|
|
|
* @var \Sl\Contracts\HttpClientInterface |
33
|
|
|
*/ |
34
|
|
|
private $httpClient; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Get Http client instance. |
38
|
|
|
* |
39
|
|
|
* @return \Sl\Contracts\HttpClientInterface |
40
|
|
|
*/ |
41
|
2 |
|
public function httpClient() |
42
|
|
|
{ |
43
|
2 |
|
if ($this->httpClient === null) { |
44
|
2 |
|
$this->httpClient = new HttpClient(self::BASE_URI); |
45
|
2 |
|
} |
46
|
|
|
|
47
|
2 |
|
return $this->httpClient; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Search for station. |
52
|
|
|
* |
53
|
|
|
* @param string $query |
54
|
|
|
* |
55
|
|
|
* @return \Sl\Contracts\Collections\StationCollectionInterface |
56
|
|
|
*/ |
57
|
1 |
|
public function searchStation($query) |
58
|
|
|
{ |
59
|
1 |
|
$data = $this->httpClient()->getAndParseJson(self::SEARCH_STATION_URI.$query); |
60
|
|
|
|
61
|
1 |
|
return StationCollection::fromArray($data, new StationMapper()); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Get departures from station. |
66
|
|
|
* |
67
|
|
|
* @param \Sl\Contracts\Foundation\StationInterface $station |
68
|
|
|
* |
69
|
|
|
* @return \Sl\Contracts\Collections\DepartureCollectionInterface |
70
|
|
|
*/ |
71
|
1 |
|
public function departuresFrom(StationInterface $station) |
72
|
|
|
{ |
73
|
1 |
|
$data = $this->httpClient()->getAndParseJson(self::DEPARTURES_FROM_URI.$station->id()); |
74
|
|
|
|
75
|
1 |
|
return DepartureCollection::fromArray($data, new DepartureMapper()); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|