From 461f860aaf257240ccf74badfe85f92ac8cf60fe Mon Sep 17 00:00:00 2001 From: her-cat Date: Fri, 4 Oct 2019 23:55:35 +0800 Subject: [PATCH 1/6] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=89=B9=E9=87=8F?= =?UTF-8?q?=E7=AE=97=E8=B7=AF=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/WebApi/Application.php | 2 + src/WebApi/RouteMatrix/Client.php | 68 ++++++++++++++++++++++ src/WebApi/RouteMatrix/ServiceProvider.php | 24 ++++++++ 3 files changed, 94 insertions(+) create mode 100644 src/WebApi/RouteMatrix/Client.php create mode 100644 src/WebApi/RouteMatrix/ServiceProvider.php diff --git a/src/WebApi/Application.php b/src/WebApi/Application.php index a530ba5..9e10065 100644 --- a/src/WebApi/Application.php +++ b/src/WebApi/Application.php @@ -35,6 +35,7 @@ * @property Direction\LiteClient $direction_lite * @property Direction\AbroadClient $direction_abroad * @property BatchRequest\Client $batch_request + * @property RouteMatrix\Client $route_matrix */ class Application extends ServiceContainer { @@ -51,5 +52,6 @@ class Application extends ServiceContainer CoordsConvert\ServiceProvider::class, Direction\ServiceProvider::class, BatchRequest\ServiceProvider::class, + RouteMatrix\ServiceProvider::class, ]; } diff --git a/src/WebApi/RouteMatrix/Client.php b/src/WebApi/RouteMatrix/Client.php new file mode 100644 index 0000000..c45069d --- /dev/null +++ b/src/WebApi/RouteMatrix/Client.php @@ -0,0 +1,68 @@ + + * + * @method driving($origins, $destinations, array $options = []) + * @method riding($origins, $destinations, array $options = []) + * @method walking($origins, $destinations, array $options = []) + */ +class Client extends BaseClient +{ + /** + * @param string $name + * @param array $arguments + * + * @return array|Response|Collection|mixed|object|ResponseInterface + * + * @throws GuzzleException + * @throws InvalidConfigException + * @throws InvalidArgumentException + */ + public function __call($name, $arguments) + { + if (!in_array($name, ['driving', 'riding', 'walking'])) { + throw new InvalidArgumentException('Invalid method "%s".', $name); + } + + if (count($arguments) < 2) { + throw new InvalidArgumentException('Invalid arguments'); + } + + $options = isset($arguments[2]) ? $arguments[2] : []; + + $options = array_merge([ + 'origins' => $this->processCoordinate($arguments[0]), + 'destinations' => $this->processCoordinate($arguments[1]), + ], $options); + + return $this->httpGet(sprintf('routematrix/v2/%s', $name), $options); + } + + protected function processCoordinate($coordinate) + { + if (is_object($coordinate)) { + $coordinate = (array) $coordinate; + } else if (!is_array($coordinate)) { + return $coordinate; + } + + $coordinate = array_map(function ($value) { + return is_array($value) ? implode(',', $value) : $value; + }, $coordinate); + + return implode('|', $coordinate); + } +} diff --git a/src/WebApi/RouteMatrix/ServiceProvider.php b/src/WebApi/RouteMatrix/ServiceProvider.php new file mode 100644 index 0000000..be67a38 --- /dev/null +++ b/src/WebApi/RouteMatrix/ServiceProvider.php @@ -0,0 +1,24 @@ + + */ +class ServiceProvider implements ServiceProviderInterface +{ + /** + * {@inheritdoc} + */ + public function register(Container $app) + { + $app['route_matrix'] = function ($app) { + return new Client($app); + }; + } +} From 38e2d71b72d93148481a340f94b0067b05a35a80 Mon Sep 17 00:00:00 2001 From: her-cat Date: Sat, 5 Oct 2019 00:27:25 +0800 Subject: [PATCH 2/6] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=89=B9=E9=87=8F?= =?UTF-8?q?=E7=AE=97=E8=B7=AF=E5=8D=95=E5=85=83=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/WebApi/RouteMatrix/Client.php | 66 +++++++++++++++++------- tests/WebApi/RouteMatrix/ClientTest.php | 68 +++++++++++++++++++++++++ 2 files changed, 116 insertions(+), 18 deletions(-) create mode 100644 tests/WebApi/RouteMatrix/ClientTest.php diff --git a/src/WebApi/RouteMatrix/Client.php b/src/WebApi/RouteMatrix/Client.php index c45069d..3de23ff 100644 --- a/src/WebApi/RouteMatrix/Client.php +++ b/src/WebApi/RouteMatrix/Client.php @@ -14,43 +14,73 @@ * Class Client. * * @author her-cat - * - * @method driving($origins, $destinations, array $options = []) - * @method riding($origins, $destinations, array $options = []) - * @method walking($origins, $destinations, array $options = []) */ class Client extends BaseClient { /** - * @param string $name - * @param array $arguments + * @param string|array $origins + * @param string|array $destinations + * @param array $options * * @return array|Response|Collection|mixed|object|ResponseInterface * * @throws GuzzleException * @throws InvalidConfigException - * @throws InvalidArgumentException */ - public function __call($name, $arguments) + public function driving($origins, $destinations, array $options = []) { - if (!in_array($name, ['driving', 'riding', 'walking'])) { - throw new InvalidArgumentException('Invalid method "%s".', $name); - } + $options = array_merge([ + 'origins' => $this->processCoordinate($origins), + 'destinations' => $this->processCoordinate($destinations), + ], $options); - if (count($arguments) < 2) { - throw new InvalidArgumentException('Invalid arguments'); - } + return $this->httpGet('routematrix/v2/driving', $options); + } + + /** + * @param string|array $origins + * @param string|array $destinations + * @param array $options + * + * @return array|Response|Collection|mixed|object|ResponseInterface + * + * @throws GuzzleException + * @throws InvalidConfigException + */ + public function riding($origins, $destinations, array $options = []) + { + $options = array_merge([ + 'origins' => $this->processCoordinate($origins), + 'destinations' => $this->processCoordinate($destinations), + ], $options); - $options = isset($arguments[2]) ? $arguments[2] : []; + return $this->httpGet('routematrix/v2/riding', $options); + } + /** + * @param string|array $origins + * @param string|array $destinations + * @param array $options + * + * @return array|Response|Collection|mixed|object|ResponseInterface + * + * @throws GuzzleException + * @throws InvalidConfigException + */ + public function walking($origins, $destinations, array $options = []) + { $options = array_merge([ - 'origins' => $this->processCoordinate($arguments[0]), - 'destinations' => $this->processCoordinate($arguments[1]), + 'origins' => $this->processCoordinate($origins), + 'destinations' => $this->processCoordinate($destinations), ], $options); - return $this->httpGet(sprintf('routematrix/v2/%s', $name), $options); + return $this->httpGet('routematrix/v2/walking', $options); } + /** + * @param string|array $coordinate + * @return string + */ protected function processCoordinate($coordinate) { if (is_object($coordinate)) { diff --git a/tests/WebApi/RouteMatrix/ClientTest.php b/tests/WebApi/RouteMatrix/ClientTest.php new file mode 100644 index 0000000..de88cab --- /dev/null +++ b/tests/WebApi/RouteMatrix/ClientTest.php @@ -0,0 +1,68 @@ +mockApiClient(Client::class); + + $client->expects()->httpGet('routematrix/v2/driving', [ + 'origins' => 'mock-origins', + 'destinations' => 'mock-destinations', + 'foo' => 'bar', + ])->andReturn('mock-result'); + + $this->assertSame('mock-result', $client->driving('mock-origins', 'mock-destinations', ['foo' => 'bar'])); + } + + public function testRiding() + { + $client = $this->mockApiClient(Client::class); + + $client->expects()->httpGet('routematrix/v2/riding', [ + 'origins' => 'mock-origins', + 'destinations' => 'mock-destinations', + 'foo' => 'bar', + ])->andReturn('mock-result'); + + $this->assertSame('mock-result', $client->riding('mock-origins', 'mock-destinations', ['foo' => 'bar'])); + } + + public function testWalking() + { + $client = $this->mockApiClient(Client::class); + + $client->expects()->httpGet('routematrix/v2/walking', [ + 'origins' => 'mock-origins', + 'destinations' => 'mock-destinations', + 'foo' => 'bar', + ])->andReturn('mock-result'); + + $this->assertSame('mock-result', $client->walking('mock-origins', 'mock-destinations', ['foo' => 'bar'])); + } + + public function testProcessCoordinate() + { + $class = new \ReflectionClass(Client::class); + + $instance = $class->newInstance(new ServiceContainer()); + + $method = $class->getMethod('processCoordinate'); + $method->setAccessible(true); + + $std = new \stdClass(); + $std->foo = 'bar'; + + $this->assertSame('foo', $method->invoke($instance, 'foo')); + $this->assertSame(1234, $method->invoke($instance, 1234)); + $this->assertSame('bar', $method->invoke($instance, $std)); + $this->assertSame('mock-lat1,mock-lng1|mock-lat2,mock-lng2', $method->invoke($instance, ['mock-lat1,mock-lng1', 'mock-lat2,mock-lng2'])); + $this->assertSame('mock-lat1,mock-lng1|mock-lat2,mock-lng2', $method->invoke($instance, [['mock-lat1', 'mock-lng1'], ['mock-lat2', 'mock-lng2']])); + } +} From acede47451ebaa894edb410dcf3d1ccfd8edcb0a Mon Sep 17 00:00:00 2001 From: her-cat Date: Fri, 4 Oct 2019 16:27:39 +0000 Subject: [PATCH 3/6] Apply fixes from StyleCI [ci skip] [skip ci] --- src/WebApi/RouteMatrix/Client.php | 19 ++++++++++++++----- src/WebApi/RouteMatrix/ServiceProvider.php | 9 +++++++++ tests/WebApi/RouteMatrix/ClientTest.php | 9 +++++++++ 3 files changed, 32 insertions(+), 5 deletions(-) diff --git a/src/WebApi/RouteMatrix/Client.php b/src/WebApi/RouteMatrix/Client.php index 3de23ff..2eb3501 100644 --- a/src/WebApi/RouteMatrix/Client.php +++ b/src/WebApi/RouteMatrix/Client.php @@ -1,10 +1,18 @@ + * + * This source file is subject to the MIT license that is bundled + * with this source code in the file LICENSE. + */ + namespace HerCat\BaiduMap\WebApi\RouteMatrix; use GuzzleHttp\Exception\GuzzleException; use HerCat\BaiduMap\Kernel\BaseClient; -use HerCat\BaiduMap\Kernel\Exceptions\InvalidArgumentException; use HerCat\BaiduMap\Kernel\Exceptions\InvalidConfigException; use HerCat\BaiduMap\Kernel\Http\Response; use HerCat\BaiduMap\Kernel\Support\Collection; @@ -20,7 +28,7 @@ class Client extends BaseClient /** * @param string|array $origins * @param string|array $destinations - * @param array $options + * @param array $options * * @return array|Response|Collection|mixed|object|ResponseInterface * @@ -40,7 +48,7 @@ public function driving($origins, $destinations, array $options = []) /** * @param string|array $origins * @param string|array $destinations - * @param array $options + * @param array $options * * @return array|Response|Collection|mixed|object|ResponseInterface * @@ -60,7 +68,7 @@ public function riding($origins, $destinations, array $options = []) /** * @param string|array $origins * @param string|array $destinations - * @param array $options + * @param array $options * * @return array|Response|Collection|mixed|object|ResponseInterface * @@ -79,13 +87,14 @@ public function walking($origins, $destinations, array $options = []) /** * @param string|array $coordinate + * * @return string */ protected function processCoordinate($coordinate) { if (is_object($coordinate)) { $coordinate = (array) $coordinate; - } else if (!is_array($coordinate)) { + } elseif (!is_array($coordinate)) { return $coordinate; } diff --git a/src/WebApi/RouteMatrix/ServiceProvider.php b/src/WebApi/RouteMatrix/ServiceProvider.php index be67a38..a47449b 100644 --- a/src/WebApi/RouteMatrix/ServiceProvider.php +++ b/src/WebApi/RouteMatrix/ServiceProvider.php @@ -1,5 +1,14 @@ + * + * This source file is subject to the MIT license that is bundled + * with this source code in the file LICENSE. + */ + namespace HerCat\BaiduMap\WebApi\RouteMatrix; use Pimple\Container; diff --git a/tests/WebApi/RouteMatrix/ClientTest.php b/tests/WebApi/RouteMatrix/ClientTest.php index de88cab..3bf17a6 100644 --- a/tests/WebApi/RouteMatrix/ClientTest.php +++ b/tests/WebApi/RouteMatrix/ClientTest.php @@ -1,5 +1,14 @@ + * + * This source file is subject to the MIT license that is bundled + * with this source code in the file LICENSE. + */ + namespace HerCat\BaiduMap\Tests\WebApi\RouteMatrix; use HerCat\BaiduMap\Kernel\ServiceContainer; From 8f8aae5f6dc2f80f30e2142d9fb6cf55d5ef9104 Mon Sep 17 00:00:00 2001 From: her-cat Date: Sat, 5 Oct 2019 00:33:07 +0800 Subject: [PATCH 4/6] Fix phpstan error. --- phpstan.neon | 1 + src/WebApi/BatchRequest/Client.php | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/phpstan.neon b/phpstan.neon index ff75ce6..89d48e2 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -5,4 +5,5 @@ parameters: inferPrivatePropertyTypeFromConstructor: true ignoreErrors: - '#HerCat\\BaiduMap\\Kernel\\Exceptions\\InvalidConfigException is not subtype of Throwable#' + - '#PHPDoc tag @throws with type GuzzleHttp\\Exception\\GuzzleException is not subtype of Throwable#' - '#Default value of the parameter \#2 \$depth \(INF.0\) of method HerCat\\BaiduMap\\Kernel\\Support\\Arr::flatten\(\) is incompatible with type int.#' diff --git a/src/WebApi/BatchRequest/Client.php b/src/WebApi/BatchRequest/Client.php index f1bf80b..0889d1c 100644 --- a/src/WebApi/BatchRequest/Client.php +++ b/src/WebApi/BatchRequest/Client.php @@ -29,7 +29,7 @@ class Client extends BaseClient protected $needSignature = false; /** - * @param $params + * @param array $params * * @return array|Response|Collection|mixed|object|ResponseInterface * From e3be25b24a1eb07e0a145e778289963ca21959fc Mon Sep 17 00:00:00 2001 From: her-cat Date: Sun, 6 Oct 2019 21:32:51 +0800 Subject: [PATCH 5/6] Update readme.md --- README.md | 52 +++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 41 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index b1d1a46..093fb43 100644 --- a/README.md +++ b/README.md @@ -1,29 +1,59 @@ -

baidu-map

+

🗺️ baidu-map

可能是我用过的最好用的百度地图 SDK 了

[![Build Status](https://travis-ci.org/her-cat/baidu-map.svg?branch=master)](https://travis-ci.org/her-cat/baidu-map) [![StyleCI build status](https://github.styleci.io/repos/200389077/shield)](https://github.styleci.io/repos/200389077) -## Installing +## 环境要求 + +- PHP >= 5.6 +- [Composer](https://getcomposer.org/) +- fileinfo 拓展(获取静态图需要用到) + +## 安装 ```shell -$ composer require her-cat/baidu-map -vvv +$ composer require "her-cat/baidu-map" -vvv ``` -## Usage +## 使用 + +```php + 'your ak', +// 'sk' => 'your sk', + 'log' => [ + 'file' => './baidu-map.log' + ], + 'response_type' => 'array', +]; + +$webApi = Factory::webApi($config); + +$result = $webApi->timezone->get('116.30815', '40.056878'); + +// Array +// ( +// [status] => 0 +// [timezone_id] => Asia/Shanghai +// [dst_offset] => 0 +// [raw_offset] => 28800 +// ) +``` -## Contributing +## 文档 -You can contribute in one of three ways: +- 编写中 -1. File bug reports using the [issue tracker](https://github.com/hercat//baidu-map/issues). -2. Answer questions or fix bugs on the [issue tracker](https://github.com/hercat//baidu-map/issues). -3. Contribute new features or update the wiki. +## 参考 -_The code contribution process is not very formal. You just need to make sure that you follow the PSR-0, PSR-1, and PSR-2 coding guidelines. Any new code contributions must be accompanied by unit tests where applicable._ +- [overtrue/wechat](https://github.com/overtrue/wechat) +- [PHP 扩展包实战教程 - 从入门到发布](https://learnku.com/courses/creating-package) ## License From 6286157396e9ee6e2edb03f6f877d879e69a1654 Mon Sep 17 00:00:00 2001 From: her-cat Date: Sun, 6 Oct 2019 21:48:43 +0800 Subject: [PATCH 6/6] Update readme.md --- README.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/README.md b/README.md index 093fb43..59dade9 100644 --- a/README.md +++ b/README.md @@ -17,6 +17,12 @@ $ composer require "her-cat/baidu-map" -vvv ``` +## 单元测试 + +```shell +$ composer test +``` + ## 使用 ```php