-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of https://github.com/her-cat/baidu-map
- Loading branch information
Showing
7 changed files
with
268 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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']])); | ||
} | ||
} |