|
1
|
|
|
<?php namespace Arcanedev\GeoLocation\Google; |
|
2
|
|
|
|
|
3
|
|
|
use Arcanedev\GeoLocation\Contracts\Entities\Position; |
|
4
|
|
|
use GuzzleHttp\ClientInterface; |
|
5
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Class AbstractService |
|
9
|
|
|
* |
|
10
|
|
|
* @package Arcanedev\GeoLocation\Google |
|
11
|
|
|
* @author ARCANEDEV <[email protected]> |
|
12
|
|
|
*/ |
|
13
|
|
|
abstract class AbstractService |
|
14
|
|
|
{ |
|
15
|
|
|
/* ----------------------------------------------------------------- |
|
16
|
|
|
| Properties |
|
17
|
|
|
| ----------------------------------------------------------------- |
|
18
|
|
|
*/ |
|
19
|
|
|
|
|
20
|
|
|
/** @var \GuzzleHttp\ClientInterface */ |
|
21
|
|
|
protected $client; |
|
22
|
|
|
|
|
23
|
|
|
/** @var string|null */ |
|
24
|
|
|
protected $key = null; |
|
25
|
|
|
|
|
26
|
|
|
/* ----------------------------------------------------------------- |
|
27
|
|
|
| Constructor |
|
28
|
|
|
| ----------------------------------------------------------------- |
|
29
|
|
|
*/ |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* GoogleDistanceMatrix constructor. |
|
33
|
|
|
* |
|
34
|
|
|
* @param \GuzzleHttp\ClientInterface $client |
|
35
|
|
|
*/ |
|
36
|
|
|
public function __construct(ClientInterface $client) |
|
37
|
|
|
{ |
|
38
|
|
|
$this->setHttpClient($client); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/* ----------------------------------------------------------------- |
|
42
|
|
|
| Getters & Setters |
|
43
|
|
|
| ----------------------------------------------------------------- |
|
44
|
|
|
*/ |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Set the HTTP Client. |
|
48
|
|
|
* |
|
49
|
|
|
* @param \GuzzleHttp\ClientInterface $client |
|
50
|
|
|
* |
|
51
|
|
|
* @return self |
|
52
|
|
|
*/ |
|
53
|
|
|
public function setHttpClient(ClientInterface $client) |
|
54
|
|
|
{ |
|
55
|
|
|
$this->client = $client; |
|
56
|
|
|
|
|
57
|
|
|
return $this; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Set the API Key. |
|
62
|
|
|
* |
|
63
|
|
|
* @param string $key |
|
64
|
|
|
* |
|
65
|
|
|
* @return self |
|
66
|
|
|
*/ |
|
67
|
|
|
public function setKey($key) |
|
68
|
|
|
{ |
|
69
|
|
|
$this->key = $key; |
|
70
|
|
|
|
|
71
|
|
|
return $this; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/* ----------------------------------------------------------------- |
|
75
|
|
|
| Common Methods |
|
76
|
|
|
| ----------------------------------------------------------------- |
|
77
|
|
|
*/ |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Make a GET request. |
|
81
|
|
|
* |
|
82
|
|
|
* @param string $url |
|
83
|
|
|
* @param array $options |
|
84
|
|
|
* |
|
85
|
|
|
* @return mixed |
|
86
|
|
|
*/ |
|
87
|
|
|
protected function get($url, array $options) |
|
88
|
|
|
{ |
|
89
|
|
|
$response = $this->client->request('GET', $url, $options); |
|
90
|
|
|
|
|
91
|
|
|
return $this->prepareResponse($response); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Prepare the URL query. |
|
96
|
|
|
* |
|
97
|
|
|
* @param array $params |
|
98
|
|
|
* |
|
99
|
|
|
* @return string |
|
100
|
|
|
*/ |
|
101
|
|
|
protected function prepareQuery(array $params) |
|
102
|
|
|
{ |
|
103
|
|
|
$query = http_build_query(array_filter( |
|
104
|
|
|
array_merge($params, $this->getDefaultQueryParams()) |
|
105
|
|
|
)); |
|
106
|
|
|
|
|
107
|
|
|
return empty($query) ? '' : '?'.$query; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Get the default query params. |
|
112
|
|
|
* |
|
113
|
|
|
* @return array |
|
114
|
|
|
*/ |
|
115
|
|
|
abstract protected function getDefaultQueryParams(); |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Prepare the response. |
|
119
|
|
|
* |
|
120
|
|
|
* @param \Psr\Http\Message\ResponseInterface $response |
|
121
|
|
|
* |
|
122
|
|
|
* @return \Arcanedev\GeoLocation\Google\AbstractResponse |
|
123
|
|
|
*/ |
|
124
|
|
|
abstract protected function prepareResponse(ResponseInterface $response); |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* Parse the position object for the query. |
|
128
|
|
|
* |
|
129
|
|
|
* @param \Arcanedev\GeoLocation\Contracts\Entities\Position $position |
|
130
|
|
|
* |
|
131
|
|
|
* @return string |
|
132
|
|
|
*/ |
|
133
|
|
|
protected function parsePosition(Position $position) |
|
134
|
|
|
{ |
|
135
|
|
|
return $position->lat()->value().','.$position->lng()->value(); |
|
136
|
|
|
} |
|
137
|
|
|
} |
|
138
|
|
|
|