Passed
Push — dev ( 3d2d32...37c844 )
by 世昌
02:32
created

Request::buildData()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 3
eloc 6
c 2
b 0
f 0
nc 3
nop 0
dl 0
loc 9
rs 10
1
<?php
2
namespace suda\framework;
3
4
use function array_key_exists;
5
use suda\framework\http\UploadedFile;
6
use suda\framework\request\RequestWrapper;
7
use suda\framework\http\Request as HTTPRequest;
8
9
class Request extends RequestWrapper
10
{
11
    /**
12
     * 参数
13
     *
14
     * @var array
15
     */
16
    protected $parameter = [];
17
18
    /**
19
     * 附加属性
20
     *
21
     * @var array
22
     */
23
    protected $attribute = [];
24
25
    /**
26
     * 是否为JSON提交
27
     *
28
     * @var boolean
29
     */
30
    protected $isJson = false;
31
32
    /**
33
     * 创建请求
34
     *
35
     * @param HTTPRequest $request
36
     */
37
    public function __construct(HTTPRequest $request)
38
    {
39
        parent::__construct($request);
40
        $this->setIsJson($this->contentIsJson());
41
    }
42
43
    /**
44
     * 获取URL
45
     *
46
     * @return string
47
     */
48
    public function getUrl():string
49
    {
50
        return $this->request->server()['request-uri'] ?? '/';
51
    }
52
53
    /**
54
     * 获取请求属性
55
     *
56
     * @param string $name
57
     * @param mixed $default
58
     * @return  mixed
59
     */
60
    public function getAttribute(string $name, $default = null)
61
    {
62
        return $this->attribute[$name] ?? $default;
63
    }
64
65
    /**
66
     * 设置请求属性
67
     *
68
     * @param string $name
69
     * @param mixed $attribute
70
     * @return $this
71
     */
72
    public function setAttribute(string $name, $attribute)
73
    {
74
        $this->attribute[$name] = $attribute;
75
76
        return $this;
77
    }
78
    
79
    /**
80
     * Set 属性
81
     *
82
     * @param  array  $attribute  属性
83
     *
84
     * @return  self
85
     */
86
    public function setAttributes(array $attribute)
87
    {
88
        $this->attribute = $attribute;
89
90
        return $this;
91
    }
92
93
    /**
94
     * 获取Scheme
95
     *
96
     * @return string
97
     */
98
    public function getScheme():string
99
    {
100
        return $this->isSecure()?'https':'http';
101
    }
102
103
    /**
104
     * 判断是否是POST请求
105
     *
106
     * @return boolean
107
     */
108
    public function isPost():bool
109
    {
110
        return $this->getMethod() === 'POST';
111
    }
112
113
    /**
114
     * 判断是否为GET请求
115
     *
116
     * @return boolean
117
     */
118
    public function isGet():bool
119
    {
120
        return $this->getMethod() === 'GET';
121
    }
122
    
123
    /**
124
     * 判断内容是否为JSON
125
     *
126
     * @return boolean
127
     */
128
    public function isJson():bool
129
    {
130
        return $this->isJson;
131
    }
132
133
    /**
134
     * 获取HTTP输入
135
     *
136
     * @return string
137
     */
138
    public function input():string
139
    {
140
        return $this->request->input();
141
    }
142
143
    /**
144
     * 获取JSON数据
145
     *
146
     * @return array|null
147
     */
148
    public function json():?array
149
    {
150
        return $this->isJson?$this->parameter:null;
151
    }
152
153
    /**
154
     * 获取提交的文件
155
     *
156
     * @param string $name
157
     * @return UploadedFile|null
158
     */
159
    public function file(string $name): ?UploadedFile
160
    {
161
        $uploaded = $this->getFile($name);
162
        if ($uploaded instanceof UploadedFile) {
163
            return $uploaded;
164
        }
165
        return null;
166
    }
167
168
    /**
169
     * 判断是否有GET请求
170
     *
171
     * @param string|null $name
172
     * @return boolean
173
     */
174
    public function hasGet(?string $name = null)
175
    {
176
        $get = $this->getQuery();
177
        if ($name !== null) {
178
            return array_key_exists($name, $get);
179
        }
180
        return count($get) > 0;
181
    }
182
183
    /**
184
     * 判断是否有JSON数据请求
185
     *
186
     * @return boolean
187
     */
188
    public function hasJson()
189
    {
190
        if ($this->isJson() && $this->json()) {
191
            return true;
192
        }
193
        return false;
194
    }
195
196
    /**
197
     * 判断是否有POST数据请求
198
     *
199
     * @param string|null $name
200
     * @return boolean
201
     */
202
    public function hasPost(?string $name = null)
203
    {
204
        $post = $this->post();
205
        if ($name !== null) {
206
            return array_key_exists($name, $post);
0 ignored issues
show
Bug introduced by
It seems like $post can also be of type null; however, parameter $search of array_key_exists() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

206
            return array_key_exists($name, /** @scrutinizer ignore-type */ $post);
Loading history...
207
        }
208
        return count($post) > 0;
209
    }
210
211
    /**
212
     * 获取POST请求的值
213
     *
214
     * @param string $name
215
     * @param mixed $default
216
     * @return mixed 获取的值
217
     */
218
    public function post(?string $name = null, $default = null)
219
    {
220
        return $this->post()[$name] ?? $default;
221
    }
222
223
    /**
224
     * 获取GET参数
225
     *
226
     * @param string|null $name
227
     * @param mixed $default
228
     * @return mixed
229
     */
230
    public function get(?string $name = null, $default = null)
231
    {
232
        return $this->getQuery($name, $default);
233
    }
234
235
    /**
236
     * 获取Cookie
237
     *
238
     * @param string $name
239
     * @param mixed $default
240
     * @return mixed
241
     */
242
    public function cookie(string $name, $default = null)
243
    {
244
        return $_COOKIE[$name] ?? $default;
245
    }
246
247
    /**
248
     * 获取URI中的参数
249
     * @param string|null $name
250
     * @param mixed $default
251
     * @return mixed
252
     */
253
    public function getParameter(?string $name = null, $default = null)
254
    {
255
        return $name === null ? $this->parameter : $this->parameter[$name] ?? $default;
256
    }
257
258
    /**
259
     * 设置URI中的参数
260
     * @param array $parameter
261
     * @return $this
262
     */
263
    public function setParameter(array $parameter)
264
    {
265
        $this->parameter = $parameter;
266
        return $this;
267
    }
268
269
    /**
270
     * 设置是否为JSON提交
271
     * @param bool $isJson
272
     * @return $this
273
     */
274
    public function setIsJson(bool $isJson)
275
    {
276
        $this->isJson = $isJson;
277
        return $this;
278
    }
279
    
280
    /**
281
     * 判断是否为JSON
282
     *
283
     * @return boolean
284
     */
285
    private function contentIsJson()
286
    {
287
        $header = strtolower($this->request->server()['content-type'] ?? '');
288
        return null !== $header && strpos($header, 'json') !== false;
289
    }
290
}
291