Last active
May 4, 2024 16:31
-
-
Save NandoKstroNet/0eb01e3c6062f075ff271f6ddbe362b1 to your computer and use it in GitHub Desktop.
Simple Http Request - API REST com PHP & VueJS / https://codeexperts.com.br/curso/php-api-rest
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"endereco": { | |
"cidade_id": "1", | |
"estado_id": "1", | |
"endereco": "Rua Nova", | |
"bairro": "Sao Bernardo", | |
"numero": "28", | |
"cep": "65056541" | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Code\Framework\HTTP; | |
class Request | |
{ | |
public function __construct( | |
private array $get = [], | |
private array $post = [], | |
private array $server = [] | |
) { | |
} | |
public static function criarRequest() | |
{ | |
return new static($_GET, $_POST, $_SERVER); | |
} | |
public function get($key = null) | |
{ | |
if (isset($this->get[$key])) | |
return $this->get[$key]; | |
return $this->get; | |
} | |
public function post($key = null) | |
{ | |
if (isset($this->post[$key])) | |
return $this->post[$key]; | |
return $this->post; | |
} | |
public function server($key = null) | |
{ | |
if (isset($this->server[$key])) | |
return $this->server[$key]; | |
return $this->server; | |
} | |
public function query($key = null) | |
{ | |
$query = $this->parsearURL('query'); | |
parse_str($query, $parametros); | |
if (isset($parametros[$key])) | |
return $parametros[$key]; | |
return $parametros; | |
} | |
public function uri() | |
{ | |
return $this->parsearURL('path'); | |
} | |
private function parsearURL($key = null) | |
{ | |
//Ref.: http://php.net/parse_url | |
$urlParseada = parse_url($this->server('REQUEST_URI')); | |
if (!isset($urlParseada[$key])) return null; | |
return $urlParseada[$key]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment