diff --git a/README.md b/README.md index b1d1a46..59dade9 100644 --- a/README.md +++ b/README.md @@ -1,29 +1,65 @@ -

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 +``` + +## 单元测试 ```shell -$ composer require her-cat/baidu-map -vvv +$ composer test ``` -## Usage +## 使用 -TODO +```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 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/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/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 * diff --git a/src/WebApi/RouteMatrix/Client.php b/src/WebApi/RouteMatrix/Client.php new file mode 100644 index 0000000..2eb3501 --- /dev/null +++ b/src/WebApi/RouteMatrix/Client.php @@ -0,0 +1,107 @@ + + * + * 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\InvalidConfigException; +use HerCat\BaiduMap\Kernel\Http\Response; +use HerCat\BaiduMap\Kernel\Support\Collection; +use Psr\Http\Message\ResponseInterface; + +/** + * Class Client. + * + * @author her-cat + */ +class Client extends BaseClient +{ + /** + * @param string|array $origins + * @param string|array $destinations + * @param array $options + * + * @return array|Response|Collection|mixed|object|ResponseInterface + * + * @throws GuzzleException + * @throws InvalidConfigException + */ + public function driving($origins, $destinations, array $options = []) + { + $options = array_merge([ + 'origins' => $this->processCoordinate($origins), + 'destinations' => $this->processCoordinate($destinations), + ], $options); + + 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); + + 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($origins), + 'destinations' => $this->processCoordinate($destinations), + ], $options); + + return $this->httpGet('routematrix/v2/walking', $options); + } + + /** + * @param string|array $coordinate + * + * @return string + */ + protected function processCoordinate($coordinate) + { + if (is_object($coordinate)) { + $coordinate = (array) $coordinate; + } elseif (!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..a47449b --- /dev/null +++ b/src/WebApi/RouteMatrix/ServiceProvider.php @@ -0,0 +1,33 @@ + + * + * 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; +use Pimple\ServiceProviderInterface; + +/** + * Class ServiceProvider. + * + * @author her-cat + */ +class ServiceProvider implements ServiceProviderInterface +{ + /** + * {@inheritdoc} + */ + public function register(Container $app) + { + $app['route_matrix'] = function ($app) { + return new Client($app); + }; + } +} diff --git a/tests/WebApi/RouteMatrix/ClientTest.php b/tests/WebApi/RouteMatrix/ClientTest.php new file mode 100644 index 0000000..3bf17a6 --- /dev/null +++ b/tests/WebApi/RouteMatrix/ClientTest.php @@ -0,0 +1,77 @@ + + * + * 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; +use HerCat\BaiduMap\Tests\TestCase; +use HerCat\BaiduMap\WebApi\RouteMatrix\Client; + +class ClientTest extends TestCase +{ + public function testDriving() + { + $client = $this->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']])); + } +}