Skip to content

Instantly share code, notes, and snippets.

@gaibz
Created February 15, 2020 04:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gaibz/710ea29c89240d05ad4518f5dc05532d to your computer and use it in GitHub Desktop.
Save gaibz/710ea29c89240d05ad4518f5dc05532d to your computer and use it in GitHub Desktop.
Common Helper for Codeigniter4
<?php
/**
* I made this file just for make things easier in Codeigniter 4
* Codeigniter4 is also support this function by default, but i don't know it feels like i need to make my own
*
*
* @package Gaibz_Helper
* @subpackage Helper
* @category Helper
* @author Herlangga Sefani Wijaya <https://github.com/gaibz> | 6 Feb 2020
*/
/**
* Check Wether input array isset and not empty, return empty string if array is not set and empty / null
*
*
* @param array $var = Array Variable
* @param string $index = Index of Array Variable
* @return array value of array or empty string if not exists
*/
function empty_if_unset(array $var, $index){
return (isset($var[$index]) && !empty($var[$index])) ? $var[$index] : "";
}
/**
* Check Wether input array isset and not empty, return Boolean
*
*
* @param array $var = Array Variable
* @param string $index = Index of Array Variable
* @return boolean
*/
function isset_not_empty(array $var, $index){
return (isset($var[$index]) && !empty($var[$index])) ? true : false;
}
/**
* Get HTTP INPUT from POST, GET, REQUEST, SERVER, PUT, PATCH, DELETE, or AUTO for autodetect
* This will become handy for building RESTful API.
* AUTO will search from HTTP REQUEST METHOD param
* return empty if not set
*
* <code>
* $input = get_http_input("username", "AUTO");
* $input = get_http_input("username", "auto");
* </code>
*
* @param string $req_var = Request Variable
* @param string $req_method = Request Method(POST/GET/REQUEST/SERVER/PUT/PATCH/DELETE/AUTO) default AUTO
* @return string empty string if no request variable registered
*
* @example see above
*/
$HTTP_INPUT = []; // for save INPUT REQUEST
function get_http_input(string $req_var, string $req_method="AUTO"){
$req_method = strtoupper($req_method);
if(count($HTTP_INPUT) === 0){
parse_str(file_get_contents("php://input"), $data);
$HTTP_INPUT = $data;
}
if($req_method === "AUTO"){
$req_method = $_SERVER['REQUEST_METHOD'];
}
if($req_method === "GET"){
return empty_if_unset($_GET, $req_var);
}
else if($req_method === "POST"){
return empty_if_unset($_POST, $req_var);
}
else if($req_method === "REQUEST"){
return empty_if_unset($_REQUEST, $req_var);
}
else if($req_method === "SERVER" || $req_method === "HEADER"){
$req_var = strtoupper($req_var);
$req_var = str_replace("-","_", $req_var);
return empty_if_unset($_SERVER, "HTTP_".$req_var);
}
else if($req_method === "PUT" || $req_method === "PATCH" || $req_method === "DELETE"){
return empty_if_unset($HTTP_INPUT, $req_var);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment