<?php
namespace App\Service\Internal;
class LineService
{
const PATH_GET_LOCALITY_LINES = '/v3/networks/%1/localities/%2/lines';
const PATH_GET_LINES = '/v3/networks/%1/lines';
const PATH_GET_LINE = '/v3/networks/{1}/lines/{2}';
const PATH_GET_LINE_DIRECTIONS = '/v3/networks/{1}/lines/{2}/directions';
const PATH_GET_LINE_DIRECTION_TIMETABLES = '/v3/networks/{1}/lines/{2}/directions/{3}/timetables';
const DEFAULT_SUGGESTIONS_LENGTH = null;
/** @var string */
protected $networkId;
/** @var CoreApiService */
protected $coreApiService;
/** @var int */
private $error;
/**
* PlaceService constructor.
*
* @param CoreApiService $coreApiService
* @param string $networkId
*/
public function __construct(
string $networkId,
CoreApiService $coreApiService
) {
$this->networkId = $networkId;
$this->coreApiService = $coreApiService;
$this->error = 0;
}
/**
* Get lines
*
* @param array $filters
*
* @return array
* @throws \Exception
*/
public function getLines(array $filters = [])
{
if (empty($filters['search'])) {
return [];
}
if (!isset($filters['limit'])) {
$filters['limit'] = self::DEFAULT_SUGGESTIONS_LENGTH;
}
if (isset($filters['lat']) && empty($filters['lat'])) {
unset ($filters['lat']);
}
if (isset($filters['lon']) && empty($filters['lon'])) {
unset ($filters['lon']);
}
$filters['context'] = 'PT_SCHEDULES_LINES_V1';
$route = $this->coreApiService->getApiCoreUrl() . self::PATH_GET_LINES;
$url = preg_replace(['/%1/'], [$this->networkId], $route);
$response = $this->coreApiService->call(
CoreApiService::CALLER_TYPE_PUBLIC,
'GET',
$route,
$url,
['Accept' => 'application/json', 'Content-Type' => 'application/json'],
$filters
);
$this->error = $response->status;
if ($response->status == 200 && isset($response->body->lines)) {
return json_decode(json_encode($response->body), true);
} else {
return [];
}
}
/**
* Get all lines
*
* @param bool $displayDirections
*
* @return array
* @throws \Exception
*/
public function getAllLines(string $displayDirections = 'false', ?array $filters = [])
{
$filters['context'] = 'PT_SCHEDULES_LINES_V1';
$filters['displayDirections'] = $displayDirections;
$route = $this->coreApiService->getApiCoreUrl() . self::PATH_GET_LINES;
$url = preg_replace(['/%1/'], [$this->networkId], $route);
$response = $this->coreApiService->call(
CoreApiService::CALLER_TYPE_PUBLIC,
'GET',
$route,
$url,
['Accept' => 'application/json', 'Content-Type' => 'application/json'],
$filters
);
$this->error = $response->status;
if ($response->status == 200 && isset($response->body->lines)) {
return json_decode(json_encode($response->body), true);
} else {
return [];
}
}
/**
* Get Line
*
* @param int $lineId
*
* @return array
*
* @throws \Exception
*/
public function getLine(string $lineId)
{
$lineId = urlencode($lineId);
$route = $this->coreApiService->getApiCoreUrl() . self::PATH_GET_LINE;
$url = preg_replace(['/\{1\}/', '/\{2\}/'], [$this->networkId, $lineId], $route);
$response = $this->coreApiService->call(
CoreApiService::CALLER_TYPE_PUBLIC,
'GET',
$route,
$url,
['Accept' => 'application/json', 'Content-Type' => 'application/json'],
[]
);
$this->error = $response->status;
if ($response->status == 200 && isset($response->body->lines)) {
return json_decode(json_encode($response->body->lines), true);
} else {
return [];
}
}
/**
* Get Line
*
* @param int $lineId
*
* @return array
*
* @throws \Exception
*/
public function getLineWithDisruptions(string $lineId)
{
$lineId = urlencode($lineId);
$route = $this->coreApiService->getApiCoreUrl() . self::PATH_GET_LINE;
$url = preg_replace(['/\{1\}/', '/\{2\}/'], [$this->networkId, $lineId], $route);
$response = $this->coreApiService->call(
CoreApiService::CALLER_TYPE_PUBLIC,
'GET',
$route,
$url,
['Accept' => 'application/json', 'Content-Type' => 'application/json'],
[]
);
$this->error = $response->status;
if ($response->status == 200 && isset($response->body->lines)) {
return json_decode(json_encode($response->body), true);
} else {
return [];
}
}
/**
* @param int $lineId
* @return array
* @throws \Exception
*/
public function getLineDirections(string $lineId)
{
$lineId = urlencode($lineId);
$route = $this->coreApiService->getApiCoreUrl() . self::PATH_GET_LINE_DIRECTIONS;
$url = preg_replace(['/\{1\}/', '/\{2\}/'], [$this->networkId, $lineId], $route);
$response = $this->coreApiService->call(
CoreApiService::CALLER_TYPE_PUBLIC,
'GET',
$route,
$url,
['Accept' => 'application/json', 'Content-Type' => 'application/json'],
['stopPoints' => true]
);
$this->error = $response->status;
if ($response->status == 200 && isset($response->body->directions)) {
return json_decode(json_encode($response->body->directions), true);
} else {
return [];
}
}
/**
* @param string $lineId
* @param string $direction
* @param string $date
*
* @return array
*
* @throws \Exception
*/
public function getLineDirectionTimetable(string $lineId, string $direction, string $date = null)
{
$lineId = urlencode($lineId);
$direction = strtoupper($direction);
$route = $this->coreApiService->getApiCoreUrl() . self::PATH_GET_LINE_DIRECTION_TIMETABLES;
$url = preg_replace(['/\{1\}/', '/\{2\}/', '/\{3\}/'], [$this->networkId, $lineId, $direction], $route);
$response = $this->coreApiService->call(
CoreApiService::CALLER_TYPE_PUBLIC,
'GET',
$route,
$url,
['Accept' => 'application/json', 'Content-Type' => 'application/json'],
["date" => $date]
);
$this->error = $response->status;
if ($response->status == 200 && isset($response->body)) {
return json_decode(json_encode($response->body), true);
} else {
return [];
}
}
/**
* @param int $localityId
* @return array
* @throws \Exception
*/
public function getLinesByLocality(string $localityId)
{
$route = $this->coreApiService->getApiCoreUrl() . self::PATH_GET_LOCALITY_LINES;
$url = preg_replace(['/%1/', '/%2/'], [$this->networkId, $localityId], $route);
$response = $this->coreApiService->call(
CoreApiService::CALLER_TYPE_PUBLIC,
'GET',
$route,
$url,
['Accept' => 'application/json', 'Content-Type' => 'application/json'],
['context' => 'PT_SCHEDULES_LINES_V1']
);
$this->error = $response->status;
if ($response->status == 200 && isset($response->body->lines)) {
return json_decode(json_encode($response->body), true);
} else {
return [];
}
}
}