Request::setParameter()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 4
rs 10
1
<?php
2
3
namespace suda\framework;
4
5
use suda\framework\http\UploadedFile;
6
use suda\framework\request\RequestWrapper;
7
use suda\framework\http\Request as RequestInterface;
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 RequestInterface $request
36
     */
37
    public function __construct(RequestInterface $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->getServer('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 && is_array($post)) {
206
            return array_key_exists($name, $post);
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
        if ($name === null) {
221
            return $this->request->post();
222
        }
223
        return $this->request->post()[$name] ?? $default;
224
    }
225
226
    /**
227
     * 获取GET参数
228
     *
229
     * @param string|null $name
230
     * @param mixed $default
231
     * @return mixed
232
     */
233
    public function get(?string $name = null, $default = null)
234
    {
235
        return $this->getQuery($name, $default);
236
    }
237
238
    /**
239
     * 获取Cookie
240
     *
241
     * @param string $name
242
     * @param mixed $default
243
     * @return mixed
244
     */
245
    public function cookie(string $name, $default = null)
246
    {
247
        return $_COOKIE[$name] ?? $default;
248
    }
249
250
    /**
251
     * 获取URI中的参数
252
     * @param string|null $name
253
     * @param mixed $default
254
     * @return mixed
255
     */
256
    public function getParameter(?string $name = null, $default = null)
257
    {
258
        return $name === null ? $this->parameter : $this->parameter[$name] ?? $default;
259
    }
260
261
    /**
262
     * 设置URI中的参数
263
     * @param array $parameter
264
     * @return $this
265
     */
266
    public function setParameter(array $parameter)
267
    {
268
        $this->parameter = $parameter;
269
        return $this;
270
    }
271
272
    /**
273
     * 设置是否为JSON提交
274
     * @param bool $isJson
275
     * @return $this
276
     */
277
    public function setIsJson(bool $isJson)
278
    {
279
        $this->isJson = $isJson;
280
        return $this;
281
    }
282
283
    /**
284
     * 判断是否为JSON
285
     *
286
     * @return boolean
287
     */
288
    private function contentIsJson()
289
    {
290
        $header = strtolower($this->getServer('content-type', ''));
291
        return null !== $header && strpos($header, 'json') !== false;
292
    }
293
}
294