forked from estadao-digital/test-dev
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRequest.php
More file actions
49 lines (38 loc) · 1.32 KB
/
Request.php
File metadata and controls
49 lines (38 loc) · 1.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<?php
include_once 'IRequest.php';
class Request implements IRequest {
function __construct(){
$this->preencheRequest();
}
//seta todas as keys no array $_SERVER como propriedades da classe Request
private function preencheRequest(){
foreach($_SERVER as $key => $value){
$this->{$this->toCamelCase($key)} = $value;
}
}
//equivalente a parameterize() do ruby
private function toCamelCase($string){
$result = strtolower($string);
preg_match_all('/_[a-z]/', $result, $matches);
foreach($matches[0] as $match){
$c = str_replace('_', '', strtoupper($match));
$result = str_replace($match, $c, $result);
}
return $result;
}
//pega o corpo(os parâmetros) da requisição
public function getBody(){
if($this->requestMethod === "GET"){
return;
}
if ($this->requestMethod == "POST"){
$body = array();
$_POST = json_decode(file_get_contents("php://input"),true);
foreach($_POST as $key => $value){
$body[$key] = $value;
}
return $body;
}
}
}
?>