1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Tools to use API as ActiveRecord for Yii2 |
4
|
|
|
* |
5
|
|
|
* @link https://github.com/hiqdev/yii2-hiart |
6
|
|
|
* @package yii2-hiart |
7
|
|
|
* @license BSD-3-Clause |
8
|
|
|
* @copyright Copyright (c) 2015-2017, HiQDev (http://hiqdev.com/) |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace hiqdev\hiart; |
12
|
|
|
|
13
|
|
|
abstract class AbstractRequest implements RequestInterface |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var string response implementation to be specified in concrete implementation |
17
|
|
|
*/ |
18
|
|
|
protected $responseClass; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var QueryBuilderInterface |
22
|
|
|
*/ |
23
|
|
|
protected $builder; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var Query |
27
|
|
|
*/ |
28
|
|
|
protected $query; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var string Connection name |
32
|
|
|
*/ |
33
|
|
|
protected $dbname; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var array request method |
37
|
|
|
*/ |
38
|
|
|
protected $method; |
39
|
|
|
protected $uri; |
40
|
|
|
protected $headers = []; |
41
|
|
|
protected $body; |
42
|
|
|
protected $version; |
43
|
|
|
|
44
|
|
|
protected $isBuilt; |
45
|
|
|
protected $parts = []; |
46
|
|
|
protected $fullUri; |
47
|
|
|
|
48
|
|
|
abstract public function send($options = []); |
49
|
|
|
|
50
|
|
|
public function __construct(QueryBuilderInterface $builder, Query $query) |
51
|
|
|
{ |
52
|
|
|
$this->builder = $builder; |
53
|
|
|
$this->query = $query; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function getDbname() |
57
|
|
|
{ |
58
|
|
|
return $this->dbname; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function getMethod() |
62
|
|
|
{ |
63
|
|
|
return $this->method; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function getUri() |
67
|
|
|
{ |
68
|
|
|
return $this->uri; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function getFullUri() |
72
|
|
|
{ |
73
|
|
|
if ($this->fullUri === null) { |
74
|
|
|
$this->fullUri = $this->createFullUri(); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return $this->fullUri; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function createFullUri() |
81
|
|
|
{ |
82
|
|
|
return ($this->isFullUri($this->uri) ? '' : $this->getDb()->getBaseUri()) . $this->uri; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function isFullUri($uri) |
86
|
|
|
{ |
87
|
|
|
return preg_match('/^https?:\\/\\//i', $uri); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function getHeaders() |
91
|
|
|
{ |
92
|
|
|
return $this->headers; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function getBody() |
96
|
|
|
{ |
97
|
|
|
return $this->body; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function getVersion() |
101
|
|
|
{ |
102
|
|
|
return $this->version; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @return Query |
107
|
|
|
*/ |
108
|
|
|
public function getQuery() |
109
|
|
|
{ |
110
|
|
|
return $this->query; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
protected function build() |
114
|
|
|
{ |
115
|
|
|
if ($this->isBuilt === null) { |
116
|
|
|
if (!empty($this->query)) { |
117
|
|
|
$this->updateFromQuery(); |
118
|
|
|
} |
119
|
|
|
$this->isBuilt = true; |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
protected function updateFromQuery() |
124
|
|
|
{ |
125
|
|
|
$this->builder->prepare($this->query); |
126
|
|
|
|
127
|
|
|
$this->buildDbname(); |
128
|
|
|
$this->buildAuth(); |
129
|
|
|
$this->buildMethod(); |
130
|
|
|
$this->buildUri(); |
131
|
|
|
$this->buildQueryParams(); |
132
|
|
|
$this->buildHeaders(); |
133
|
|
|
$this->buildBody(); |
134
|
|
|
$this->buildFormParams(); |
135
|
|
|
$this->buildProtocolVersion(); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
protected function buildDbname() |
139
|
|
|
{ |
140
|
|
|
$this->dbname = $this->getDb()->name; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
protected function buildAuth() |
144
|
|
|
{ |
145
|
|
|
$this->builder->buildAuth($this->query); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
protected function buildMethod() |
149
|
|
|
{ |
150
|
|
|
$this->method = $this->builder->buildMethod($this->query) ?: 'GET'; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
protected function buildUri() |
154
|
|
|
{ |
155
|
|
|
$this->uri = $this->builder->buildUri($this->query); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
protected function buildQueryParams() |
159
|
|
|
{ |
160
|
|
|
$params = $this->builder->buildQueryParams($this->query); |
161
|
|
|
if (is_array($params)) { |
162
|
|
|
$params = http_build_query($params, '', '&'); |
163
|
|
|
} |
164
|
|
|
if (!empty($params)) { |
165
|
|
|
$this->uri .= '?' . $params; |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
protected function buildHeaders() |
170
|
|
|
{ |
171
|
|
|
$this->headers = $this->builder->buildHeaders($this->query); |
172
|
|
|
if (empty($this->headers['User-Agent'])) { |
173
|
|
|
$this->headers['User-Agent'] = $this->prepareUserAgent(); |
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
protected function buildBody() |
178
|
|
|
{ |
179
|
|
|
$this->body = $this->builder->buildBody($this->query); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
protected function buildFormParams() |
183
|
|
|
{ |
184
|
|
|
$this->setFormParams($this->builder->buildFormParams($this->query)); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
protected function setFormParams($params) |
188
|
|
|
{ |
189
|
|
|
if (!empty($params)) { |
190
|
|
|
$this->body = is_array($params) ? http_build_query($params, '', '&') : $params; |
191
|
|
|
$this->headers['Content-Type'] = 'application/x-www-form-urlencoded'; |
192
|
|
|
} |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
protected function buildProtocolVersion() |
196
|
|
|
{ |
197
|
|
|
$this->version = $this->builder->buildProtocolVersion($this->query) ?: '1.1'; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
public function serialize() |
201
|
|
|
{ |
202
|
|
|
return serialize($this->getParts()); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
public function unserialize($string) |
206
|
|
|
{ |
207
|
|
|
foreach (unserialize($string) as $key => $value) { |
208
|
|
|
$this->{$key} = $value; |
209
|
|
|
} |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
public function getParts() |
213
|
|
|
{ |
214
|
|
|
if (empty($this->parts)) { |
215
|
|
|
$this->build(); |
216
|
|
|
foreach (['dbname', 'method', 'uri', 'headers', 'body', 'version'] as $key) { |
217
|
|
|
$this->parts[$key] = $this->{$key}; |
218
|
|
|
} |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
return $this->parts; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
public function isRaw() |
225
|
|
|
{ |
226
|
|
|
return !empty($this->query->options['raw']); |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
protected function getHandler() |
230
|
|
|
{ |
231
|
|
|
$handler = $this->getDb()->getHandler(); |
232
|
|
|
if ($handler === null) { |
233
|
|
|
$handler = $this->createHandler(); |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
return $handler; |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
protected function createHandler() |
240
|
|
|
{ |
241
|
|
|
$config = $this->prepareHandlerConfig($this->getDb()->config); |
242
|
|
|
|
243
|
|
|
return new $this->handlerClass($config); |
|
|
|
|
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
protected function prepareHandlerConfig($config) |
247
|
|
|
{ |
248
|
|
|
return $config; |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
protected function prepareUserAgent() |
252
|
|
|
{ |
253
|
|
|
return $this->getDb()->getUserAgent(); |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
public function getDb() |
257
|
|
|
{ |
258
|
|
|
return isset($this->builder) ? $this->builder->db : AbstractConnection::getDb($this->dbname); |
|
|
|
|
259
|
|
|
} |
260
|
|
|
} |
261
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: