src/Service/Internal/LineService.php line 196

Open in your IDE?
  1. <?php
  2. namespace App\Service\Internal;
  3. class LineService
  4. {
  5. const PATH_GET_LOCALITY_LINES = '/v3/networks/%1/localities/%2/lines';
  6. const PATH_GET_LINES = '/v3/networks/%1/lines';
  7. const PATH_GET_LINE = '/v3/networks/{1}/lines/{2}';
  8. const PATH_GET_LINE_DIRECTIONS = '/v3/networks/{1}/lines/{2}/directions';
  9. const PATH_GET_LINE_DIRECTION_TIMETABLES = '/v3/networks/{1}/lines/{2}/directions/{3}/timetables';
  10. const DEFAULT_SUGGESTIONS_LENGTH = null;
  11. /** @var string */
  12. protected $networkId;
  13. /** @var CoreApiService */
  14. protected $coreApiService;
  15. /** @var int */
  16. private $error;
  17. /**
  18. * PlaceService constructor.
  19. *
  20. * @param CoreApiService $coreApiService
  21. * @param string $networkId
  22. */
  23. public function __construct(
  24. string $networkId,
  25. CoreApiService $coreApiService
  26. ) {
  27. $this->networkId = $networkId;
  28. $this->coreApiService = $coreApiService;
  29. $this->error = 0;
  30. }
  31. /**
  32. * Get lines
  33. *
  34. * @param array $filters
  35. *
  36. * @return array
  37. * @throws \Exception
  38. */
  39. public function getLines(array $filters = [])
  40. {
  41. if (empty($filters['search'])) {
  42. return [];
  43. }
  44. if (!isset($filters['limit'])) {
  45. $filters['limit'] = self::DEFAULT_SUGGESTIONS_LENGTH;
  46. }
  47. if (isset($filters['lat']) && empty($filters['lat'])) {
  48. unset ($filters['lat']);
  49. }
  50. if (isset($filters['lon']) && empty($filters['lon'])) {
  51. unset ($filters['lon']);
  52. }
  53. $filters['context'] = 'PT_SCHEDULES_LINES_V1';
  54. $route = $this->coreApiService->getApiCoreUrl() . self::PATH_GET_LINES;
  55. $url = preg_replace(['/%1/'], [$this->networkId], $route);
  56. $response = $this->coreApiService->call(
  57. CoreApiService::CALLER_TYPE_PUBLIC,
  58. 'GET',
  59. $route,
  60. $url,
  61. ['Accept' => 'application/json', 'Content-Type' => 'application/json'],
  62. $filters
  63. );
  64. $this->error = $response->status;
  65. if ($response->status == 200 && isset($response->body->lines)) {
  66. return json_decode(json_encode($response->body), true);
  67. } else {
  68. return [];
  69. }
  70. }
  71. /**
  72. * Get all lines
  73. *
  74. * @param bool $displayDirections
  75. *
  76. * @return array
  77. * @throws \Exception
  78. */
  79. public function getAllLines(string $displayDirections = 'false', ?array $filters = [])
  80. {
  81. $filters['context'] = 'PT_SCHEDULES_LINES_V1';
  82. $filters['displayDirections'] = $displayDirections;
  83. $route = $this->coreApiService->getApiCoreUrl() . self::PATH_GET_LINES;
  84. $url = preg_replace(['/%1/'], [$this->networkId], $route);
  85. $response = $this->coreApiService->call(
  86. CoreApiService::CALLER_TYPE_PUBLIC,
  87. 'GET',
  88. $route,
  89. $url,
  90. ['Accept' => 'application/json', 'Content-Type' => 'application/json'],
  91. $filters
  92. );
  93. $this->error = $response->status;
  94. if ($response->status == 200 && isset($response->body->lines)) {
  95. return json_decode(json_encode($response->body), true);
  96. } else {
  97. return [];
  98. }
  99. }
  100. /**
  101. * Get Line
  102. *
  103. * @param int $lineId
  104. *
  105. * @return array
  106. *
  107. * @throws \Exception
  108. */
  109. public function getLine(string $lineId)
  110. {
  111. $lineId = urlencode($lineId);
  112. $route = $this->coreApiService->getApiCoreUrl() . self::PATH_GET_LINE;
  113. $url = preg_replace(['/\{1\}/', '/\{2\}/'], [$this->networkId, $lineId], $route);
  114. $response = $this->coreApiService->call(
  115. CoreApiService::CALLER_TYPE_PUBLIC,
  116. 'GET',
  117. $route,
  118. $url,
  119. ['Accept' => 'application/json', 'Content-Type' => 'application/json'],
  120. []
  121. );
  122. $this->error = $response->status;
  123. if ($response->status == 200 && isset($response->body->lines)) {
  124. return json_decode(json_encode($response->body->lines), true);
  125. } else {
  126. return [];
  127. }
  128. }
  129. /**
  130. * Get Line
  131. *
  132. * @param int $lineId
  133. *
  134. * @return array
  135. *
  136. * @throws \Exception
  137. */
  138. public function getLineWithDisruptions(string $lineId)
  139. {
  140. $lineId = urlencode($lineId);
  141. $route = $this->coreApiService->getApiCoreUrl() . self::PATH_GET_LINE;
  142. $url = preg_replace(['/\{1\}/', '/\{2\}/'], [$this->networkId, $lineId], $route);
  143. $response = $this->coreApiService->call(
  144. CoreApiService::CALLER_TYPE_PUBLIC,
  145. 'GET',
  146. $route,
  147. $url,
  148. ['Accept' => 'application/json', 'Content-Type' => 'application/json'],
  149. []
  150. );
  151. $this->error = $response->status;
  152. if ($response->status == 200 && isset($response->body->lines)) {
  153. return json_decode(json_encode($response->body), true);
  154. } else {
  155. return [];
  156. }
  157. }
  158. /**
  159. * @param int $lineId
  160. * @return array
  161. * @throws \Exception
  162. */
  163. public function getLineDirections(string $lineId)
  164. {
  165. $lineId = urlencode($lineId);
  166. $route = $this->coreApiService->getApiCoreUrl() . self::PATH_GET_LINE_DIRECTIONS;
  167. $url = preg_replace(['/\{1\}/', '/\{2\}/'], [$this->networkId, $lineId], $route);
  168. $response = $this->coreApiService->call(
  169. CoreApiService::CALLER_TYPE_PUBLIC,
  170. 'GET',
  171. $route,
  172. $url,
  173. ['Accept' => 'application/json', 'Content-Type' => 'application/json'],
  174. ['stopPoints' => true]
  175. );
  176. $this->error = $response->status;
  177. if ($response->status == 200 && isset($response->body->directions)) {
  178. return json_decode(json_encode($response->body->directions), true);
  179. } else {
  180. return [];
  181. }
  182. }
  183. /**
  184. * @param string $lineId
  185. * @param string $direction
  186. * @param string $date
  187. *
  188. * @return array
  189. *
  190. * @throws \Exception
  191. */
  192. public function getLineDirectionTimetable(string $lineId, string $direction, string $date = null)
  193. {
  194. $lineId = urlencode($lineId);
  195. $direction = strtoupper($direction);
  196. $route = $this->coreApiService->getApiCoreUrl() . self::PATH_GET_LINE_DIRECTION_TIMETABLES;
  197. $url = preg_replace(['/\{1\}/', '/\{2\}/', '/\{3\}/'], [$this->networkId, $lineId, $direction], $route);
  198. $response = $this->coreApiService->call(
  199. CoreApiService::CALLER_TYPE_PUBLIC,
  200. 'GET',
  201. $route,
  202. $url,
  203. ['Accept' => 'application/json', 'Content-Type' => 'application/json'],
  204. ["date" => $date]
  205. );
  206. $this->error = $response->status;
  207. if ($response->status == 200 && isset($response->body)) {
  208. return json_decode(json_encode($response->body), true);
  209. } else {
  210. return [];
  211. }
  212. }
  213. /**
  214. * @param int $localityId
  215. * @return array
  216. * @throws \Exception
  217. */
  218. public function getLinesByLocality(string $localityId)
  219. {
  220. $route = $this->coreApiService->getApiCoreUrl() . self::PATH_GET_LOCALITY_LINES;
  221. $url = preg_replace(['/%1/', '/%2/'], [$this->networkId, $localityId], $route);
  222. $response = $this->coreApiService->call(
  223. CoreApiService::CALLER_TYPE_PUBLIC,
  224. 'GET',
  225. $route,
  226. $url,
  227. ['Accept' => 'application/json', 'Content-Type' => 'application/json'],
  228. ['context' => 'PT_SCHEDULES_LINES_V1']
  229. );
  230. $this->error = $response->status;
  231. if ($response->status == 200 && isset($response->body->lines)) {
  232. return json_decode(json_encode($response->body), true);
  233. } else {
  234. return [];
  235. }
  236. }
  237. }