1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Raptor\Request\Components; |
4
|
|
|
|
5
|
|
|
class Body |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* Request body parameters ($_POST). |
9
|
|
|
* |
10
|
|
|
* @var array |
11
|
|
|
*/ |
12
|
|
|
protected $param; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Uploaded files ($_FILES). |
16
|
|
|
* |
17
|
|
|
* @var array |
18
|
|
|
*/ |
19
|
|
|
protected $files; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Content length ($_SERVER['CONTENT_LENGTH']). |
23
|
|
|
* |
24
|
|
|
* @var array |
25
|
|
|
*/ |
26
|
|
|
protected $contentLength; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Content type ($_SERVER['CONTENT_TYPE']). |
30
|
|
|
* |
31
|
|
|
* @var array |
32
|
|
|
*/ |
33
|
|
|
protected $contentType; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Get request body parameters. |
37
|
|
|
* |
38
|
|
|
* @param string $key (optional) |
39
|
|
|
* @param mixed $default (optional) |
40
|
|
|
* @return mixed |
41
|
|
|
*/ |
42
|
|
|
public function param($key = null, $default = null) |
43
|
|
|
{ |
44
|
|
|
if ($this->param === null) { |
45
|
|
|
$this->param = $_POST; |
46
|
|
|
if ( |
47
|
|
|
isset($_SERVER['CONTENT_TYPE']) && |
48
|
|
|
$_SERVER['CONTENT_TYPE'] === 'application/x-www-form-urlencoded' |
49
|
|
|
) { |
50
|
|
|
parse_str(file_get_contents("php://input"), $this->param); |
|
|
|
|
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
if ($key === null) { |
54
|
|
|
return $this->param; |
55
|
|
|
} |
56
|
|
|
return isset($this->param[$key]) ? $this->param[$key] : $default; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Get uploaded files. |
61
|
|
|
* |
62
|
|
|
* @param string $key (optional) |
63
|
|
|
* @return array |
64
|
|
|
*/ |
65
|
|
|
public function files($key = null) |
66
|
|
|
{ |
67
|
|
|
if ($this->files === null) { |
68
|
|
|
$this->files = $_FILES; |
69
|
|
|
} |
70
|
|
|
if ($key === null) { |
71
|
|
|
return $this->files; |
72
|
|
|
} |
73
|
|
|
return isset($this->files[$key]) ? $this->files[$key] : null; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Get the content length. |
78
|
|
|
* |
79
|
|
|
* @return int |
80
|
|
|
*/ |
81
|
|
|
public function contentLength() |
82
|
|
|
{ |
83
|
|
|
if ($this->contentLength) { |
|
|
|
|
84
|
|
|
return $this->contentLength; |
|
|
|
|
85
|
|
|
} |
86
|
|
|
return $this->contentLength = isset($_SERVER['CONTENT_LENGTH']) ? (int) $_SERVER['CONTENT_LENGTH'] : null; |
|
|
|
|
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Get the content type. |
91
|
|
|
* |
92
|
|
|
* @return string |
93
|
|
|
*/ |
94
|
|
|
public function contentType() |
95
|
|
|
{ |
96
|
|
|
if ($this->contentType) { |
|
|
|
|
97
|
|
|
return $this->contentType; |
|
|
|
|
98
|
|
|
} |
99
|
|
|
return $this->contentType = isset($_SERVER['CONTENT_TYPE']) ? $_SERVER['CONTENT_TYPE'] : null; |
100
|
|
|
} |
101
|
|
|
} |