Skip to content

Commit

Permalink
Все методы api теперь возращают json
Browse files Browse the repository at this point in the history
  • Loading branch information
visavi committed Jun 12, 2023
1 parent 4f145db commit 218dc9e
Show file tree
Hide file tree
Showing 11 changed files with 207 additions and 35 deletions.
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@
"ext-json": "*",
"ext-curl": "*",
"ext-mbstring": "*",
"guzzlehttp/guzzle": "^7.4"
"guzzlehttp/guzzle": "^7.7"
},
"require-dev": {
"phpunit/phpunit": "^9.5"
"phpunit/phpunit": "^9.5",
"symfony/var-dumper": "^6.0"
},
"autoload": {
"psr-4": {
Expand Down
173 changes: 172 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion examples/station_code.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
$stations = $api->stationCode($params);

if ($stations) {
var_dump($stations);
echo $stations;
} else {
echo 'Не найдено совпадений!';
}
4 changes: 2 additions & 2 deletions examples/train_carriages.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
'code1' => '2000000',
'dt0' => $date0->format('d.m.Y'),
];
$routes = $api->trainRoutes($params);
$routes = json_decode($api->trainRoutes($params));

if ($routes) {
$params = [
Expand All @@ -27,7 +27,7 @@
'tnum0' => $routes[0]->number,
];

var_dump($api->trainCarriages($params));
echo $api->trainCarriages($params);

} else {
echo 'Не удалось найти маршрут';
Expand Down
2 changes: 1 addition & 1 deletion examples/train_routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@

$api = new Rzd\Api();

var_dump($api->trainRoutes($params));
echo $api->trainRoutes($params);
4 changes: 2 additions & 2 deletions examples/train_routes_params.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
$config->setReferer('https://ticket.rzd.ru/');

// Enable debug mode
$config->setDebugMode(true);
//$config->setDebugMode(true);

// Enable proxy
//$config->setProxy('https://username:[email protected]:10');
Expand All @@ -33,4 +33,4 @@

$api = new Rzd\Api($config);

var_dump($api->trainRoutes($params));
echo $api->trainRoutes($params);
2 changes: 1 addition & 1 deletion examples/train_routes_return.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@

$api = new Rzd\Api();

var_dump($api->trainRoutesReturn($params));
echo $api->trainRoutesReturn($params);
2 changes: 1 addition & 1 deletion examples/train_routes_transfer.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@

$api = new Rzd\Api();

var_dump($api->trainRoutes($params));
echo $api->trainRoutes($params);
2 changes: 1 addition & 1 deletion examples/train_station_list.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@
'depDate' => $date0->format('d.m.Y'),
];

var_dump($api->trainStationList($params));
echo $api->trainStationList($params);
37 changes: 18 additions & 19 deletions src/Rzd/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ class Api
{
public const ROUTES_LAYER = 5827;
public const CARRIAGES_LAYER = 5764;

public const STATIONS_STRUCTURE_ID = 704;

/**
Expand Down Expand Up @@ -50,93 +49,93 @@ public function __construct(Config $config = null)
*
* @param array $params Массив параметров
*
* @return array<object>
* @return string
* @throws GuzzleException
*/
public function trainRoutes(array $params): array
public function trainRoutes(array $params): string
{
$layer = [
'layer_id' => static::ROUTES_LAYER,
];
$routes = $this->query->get($this->path, $layer + $params);

return $routes->tp[0]->list;
return json_encode($routes->tp[0]->list, JSON_UNESCAPED_UNICODE);
}

/**
* Получает маршруты туда-обратно
*
* @param array $params Массив параметров
*
* @return array<object>
* @return string
* @throws GuzzleException
*/
public function trainRoutesReturn(array $params): array
public function trainRoutesReturn(array $params): string
{
$layer = [
'layer_id' => static::ROUTES_LAYER,
];
$routes = $this->query->get($this->path, $layer + $params);

return [
return json_encode([
'forward' => $routes->tp[0]->list,
'back' => $routes->tp[1]->list
];
], JSON_UNESCAPED_UNICODE);
}

/**
* Получение списка вагонов
*
* @param array $params Массив параметров
*
* @return array<object>
* @return string
* @throws GuzzleException
*/
public function trainCarriages(array $params): array
public function trainCarriages(array $params): string
{
$layer = [
'layer_id' => static::CARRIAGES_LAYER,
];
$carriages = $this->query->get($this->path, $layer + $params);

return [
return json_encode([
'cars' => $carriages->lst[0]->cars ?? null,
'functionBlocks' => $carriages->lst[0]->functionBlocks ?? null,
'schemes' => $carriages->schemes ?? null,
'companies' => $carriages->insuranceCompany ?? null,
];
], JSON_UNESCAPED_UNICODE);
}

/**
* Получение списка станций
*
* @param array $params Массив параметров
*
* @return array
* @return string
* @throws GuzzleException
*/
public function trainStationList(array $params): array
public function trainStationList(array $params): string
{
$layer = [
'STRUCTURE_ID' => static::STATIONS_STRUCTURE_ID,
];
$stations = $this->query->get($this->stationListPath, $layer + $params);

return [
return json_encode([
'train' => $stations->data->trainInfo,
'routes' => $stations->data->routes,
];
], JSON_UNESCAPED_UNICODE);
}

/**
* Получение списка кодов станций
*
* @param array $params Массив параметров
*
* @return array
* @return string
* @throws GuzzleException
*/
public function stationCode(array $params): array
public function stationCode(array $params): string
{
$lang = [
'lang' => $this->lang,
Expand All @@ -156,6 +155,6 @@ public function stationCode(array $params): array
}
}

return $stations;
return json_encode($stations, JSON_UNESCAPED_UNICODE);
}
}
Loading

0 comments on commit 218dc9e

Please sign in to comment.