Passed
Push — master ( 92182b...13d26f )
by 世昌
03:14
created

FormDataAttribute   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 182
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 41
dl 0
loc 182
rs 10
c 0
b 0
f 0
wmc 25

11 Methods

Rating   Name   Duplication   Size   Complexity  
A hasJson() 0 6 3
A hasGet() 0 7 2
A post() 0 13 5
A file() 0 11 4
A isGet() 0 3 1
A isPost() 0 3 1
A json() 0 9 3
A input() 0 2 1
A get() 0 3 1
A isJson() 0 4 2
A hasPost() 0 7 2
1
<?php
2
namespace nebula\request\attribute;
3
4
use nebula\request\file\UploadedFile;
5
6
/**
7
 * HTTP请求解析器 数据
8
 *
9
 */
10
trait FormDataAttribute
11
{
12
    protected $json;
13
    protected $files;
14
15
    /**
16
     * 判断内容是否为JSON
17
     *
18
     * @return boolean
19
     */
20
    public function isJson():bool
21
    {
22
        $header = $this->getHeader('content-type');
23
        return !is_null($header) && $header->isJson();
24
    }
25
26
    /**
27
     * 判断是否是POST请求
28
     *
29
     * @return boolean
30
     */
31
    public function isPost():bool
32
    {
33
        return $this->getMethod() === 'POST';
34
    }
35
36
    /**
37
     * 判断是否为GET请求
38
     *
39
     * @return boolean
40
     */
41
    public function isGet():bool
42
    {
43
        return $this->getMethod() === 'GET';
44
    }
45
46
    /**
47
     * 获取JSON数据
48
     *
49
     * @return array|null
50
     */
51
    public function json():?array {
52
        if (isset($this->json)) {
53
            return $this->json;
54
        }
55
        $data =json_decode($this->input(), true, 512, JSON_BIGINT_AS_STRING);
56
        if (json_last_error()!==JSON_ERROR_NONE) {
57
            $this->json = null;
58
        }
59
        return $this->json = $data;
60
    }
61
   
62
    /**
63
     * 获取GET参数
64
     *
65
     * @param string|null $name
66
     * @param mixed $default
67
     * @return mixed
68
     */
69
    public function get(?string $name=null, $default =null)
70
    {
71
        return $this->getParameter($name, $default);
72
    }
73
74
    /**
75
     * 获取POST请求的值
76
     *
77
     * @param string $name
78
     * @param mixed $default
79
     * @return mixed 获取的值
80
     */
81
    public static function post(?string $name=null, $default=null)
82
    {
83
        if (is_null($name)) {
84
            return $_POST;
85
        }
86
        if (array_key_exists($name, $_POST)) {
87
            if (\is_string($_POST[$name]) && strlen($_POST[$name])) {
88
                return $_POST[$name];
89
            } else {
90
                return $_POST[$name];
91
            }
92
        }
93
        return $default;
94
    }
95
96
    /**
97
     * 获取HTTP输入
98
     *
99
     * @return string
100
     */
101
    public function input():string {
102
        return \file_get_contents('php://input');
103
    }
104
 
105
106
    /**
107
     * 获取提交的文件
108
     *
109
     * @param string $name
110
     * @return UploadedFile|null
111
     */
112
    public function file(string $name = null): ?UploadedFile {
113
        if (!isset($this->files)) {
114
            $this->files = [];
115
            foreach ($_FILES as $name => $uploaded) {
116
                $this->files[$name] = new UploadedFile($uploaded['tmp_name'], $uploaded['name'], $uploaded['type'], $uploaded['error']);
117
            }
118
        }
119
        if (\is_null($name)) {
120
            return $this->files;
121
        }
122
        return $this->files[$name] ?? null;
123
    }
124
125
    /**
126
     * 判断是否有GET请求
127
     *
128
     * @param string|null $name
129
     * @return boolean
130
     */
131
    public function hasGet(?string $name=null)
132
    {
133
        $get = $this->getParameter();
134
        if (is_null($name)) {
135
            return \array_key_exists($name, $get);
136
        }
137
        return count($get) > 0;
138
    }
139
140
  
141
142
    /**
143
     * 判断是否有JSON数据请求
144
     *
145
     * @return boolean
146
     */
147
    public function hasJson()
148
    {
149
        if ($this->isJson() && $this->json()) {
150
            return true;
151
        }
152
        return false;
153
    }
154
155
    /**
156
     * 判断是否有POST数据请求
157
     *
158
     * @return boolean
159
     */
160
    public function hasPost(?string $name=null)
161
    {
162
        $post = $this->post();
163
        if ($name) {
164
            return \array_key_exists($name, $post);
165
        }
166
        return count($post) > 0;
167
    }
168
    
169
    /**
170
     * 获取参数
171
     *
172
     * @return string
173
     */
174
    abstract public function getMethod();
175
176
    /**
177
     * 获取请求头
178
     *
179
     * @param string $name
180
     * @param mixed $default
181
     * @return mixed
182
     */
183
    abstract public function getHeader(string $name, $default =null);
184
    /**
185
     * 获取请求参数
186
     *
187
     * @param string|null $name
188
     * @param mixed $default
189
     * @return mixed
190
     */
191
    abstract public function getParameter(?string $name = null, $default = null);
192
}
193