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