Passed
Push — master ( d352f7...706a16 )
by 世昌
02:30
created

RequestWrapper::getUriBase()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
namespace suda\framework\request;
3
4
use suda\framework\http\Request;
5
use suda\framework\http\UploadedFile;
6
7
/**
8
 * 请求包装器
9
 * 包装PHP请求
10
 */
11
class RequestWrapper
12
{
13
14
    /**
15
     * HTTP请求
16
     *
17
     * @var Request
18
     */
19
    protected $request;
20
    /**
21
     * 远程地址
22
     *
23
     * @var string
24
     */
25
    protected $remoteAddr = '0.0.0.0';
26
    
27
    /**
28
     * 获取本地HOST
29
     *
30
     * @var string
31
     */
32
    protected $host = '127.0.0.1';
33
34
    /**
35
     * 获取本地端口
36
     *
37
     * @var int
38
     */
39
    protected $port = 80;
40
41
    /**
42
     * 是否为安全模式
43
     *
44
     * @var boolean
45
     */
46
    protected $secure = false;
47
48
    /**
49
     * 请求URI
50
     *
51
     * @var string
52
     */
53
    protected $uri = '/';
54
55
    /**
56
     * 请求参数
57
     *
58
     * @var string
59
     */
60
    protected $method = 'GET';
61
62
    /**
63
     * 查询参数($_GET)
64
     *
65
     * @var array
66
     */
67
    protected $query = [];
68
69
    /**
70
     * 请求索引
71
     *
72
     * @var string
73
     */
74
    protected $index;
75
76
    /**
77
     * URI基础部分
78
     *
79
     * @var string
80
     */
81
    protected $uriBase;
82
83
    /**
84
     * 创建请求包装器
85
     *
86
     * @param Request $request
87
     */
88
    public function __construct(Request $request)
89
    {
90
        $this->request = $request;
91
        (new Builder($request))->build($this);
92
    }
93
94
    /**
95
     * Get 远程地址
96
     *
97
     * @return  string
98
     */
99
    public function getRemoteAddr()
100
    {
101
        return $this->remoteAddr;
102
    }
103
104
    /**
105
     * Set 远程地址
106
     *
107
     * @param  string  $remoteAddr  远程地址
108
     *
109
     * @return  $this
110
     */
111
    public function setRemoteAddr(string $remoteAddr)
112
    {
113
        $this->remoteAddr = $remoteAddr;
114
115
        return $this;
116
    }
117
118
    /**
119
     * Get 获取本地HOST
120
     *
121
     * @return  string
122
     */
123
    public function getHost()
124
    {
125
        return $this->host;
126
    }
127
128
    /**
129
     * Set 获取本地HOST
130
     *
131
     * @param  string  $host  获取本地HOST
132
     *
133
     * @return  $this
134
     */
135
    public function setHost(string $host)
136
    {
137
        $this->host = $host;
138
139
        return $this;
140
    }
141
142
    /**
143
     * Get 获取本地端口
144
     *
145
     * @return  int
146
     */
147
    public function getPort()
148
    {
149
        return $this->port;
150
    }
151
152
    /**
153
     * Set 获取本地端口
154
     *
155
     * @param  int  $port  获取本地端口
156
     *
157
     * @return  $this
158
     */
159
    public function setPort(int $port)
160
    {
161
        $this->port = $port;
162
163
        return $this;
164
    }
165
166
    /**
167
     * Get 是否为安全模式
168
     *
169
     * @return  bool
170
     */
171
    public function isSecure():bool
172
    {
173
        return $this->secure;
174
    }
175
176
    /**
177
     * Set 是否为安全模式
178
     *
179
     * @param  bool  $secure  是否为安全模式
180
     * @return  $this
181
     */
182
    public function setSecure(bool $secure)
183
    {
184
        $this->secure = $secure;
185
        return $this;
186
    }
187
188
    /**
189
     * Get 请求URI
190
     *
191
     * @return  string
192
     */
193
    public function getUri()
194
    {
195
        return $this->uri;
196
    }
197
198
    /**
199
     * Set 请求URI
200
     *
201
     * @param  string  $uri  请求URI
202
     * @return  $this
203
     */
204
    public function setUri(string $uri)
205
    {
206
        $this->uri = $uri;
207
        return $this;
208
    }
209
210
    /**
211
     * Get 请求参数
212
     *
213
     * @return  string
214
     */
215
    public function getMethod()
216
    {
217
        return $this->method;
218
    }
219
220
    /**
221
     * Set 请求参数
222
     *
223
     * @param  string  $method  请求参数
224
     * @return  $this
225
     */
226
    public function setMethod(string $method)
227
    {
228
        $this->method = $method;
229
        return $this;
230
    }
231
232
    /**
233
     * 获取查询参数
234
     *
235
     * @param string $name
236
     * @param mixed $default
237
     * @return mixed
238
     */
239
    public function getQuery(?string $name = null, $default = null)
240
    {
241
        return  $name === null ? $this->query:$this->query[$name] ?? $default;
242
    }
243
244
    /**
245
     * 设置查询参数
246
     *
247
     * @param string $name
248
     * @param $query
249
     * @return $this
250
     */
251
    public function setQuery(string $name, $query)
252
    {
253
        $this->query[$name] = $query;
254
255
        return $this;
256
    }
257
    
258
    /**
259
     * 设置查询参数
260
     *
261
     * @param array $query
262
     * @return $this
263
     */
264
    public function setQueries(array $query)
265
    {
266
        $this->query = $query;
267
268
        return $this;
269
    }
270
271
    /**
272
     * 合并参数
273
     *
274
     * @param array $query
275
     * @return $this
276
     */
277
    public function mergeQueries(array $query)
278
    {
279
        $this->query = array_merge($this->query, $query);
280
        return $this;
281
    }
282
283
    /**
284
     * Get 请求索引
285
     *
286
     * @return  string
287
     */
288
    public function getIndex()
289
    {
290
        return $this->index;
291
    }
292
293
    /**
294
     * Set 请求索引
295
     *
296
     * @param  string  $index  请求索引
297
     * @return $this
298
     */
299
    public function setIndex(string $index)
300
    {
301
        $this->index = $index;
302
303
        return $this;
304
    }
305
306
    /**
307
     * 获取文件
308
     *
309
     * @param string|null $name
310
     * @return  UploadedFile[]|UploadedFile|null
311
     */
312
    public function getFile(?string $name = null)
313
    {
314
        return null === $name ? $this->request->files() : $this->request->files()[$name] ?? null;
315
    }
316
317
    /**
318
     * 获取请求头
319
     *
320
     * @param string $name
321
     * @param mixed $default
322
     * @return mixed
323
     */
324
    public function getHeader(string $name, $default = null)
325
    {
326
        if (array_key_exists(strtolower($name), $this->request->header())) {
327
            return $this->request->header()[$name];
328
        }
329
        return $default;
330
    }
331
332
    /**
333
     * 判断请求头
334
     *
335
     * @param string $name
336
     * @return boolean
337
     */
338
    public function hasHeader(string $name)
339
    {
340
        return $this->getHeader($name) !== null;
341
    }
342
343
    /**
344
     * Get 文件包装
345
     *
346
     * @return  UploadedFile[]
347
     */
348
    public function getFiles()
349
    {
350
        return $this->request->files();
351
    }
352
353
    /**
354
     * Get 请求头部
355
     *
356
     * @return  string[]
357
     */
358
    public function getHeaders()
359
    {
360
        return $this->request->header();
361
    }
362
363
    /**
364
     * 获取Cookie
365
     *
366
     * @param string $name
367
     * @param mixed $default
368
     * @return mixed
369
     */
370
    public function getCookie(string $name = null, $default = null)
371
    {
372
        if ($name === null) {
373
            return $this->request->cookies();
374
        }
375
        return $this->request->cookies()[$name] ?? $default;
376
    }
377
378
    /**
379
     * 获取服务器变量
380
     *
381
     * @param string $name
382
     * @param mixed $default
383
     * @return mixed
384
     */
385
    public function getServer(string $name = null, $default = null)
386
    {
387
        if ($name === null) {
388
            return $this->request->server();
389
        }
390
        return $this->request->server()[$name] ?? $default;
391
    }
392
393
    /**
394
     * Get URI基础部分
395
     * @return  string
396
     */
397
    public function getUriBase()
398
    {
399
        return $this->uriBase;
400
    }
401
402
    /**
403
     * Set URI基础部分
404
     *
405
     * @param  string  $uriBase  URI基础部分
406
     * @return  $this
407
     */
408
    public function setUriBase(string $uriBase)
409
    {
410
        $this->uriBase = $uriBase;
411
        return $this;
412
    }
413
}
414