Advertisement
Guest User

Untitled

a guest
Jun 20th, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.31 KB | None | 0 0
  1. <?php
  2.  
  3. class Request implements IRequest
  4. {
  5.     private $supportedHttpMethods = array(
  6.         "GET",
  7.         "POST",
  8.         "DELETE",
  9.         "PUT",
  10.         "PATCH",
  11.     );
  12.  
  13.     function __construct(){
  14.         $this->bootstrap();
  15.         $this->thisURL = $this->getUrl();
  16.     }
  17.     private function bootstrap(){
  18.         //echo "<pre>";
  19.         foreach($_SERVER as $key => $value){
  20.             $this->{$this->toCamelCase($key)} = $value;
  21.             //echo "<br>[$key] $value";
  22.         }
  23.         //echo "<pre>";
  24.  
  25.         if($this->requestMethod=="POST"){
  26.             if(isset($_POST["_method"]) && in_array(strtoupper($_POST['_method']), $this->supportedHttpMethods)){
  27.                 $this->requestMethod=strtoupper($_POST['_method']);
  28.             }
  29.         }
  30.     }
  31.     private function toCamelCase($string){
  32.         $result = strtolower($string);
  33.         preg_match_all('/_[a-z]/', $result, $matches);
  34.         foreach($matches[0] as $match){
  35.             $c = str_replace('_', '', strtoupper($match));
  36.             $result = str_replace($match, $c, $result);
  37.         }
  38.         return $result;
  39.     }
  40.     public function getUrl(bool $explode = false){
  41.         if(isset($this->requestUri)){
  42.             $url = rtrim($this->requestUri, '/');
  43.             $url = filter_var($url, FILTER_SANITIZE_URL);
  44.  
  45.             $url = strstr($url, '?', true)? strstr($url, '?', true) : $url;
  46.             $url = explode('/', $url);
  47.  
  48.             while(isset($url[0]) && ($url[0]==='' || $url[0]==='GorajEsthetic' || $url[0]==="index.php")){
  49.                 array_shift($url);
  50.             }
  51.  
  52.             if($explode) {
  53.                 if(!isset($url[0])) $url[0] = '/';
  54.                 return $url;
  55.             }
  56.             $url = implode('/', $url);
  57.             $url = '/'.$url;
  58.  
  59.             return $url;
  60.         }
  61.     }
  62.     public function getBody(){
  63.         if($this->requestMethod === "GET"){
  64.             $body = array();
  65.             foreach ($_GET as $key => $value){
  66.                 $body[$key] = filter_input(INPUT_GET, $key, FILTER_SANITIZE_SPECIAL_CHARS);
  67.             }
  68.             return $body;
  69.         }
  70.         if ($this->requestMethod == "POST" || $this->requestMethod == "DELETE" || $this->requestMethod == "PUT" || $this->requestMethod == "PATCH"){
  71.             $body = array();
  72.             foreach($_POST as $key => $value){
  73.                 if(is_array($value)) $body[$key] = filter_input(INPUT_POST, $key, FILTER_SANITIZE_SPECIAL_CHARS, FILTER_REQUIRE_ARRAY);
  74.                 else{
  75.                     $body[$key] = filter_input(INPUT_POST, $key, FILTER_SANITIZE_SPECIAL_CHARS);
  76.                 }
  77.             }
  78.             return $body;
  79.         }
  80.     }
  81.     public function getPatch(){
  82.         if($this->requestMethod ==="PATCH" || $this->getRequestMethod() ==="DELETE"){
  83.             parse_str(file_get_contents('php://input'), $_PATCH);
  84.             return $_PATCH;
  85.  
  86.             $body = array();
  87.             foreach($_PATCH as $key => $value){
  88.                 $body[$key] = $value;
  89.             }
  90.             return $body;
  91.         }
  92.     }
  93.     public function setRequestMethod(string $method) :void
  94.     {
  95.         $_SERVER["REQUEST_METHOD"] = $method;
  96.         $this->requestMethod = $method;
  97.     }
  98.  
  99.     public function getRequestMethod() :string
  100.     {
  101.         return $this->requestMethod;
  102.     }
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement