1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Ivory Google Map package. |
5
|
|
|
* |
6
|
|
|
* (c) Eric GELOEN <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please read the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Ivory\GoogleMap\Service; |
13
|
|
|
|
14
|
|
|
use Http\Client\HttpClient; |
15
|
|
|
use Http\Message\MessageFactory; |
16
|
|
|
use Psr\Http\Message\RequestInterface as PsrRequestInterface; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @author GeLo <[email protected]> |
20
|
|
|
*/ |
21
|
|
|
abstract class AbstractService |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
private $url; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var string|null |
30
|
|
|
*/ |
31
|
|
|
private $key; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var BusinessAccount|null |
35
|
|
|
*/ |
36
|
|
|
private $businessAccount; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param string $url |
40
|
|
|
*/ |
41
|
|
|
public function __construct($url) |
42
|
|
|
{ |
43
|
|
|
$this->setUrl($url); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @return string |
48
|
|
|
*/ |
49
|
|
|
public function getUrl() |
50
|
|
|
{ |
51
|
|
|
return $this->url; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param string $url |
56
|
|
|
*/ |
57
|
|
|
public function setUrl($url) |
58
|
|
|
{ |
59
|
|
|
$this->url = $url; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @return bool |
64
|
|
|
*/ |
65
|
|
|
public function hasKey() |
66
|
|
|
{ |
67
|
|
|
return $this->key !== null; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @return string|null |
72
|
|
|
*/ |
73
|
|
|
public function getKey() |
74
|
|
|
{ |
75
|
|
|
return $this->key; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param string|null $key |
80
|
|
|
*/ |
81
|
|
|
public function setKey($key) |
82
|
|
|
{ |
83
|
|
|
$this->key = $key; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @return bool |
88
|
|
|
*/ |
89
|
|
|
public function hasBusinessAccount() |
90
|
|
|
{ |
91
|
|
|
return $this->businessAccount !== null; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @return BusinessAccount |
96
|
|
|
*/ |
97
|
|
|
public function getBusinessAccount() |
98
|
|
|
{ |
99
|
|
|
return $this->businessAccount; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param BusinessAccount $businessAccount |
104
|
|
|
*/ |
105
|
|
|
public function setBusinessAccount(BusinessAccount $businessAccount = null) |
106
|
|
|
{ |
107
|
|
|
$this->businessAccount = $businessAccount; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @param RequestInterface $request |
112
|
|
|
* |
113
|
|
|
* @return string |
114
|
|
|
*/ |
115
|
|
|
protected function createUrl(RequestInterface $request) |
116
|
|
|
{ |
117
|
|
|
$query = $request->buildQuery(); |
118
|
|
|
|
119
|
|
|
if ($this->hasKey()) { |
120
|
|
|
$query['key'] = $this->key; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
$url = $this->createBaseUrl($request).'?'.http_build_query($query, '', '&'); |
124
|
|
|
|
125
|
|
|
if ($this->hasBusinessAccount()) { |
126
|
|
|
$url = $this->businessAccount->signUrl($url); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
return $url; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @param RequestInterface $request |
134
|
|
|
* |
135
|
|
|
* @return string |
136
|
|
|
*/ |
137
|
|
|
protected function createBaseUrl(RequestInterface $request) |
138
|
|
|
{ |
139
|
|
|
$url = $this->getUrl(); |
140
|
|
|
|
141
|
|
|
if ($request instanceof ContextualizedRequestInterface) { |
142
|
|
|
$url .= '/'.$request->buildContext(); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
return $url; |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|