Passed
Push — master ( b45126...8af3f1 )
by 世昌
03:18
created

Request   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 104
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 15
c 2
b 0
f 0
dl 0
loc 104
rs 10
wmc 17

9 Methods

Rating   Name   Duplication   Size   Complexity  
A cookies() 0 3 2
A files() 0 3 1
A header() 0 3 2
A __construct() 0 4 1
A server() 0 3 2
A get() 0 3 2
A input() 0 3 2
A post() 0 3 2
A buildFilesFromEnv() 0 5 3
1
<?php
2
3
4
namespace suda\swoole;
5
6
use suda\framework\http\UploadedFile;
7
8
/**
9
 * Class Request
10
 * @package suda\swoole
11
 */
12
class Request implements \suda\framework\http\Request
13
{
14
15
    /**
16
     * @var \Swoole\Http\Request
17
     */
18
    protected $request;
19
20
    /**
21
     * @var UploadedFile[]
22
     */
23
    protected $files;
24
25
    /**
26
     * Request constructor.
27
     * @param \Swoole\Http\Request $request
28
     */
29
    public function __construct(\Swoole\Http\Request $request)
30
    {
31
        $this->request = $request;
32
        $this->buildFilesFromEnv();
33
    }
34
35
    /**
36
     * 获取请求头
37
     *
38
     * @return array
39
     */
40
    public function header(): array
41
    {
42
        return $this->request->header ?: [];
43
    }
44
45
    /**
46
     * 获取服务环境
47
     *
48
     * @return array
49
     */
50
    public function server(): array
51
    {
52
        return $this->request->server ?: [];
53
    }
54
55
    /**
56
     * GET数据
57
     *
58
     * @return array
59
     */
60
    public function get(): array
61
    {
62
        return $this->request->get ?: [];
63
    }
64
65
    /**
66
     * POST数据
67
     *
68
     * @return array
69
     */
70
    public function post(): array
71
    {
72
        return $this->request->post ?: [];
73
    }
74
75
    /**
76
     * 获取Cookie
77
     *
78
     * @return array
79
     */
80
    public function cookies(): array
81
    {
82
        return $this->request->cookie ?: [];
83
    }
84
85
    /**
86
     * 获取原始输入
87
     *
88
     * @return string
89
     */
90
    public function input(): string
91
    {
92
        return $this->request->rawContent() ?: '';
93
    }
94
95
    /**
96
     * Get 输出的文件
97
     *
98
     * @return  UploadedFile[]
99
     */
100
    public function files(): array
101
    {
102
        return $this->files;
103
    }
104
105
106
    /**
107
     * 构建文件数据
108
     *
109
     * @return void
110
     */
111
    protected function buildFilesFromEnv()
112
    {
113
        if (is_array($this->request->files)) {
114
            foreach ($this->request->files as $name => $file) {
115
                $this->files[$name] = new UploadedFile($file['tmp_name'], $file['name'], $file['type'], $file['error']);
116
            }
117
        }
118
    }
119
}
120