Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
46 / 46 |
|
100.00% |
15 / 15 |
CRAP | |
100.00% |
1 / 1 |
| Request | |
100.00% |
46 / 46 |
|
100.00% |
15 / 15 |
31 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
4 | |||
| get | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
2 | |||
| server | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
2 | |||
| httpMethod | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| ip | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
4 | |||
| setHeader | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| header | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 | |||
| cookie | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
2 | |||
| body | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| setBody | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| bodyAsJson | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
3 | |||
| uploadedFile | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
2 | |||
| createUploadedFiles | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
3 | |||
| createUploadedFilesFromArray | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
2 | |||
| createUploadedFile | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Dynart\Micro; |
| 4 | |
| 5 | /** |
| 6 | * Represents the HTTP request |
| 7 | * |
| 8 | * It can be used for getting the information of the HTTP request: the request method (POST, GET, etc.), |
| 9 | * the query parameters, the headers, the information that created by the web server, the cookies |
| 10 | * and the uploaded files (@see UploadedFile). |
| 11 | * |
| 12 | * @package Dynart\Micro |
| 13 | */ |
| 14 | class Request { |
| 15 | |
| 16 | /** |
| 17 | * The incoming HTTP request headers. |
| 18 | * |
| 19 | * @var array |
| 20 | */ |
| 21 | protected $headers = []; |
| 22 | |
| 23 | /** |
| 24 | * The incoming uploaded files. |
| 25 | * |
| 26 | * @var array |
| 27 | */ |
| 28 | protected $uploadedFiles = []; |
| 29 | |
| 30 | /** |
| 31 | * Stores the request body |
| 32 | * @var string |
| 33 | */ |
| 34 | protected $body; |
| 35 | |
| 36 | /** |
| 37 | * The Request constructor |
| 38 | * |
| 39 | * Fills up the `headers` and the `uploadedFiles` arrays. |
| 40 | * |
| 41 | * @see Request::$headers |
| 42 | * @see Request::$uploadedFiles |
| 43 | */ |
| 44 | public function __construct() { |
| 45 | $headers = function_exists('getallheaders') ? getallheaders() : ['x-test-header' => 'test-value']; |
| 46 | foreach ($headers as $key => $value) { |
| 47 | $this->headers[strtolower($key)] = $value; |
| 48 | } |
| 49 | if (!empty($_FILES)) { |
| 50 | $this->createUploadedFiles(); |
| 51 | } |
| 52 | $this->body = file_get_contents('php://input'); |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Returns with a parameter of the request, uses the $_REQUEST array |
| 57 | * |
| 58 | * @param string $name The name of the parameter |
| 59 | * @param mixed|null $default The default value if the parameter doesn't present |
| 60 | * @return mixed|null The value of the parameter or the default value |
| 61 | */ |
| 62 | public function get(string $name, $default = null) { |
| 63 | return array_key_exists($name, $_REQUEST) ? $_REQUEST[$name] : $default; |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Returns with the information that was created by the web server, uses the $_SERVER array |
| 68 | * |
| 69 | * @param string $name The name of the server info |
| 70 | * @param mixed|null $default The default value if the info doesn't present |
| 71 | * @return mixed|null The info created by the web server or the default value |
| 72 | */ |
| 73 | public function server(string $name, $default = null) { |
| 74 | return array_key_exists($name, $_SERVER) ? $_SERVER[$name] : $default; |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Returns the HTTP request method |
| 79 | * |
| 80 | * @return string The request method. Can be GET, POST, PUT, OPTIONS, PATCH, DELETE |
| 81 | */ |
| 82 | public function httpMethod(): string { |
| 83 | return $this->server('REQUEST_METHOD'); |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Returns with the IP of the client if present, otherwise null |
| 88 | * |
| 89 | * Important! This value can't be trusted, this is just for hashing/logging purposes. |
| 90 | * |
| 91 | * @return string|null |
| 92 | */ |
| 93 | public function ip() { |
| 94 | if (!empty($_SERVER['HTTP_CLIENT_IP'])) { |
| 95 | return $_SERVER['HTTP_CLIENT_IP']; |
| 96 | } else if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){ |
| 97 | return $_SERVER['HTTP_X_FORWARDED_FOR']; |
| 98 | } else if (!empty($_SERVER['REMOTE_ADDR'])) { |
| 99 | return $_SERVER['REMOTE_ADDR']; |
| 100 | } |
| 101 | return null; |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Sets a request header |
| 106 | * |
| 107 | * @param string $name The name of the header |
| 108 | * @param string $value The value of the header |
| 109 | */ |
| 110 | public function setHeader(string $name, string $value) { |
| 111 | $this->headers[strtolower($name)] = $value; |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Returns with a request header by name |
| 116 | * |
| 117 | * @param string $name The header name |
| 118 | * @param mixed|null $default The default value if the header doesn't present |
| 119 | * @return mixed|null The header value or the default value |
| 120 | */ |
| 121 | public function header(string $name, $default = null) { |
| 122 | $lowerName = strtolower($name); |
| 123 | return isset($this->headers[$lowerName]) ? $this->headers[$lowerName] : $default; |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Returns with a cookie value by name |
| 128 | * |
| 129 | * @param string $name The name of the cookie |
| 130 | * @param mixed|null $default The default value if the cookie doesn't present |
| 131 | * @return mixed|null The cookie value or the default value |
| 132 | */ |
| 133 | public function cookie(string $name, $default = null) { |
| 134 | return array_key_exists($name, $_COOKIE) ? $_COOKIE[$name] : $default; |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * Returns with the request body |
| 139 | * |
| 140 | * @return bool|string The request body |
| 141 | */ |
| 142 | public function body() { |
| 143 | return $this->body; |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * Sets the request body |
| 148 | * |
| 149 | * @param string $content |
| 150 | */ |
| 151 | public function setBody(string $content) { |
| 152 | $this->body = $content; |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * Returns with the request body as an associative array parsed from JSON |
| 157 | * |
| 158 | * Throws an MicroException if the JSON is invalid. |
| 159 | * |
| 160 | * @throws MicroException |
| 161 | * @return mixed|null Returns with an associative array from parsed JSON or null if the request body is empty |
| 162 | */ |
| 163 | public function bodyAsJson() { |
| 164 | $json = $this->body(); |
| 165 | if (!$json) { |
| 166 | return null; |
| 167 | } |
| 168 | $result = json_decode($json, true); |
| 169 | if ($result) { |
| 170 | return $result; |
| 171 | } |
| 172 | throw new MicroException("The request body is not a valid JSON: ".$json); |
| 173 | } |
| 174 | |
| 175 | /** |
| 176 | * Returns with the uploaded file by parameter name |
| 177 | * |
| 178 | * If the parameter not present it will return with a null. |
| 179 | * if only one file uploaded it will return with an UploadedFile instance |
| 180 | * If the more than one file uploaded it will return with an array. |
| 181 | * |
| 182 | * @param string $name The name of the parameter of the POST request |
| 183 | * @return UploadedFile|UploadedFile[]|null Returns the UploadedFile instance or array or null |
| 184 | */ |
| 185 | public function uploadedFile(string $name) { |
| 186 | return isset($this->uploadedFiles[$name]) ? $this->uploadedFiles[$name] : null; |
| 187 | } |
| 188 | |
| 189 | /** |
| 190 | * It will fill up the `uploadedFiles` array |
| 191 | */ |
| 192 | protected function createUploadedFiles() { |
| 193 | foreach ($_FILES as $name => $file) { |
| 194 | if (is_array($file['name'])) { |
| 195 | $this->createUploadedFilesFromArray($name, $file); |
| 196 | } else { |
| 197 | $this->uploadedFiles[$name] = $this->createUploadedFile($file); |
| 198 | } |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | /** |
| 203 | * It will create an UploadedFile array by parameter name and puts into the `uploadedFiles` array |
| 204 | * |
| 205 | * @param string $name The name of the parameter |
| 206 | * @param array $file One element of the $_FILES array |
| 207 | */ |
| 208 | protected function createUploadedFilesFromArray($name, $file) { |
| 209 | $this->uploadedFiles[$name] = []; |
| 210 | foreach (array_keys($file['name']) as $index) { |
| 211 | $this->uploadedFiles[$name][$index] = $this->createUploadedFile([ |
| 212 | 'name' => $file['name'][$index], |
| 213 | 'tmp_name' => $file['tmp_name'][$index], |
| 214 | 'error' => $file['error'][$index], |
| 215 | 'type' => $file['type'][$index], |
| 216 | 'size' => $file['size'][$index] |
| 217 | ]); |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | /** |
| 222 | * It will create an UploadedFile instance by an array (one element from the $_FILES) |
| 223 | * |
| 224 | * @param array $file One element of the $_FILES array |
| 225 | * @return UploadedFile The UploadedFile instance |
| 226 | */ |
| 227 | protected function createUploadedFile(array $file): UploadedFile { |
| 228 | return Micro::create(UploadedFile::class, [ |
| 229 | $file['name'], $file['tmp_name'], $file['error'], $file['type'], $file['size'] |
| 230 | ]); |
| 231 | } |
| 232 | |
| 233 | } |