Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/her-cat/baidu-map
Browse files Browse the repository at this point in the history
  • Loading branch information
her-cat committed Oct 20, 2019
2 parents 96aa7f9 + 6286157 commit 834b99d
Show file tree
Hide file tree
Showing 7 changed files with 268 additions and 12 deletions.
58 changes: 47 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,29 +1,65 @@
<h1 align="center"> baidu-map </h1>
<h1 align="center"> 🗺️ baidu-map </h1>

<p align="center">可能是我用过的最好用的百度地图 SDK 了</p>

[![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
<?php

use HerCat\BaiduMap\Factory;

$config = [
'ak' => '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

Expand Down
1 change: 1 addition & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -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.#'
2 changes: 2 additions & 0 deletions src/WebApi/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -51,5 +52,6 @@ class Application extends ServiceContainer
CoordsConvert\ServiceProvider::class,
Direction\ServiceProvider::class,
BatchRequest\ServiceProvider::class,
RouteMatrix\ServiceProvider::class,
];
}
2 changes: 1 addition & 1 deletion src/WebApi/BatchRequest/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class Client extends BaseClient
protected $needSignature = false;

/**
* @param $params
* @param array $params
*
* @return array|Response|Collection|mixed|object|ResponseInterface
*
Expand Down
107 changes: 107 additions & 0 deletions src/WebApi/RouteMatrix/Client.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
<?php

/*
* This file is part of the her-cat/baidu-map.
*
* (c) her-cat <[email protected]>
*
* 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 <[email protected]>
*/
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);
}
}
33 changes: 33 additions & 0 deletions src/WebApi/RouteMatrix/ServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

/*
* This file is part of the her-cat/baidu-map.
*
* (c) her-cat <[email protected]>
*
* 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 <[email protected]>
*/
class ServiceProvider implements ServiceProviderInterface
{
/**
* {@inheritdoc}
*/
public function register(Container $app)
{
$app['route_matrix'] = function ($app) {
return new Client($app);
};
}
}
77 changes: 77 additions & 0 deletions tests/WebApi/RouteMatrix/ClientTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

/*
* This file is part of the her-cat/baidu-map.
*
* (c) her-cat <[email protected]>
*
* 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']]));
}
}

0 comments on commit 834b99d

Please sign in to comment.