1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the Ivory Google Map package. |
7
|
|
|
* |
8
|
|
|
* (c) Eric GELOEN <[email protected]> |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please read the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Ivory\GoogleMap\Service\Direction; |
15
|
|
|
|
16
|
|
|
use Ivory\GoogleMap\Service\AbstractSerializableService; |
17
|
|
|
use Ivory\GoogleMap\Service\Direction\Request\DirectionRequestInterface; |
18
|
|
|
use Ivory\GoogleMap\Service\Direction\Response\DirectionResponse; |
19
|
|
|
use Ivory\Serializer\Context\Context; |
20
|
|
|
use Ivory\Serializer\Naming\SnakeCaseNamingStrategy; |
21
|
|
|
use Ivory\Serializer\SerializerInterface; |
22
|
|
|
use Psr\Http\Client\ClientInterface; |
23
|
|
|
use Psr\Http\Message\RequestFactoryInterface; |
24
|
|
|
|
25
|
|
|
class DirectionService extends AbstractSerializableService |
26
|
|
|
{ |
27
|
|
|
public function __construct( |
28
|
|
|
ClientInterface $client, |
29
|
|
|
RequestFactoryInterface $messageFactory, |
30
|
|
|
SerializerInterface $serializer = null |
31
|
|
|
) { |
32
|
|
|
parent::__construct('https://maps.googleapis.com/maps/api/directions', $client, $messageFactory, $serializer); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function route(DirectionRequestInterface $request): DirectionResponse |
36
|
|
|
{ |
37
|
|
|
$httpRequest = $this->createRequest($request); |
38
|
|
|
$httpResponse = $this->getClient()->sendRequest($httpRequest); |
39
|
|
|
|
40
|
|
|
$response = $this->deserialize( |
41
|
|
|
$httpResponse, |
42
|
|
|
DirectionResponse::class, |
43
|
|
|
(new Context())->setNamingStrategy(new SnakeCaseNamingStrategy()) |
44
|
|
|
); |
45
|
|
|
|
46
|
|
|
$response->setRequest($request); |
47
|
|
|
|
48
|
|
|
return $response; |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
|