Last active
November 3, 2019 09:42
-
-
Save agusputra/c92bf9ad6f182ff1fa49 to your computer and use it in GitHub Desktop.
utils
This file contains hidden or 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 | |
| class Request | |
| { | |
| public static function get($input, $cleaning = true) | |
| { | |
| // global $db; | |
| $input = isset($_GET[$input]) ? $_GET[$input] : null; | |
| // if ($input && gettype($input) !== 'array') { | |
| // $input = $cleaning ? @$db->clean($input) : $input; | |
| // } | |
| if ($input) { | |
| if ($cleaning) { | |
| Utils::cleanObject($input); | |
| } | |
| } | |
| return $input; | |
| } | |
| public static function post($input, $cleaning = true) | |
| { | |
| // global $db; | |
| $input = isset($_POST[$input]) ? $_POST[$input] : null; | |
| // if ($input && gettype($input) !== 'array') { | |
| // $input = $cleaning ? @$db->clean($input) : $input; | |
| // } | |
| if ($input) { | |
| if ($cleaning) { | |
| Utils::cleanObject($input); | |
| } | |
| } | |
| return $input; | |
| } | |
| public static function isGet() | |
| { | |
| return $_SERVER['REQUEST_METHOD'] === 'GET'; | |
| } | |
| public static function isPost() | |
| { | |
| return $_SERVER['REQUEST_METHOD'] === 'POST'; | |
| } | |
| } | |
| class Response | |
| { | |
| public static function returnAndExit($responseCode) | |
| { | |
| http_response_code($responseCode); | |
| exit; | |
| } | |
| public static function json($param) | |
| { | |
| header('Content-Type: application/json'); | |
| echo json_encode($param); | |
| exit; | |
| } | |
| } | |
| class Utils | |
| { | |
| public static function clean(&$param) | |
| { | |
| //global $db; | |
| if (!is_null($param)) { | |
| if (!is_scalar($param)) { | |
| throw new Exception('param must be scalar value'); | |
| } | |
| //$param = $db->clean($param); | |
| } | |
| return $param; | |
| } | |
| public static function cleanObject(&$param) | |
| { | |
| if (is_array($param)) { | |
| foreach ($param as &$p) { | |
| self::cleanObject($p); | |
| } | |
| unset($p); | |
| } else if (is_object($param)) { | |
| $obj = get_object_vars($param); | |
| foreach ($obj as $k => $v) { | |
| if (is_array($v)) { | |
| foreach ($v as $k2 => $v2) { | |
| self::cleanObject($v2); | |
| } | |
| } elseif (is_object($v)) { | |
| self::cleanObject($v); | |
| } | |
| $obj[$k] = self::clean($v); | |
| } | |
| } else { | |
| self::clean($param); | |
| } | |
| return $param; | |
| } | |
| public static function flattenArray($source, $key = null) | |
| { | |
| $result = array(); | |
| if ($key) { | |
| array_walk_recursive($source, function ($v, $k) use ($key, &$result) { | |
| if ($k === $key) { | |
| $result[] = $v; | |
| } | |
| }); | |
| } else { | |
| array_walk_recursive($source, function ($v, $k) use (&$result) { | |
| $result[] = $v; | |
| }); | |
| } | |
| return $result; | |
| } | |
| } | |
| class Helper | |
| { | |
| private static $instance; | |
| public static $alert; | |
| public function __construct() | |
| { | |
| self::$alert = new Alert(); | |
| } | |
| public static function init() | |
| { | |
| if (is_null(self::$instance)) { | |
| self::$instance = new self(); | |
| } | |
| return self::$instance; | |
| } | |
| } | |
| class Alert | |
| { | |
| public function __call($name, $arguments) | |
| { | |
| global $globalViewAlert; | |
| switch ($name) { | |
| case 'success': { | |
| return $globalViewAlert = ['level' => 'success', 'msg' => $arguments[0]]; | |
| } | |
| case 'info': { | |
| return $globalViewAlert = ['level' => 'info', 'msg' => $arguments[0]]; | |
| } | |
| case 'warning': { | |
| return $globalViewAlert = ['level' => 'warning', 'msg' => $arguments[0]]; | |
| } | |
| case 'error': { | |
| return $globalViewAlert = ['level' => 'error', 'msg' => $arguments[0]]; | |
| } | |
| } | |
| } | |
| } | |
| (new Helper())->init(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment