From 218dc9ed06d5a72a5daba1d8f4acdf9dd9433f62 Mon Sep 17 00:00:00 2001 From: visavi Date: Tue, 13 Jun 2023 00:39:06 +0300 Subject: [PATCH] =?UTF-8?q?=D0=92=D1=81=D0=B5=20=D0=BC=D0=B5=D1=82=D0=BE?= =?UTF-8?q?=D0=B4=D1=8B=20api=20=D1=82=D0=B5=D0=BF=D0=B5=D1=80=D1=8C=20?= =?UTF-8?q?=D0=B2=D0=BE=D0=B7=D1=80=D0=B0=D1=89=D0=B0=D1=8E=D1=82=20json?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- composer.json | 5 +- composer.lock | 173 ++++++++++++++++++++++++++++- examples/station_code.php | 2 +- examples/train_carriages.php | 4 +- examples/train_routes.php | 2 +- examples/train_routes_params.php | 4 +- examples/train_routes_return.php | 2 +- examples/train_routes_transfer.php | 2 +- examples/train_station_list.php | 2 +- src/Rzd/Api.php | 37 +++--- src/Rzd/Query.php | 9 +- 11 files changed, 207 insertions(+), 35 deletions(-) diff --git a/composer.json b/composer.json index e023b91..a53a582 100644 --- a/composer.json +++ b/composer.json @@ -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": { diff --git a/composer.lock b/composer.lock index 2d5c2cd..cc6d10d 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "c449abf764c115f9660683085d318bb2", + "content-hash": "1db3f919f0b009de708e4e42761bc0c6", "packages": [ { "name": "guzzlehttp/guzzle", @@ -2285,6 +2285,177 @@ ], "time": "2020-09-28T06:39:44+00:00" }, + { + "name": "symfony/polyfill-mbstring", + "version": "v1.27.0", + "source": { + "type": "git", + "url": "https://github.com/symfony/polyfill-mbstring.git", + "reference": "8ad114f6b39e2c98a8b0e3bd907732c207c2b534" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/polyfill-mbstring/zipball/8ad114f6b39e2c98a8b0e3bd907732c207c2b534", + "reference": "8ad114f6b39e2c98a8b0e3bd907732c207c2b534", + "shasum": "" + }, + "require": { + "php": ">=7.1" + }, + "provide": { + "ext-mbstring": "*" + }, + "suggest": { + "ext-mbstring": "For best performance" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-main": "1.27-dev" + }, + "thanks": { + "name": "symfony/polyfill", + "url": "https://github.com/symfony/polyfill" + } + }, + "autoload": { + "files": [ + "bootstrap.php" + ], + "psr-4": { + "Symfony\\Polyfill\\Mbstring\\": "" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Nicolas Grekas", + "email": "p@tchwork.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Symfony polyfill for the Mbstring extension", + "homepage": "https://symfony.com", + "keywords": [ + "compatibility", + "mbstring", + "polyfill", + "portable", + "shim" + ], + "support": { + "source": "https://github.com/symfony/polyfill-mbstring/tree/v1.27.0" + }, + "funding": [ + { + "url": "https://symfony.com/sponsor", + "type": "custom" + }, + { + "url": "https://github.com/fabpot", + "type": "github" + }, + { + "url": "https://tidelift.com/funding/github/packagist/symfony/symfony", + "type": "tidelift" + } + ], + "time": "2022-11-03T14:55:06+00:00" + }, + { + "name": "symfony/var-dumper", + "version": "v6.0.19", + "source": { + "type": "git", + "url": "https://github.com/symfony/var-dumper.git", + "reference": "eb980457fa6899840fe1687e8627a03a7d8a3d52" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/var-dumper/zipball/eb980457fa6899840fe1687e8627a03a7d8a3d52", + "reference": "eb980457fa6899840fe1687e8627a03a7d8a3d52", + "shasum": "" + }, + "require": { + "php": ">=8.0.2", + "symfony/polyfill-mbstring": "~1.0" + }, + "conflict": { + "phpunit/phpunit": "<5.4.3", + "symfony/console": "<5.4" + }, + "require-dev": { + "ext-iconv": "*", + "symfony/console": "^5.4|^6.0", + "symfony/process": "^5.4|^6.0", + "symfony/uid": "^5.4|^6.0", + "twig/twig": "^2.13|^3.0.4" + }, + "suggest": { + "ext-iconv": "To convert non-UTF-8 strings to UTF-8 (or symfony/polyfill-iconv in case ext-iconv cannot be used).", + "ext-intl": "To show region name in time zone dump", + "symfony/console": "To use the ServerDumpCommand and/or the bin/var-dump-server script" + }, + "bin": [ + "Resources/bin/var-dump-server" + ], + "type": "library", + "autoload": { + "files": [ + "Resources/functions/dump.php" + ], + "psr-4": { + "Symfony\\Component\\VarDumper\\": "" + }, + "exclude-from-classmap": [ + "/Tests/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Nicolas Grekas", + "email": "p@tchwork.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Provides mechanisms for walking through any arbitrary PHP variable", + "homepage": "https://symfony.com", + "keywords": [ + "debug", + "dump" + ], + "support": { + "source": "https://github.com/symfony/var-dumper/tree/v6.0.19" + }, + "funding": [ + { + "url": "https://symfony.com/sponsor", + "type": "custom" + }, + { + "url": "https://github.com/fabpot", + "type": "github" + }, + { + "url": "https://tidelift.com/funding/github/packagist/symfony/symfony", + "type": "tidelift" + } + ], + "time": "2023-01-20T17:44:14+00:00" + }, { "name": "theseer/tokenizer", "version": "1.2.1", diff --git a/examples/station_code.php b/examples/station_code.php index cdf55b4..1552908 100644 --- a/examples/station_code.php +++ b/examples/station_code.php @@ -11,7 +11,7 @@ $stations = $api->stationCode($params); if ($stations) { - var_dump($stations); + echo $stations; } else { echo 'Не найдено совпадений!'; } diff --git a/examples/train_carriages.php b/examples/train_carriages.php index aa66905..3cdf1c3 100644 --- a/examples/train_carriages.php +++ b/examples/train_carriages.php @@ -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 = [ @@ -27,7 +27,7 @@ 'tnum0' => $routes[0]->number, ]; - var_dump($api->trainCarriages($params)); + echo $api->trainCarriages($params); } else { echo 'Не удалось найти маршрут'; diff --git a/examples/train_routes.php b/examples/train_routes.php index ab319cd..0b2b7d1 100644 --- a/examples/train_routes.php +++ b/examples/train_routes.php @@ -16,4 +16,4 @@ $api = new Rzd\Api(); -var_dump($api->trainRoutes($params)); +echo $api->trainRoutes($params); diff --git a/examples/train_routes_params.php b/examples/train_routes_params.php index ec119e1..4c627de 100644 --- a/examples/train_routes_params.php +++ b/examples/train_routes_params.php @@ -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:password@192.168.16.1:10'); @@ -33,4 +33,4 @@ $api = new Rzd\Api($config); -var_dump($api->trainRoutes($params)); +echo $api->trainRoutes($params); diff --git a/examples/train_routes_return.php b/examples/train_routes_return.php index 07bcc76..f25bd18 100644 --- a/examples/train_routes_return.php +++ b/examples/train_routes_return.php @@ -18,4 +18,4 @@ $api = new Rzd\Api(); -var_dump($api->trainRoutesReturn($params)); +echo $api->trainRoutesReturn($params); diff --git a/examples/train_routes_transfer.php b/examples/train_routes_transfer.php index 01c7889..b493bff 100644 --- a/examples/train_routes_transfer.php +++ b/examples/train_routes_transfer.php @@ -17,4 +17,4 @@ $api = new Rzd\Api(); -var_dump($api->trainRoutes($params)); +echo $api->trainRoutes($params); diff --git a/examples/train_station_list.php b/examples/train_station_list.php index 6c7c094..0c8a8a4 100644 --- a/examples/train_station_list.php +++ b/examples/train_station_list.php @@ -11,4 +11,4 @@ 'depDate' => $date0->format('d.m.Y'), ]; -var_dump($api->trainStationList($params)); +echo $api->trainStationList($params); diff --git a/src/Rzd/Api.php b/src/Rzd/Api.php index c6553f9..1b898e2 100644 --- a/src/Rzd/Api.php +++ b/src/Rzd/Api.php @@ -8,7 +8,6 @@ class Api { public const ROUTES_LAYER = 5827; public const CARRIAGES_LAYER = 5764; - public const STATIONS_STRUCTURE_ID = 704; /** @@ -50,17 +49,17 @@ public function __construct(Config $config = null) * * @param array $params Массив параметров * - * @return array + * @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); } /** @@ -68,20 +67,20 @@ public function trainRoutes(array $params): array * * @param array $params Массив параметров * - * @return array + * @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); } /** @@ -89,22 +88,22 @@ public function trainRoutesReturn(array $params): array * * @param array $params Массив параметров * - * @return array + * @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); } /** @@ -112,20 +111,20 @@ public function trainCarriages(array $params): array * * @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); } /** @@ -133,10 +132,10 @@ public function trainStationList(array $params): array * * @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, @@ -156,6 +155,6 @@ public function stationCode(array $params): array } } - return $stations; + return json_encode($stations, JSON_UNESCAPED_UNICODE); } } diff --git a/src/Rzd/Query.php b/src/Rzd/Query.php index a66a94e..1ce026c 100644 --- a/src/Rzd/Query.php +++ b/src/Rzd/Query.php @@ -62,12 +62,13 @@ public function get(string $path, array $params = [], string $method = 'POST'): /** * Запрашивает данные * - * @param string $path Путь к странице - * @param array $params Массив параметров + * @param string $path Путь к странице + * @param array $params Массив параметров * @param string $method Тип запроса * * @return array|object * @throws GuzzleException + * @throws Exception */ protected function run(string $path, array $params, string $method): mixed { @@ -99,8 +100,8 @@ protected function run(string $path, array $params, string $method): mixed break; case 'OK': - if (isset($response->tp[0]->msgList[0]->message)) { - throw new RuntimeException($response->tp[0]->msgList[0]->message); + if (isset($content->tp[0]->msgList[0]->message)) { + throw new RuntimeException($content->tp[0]->msgList[0]->message); } break 2;