Created
April 1, 2020 17:42
-
-
Save osnipezzini/6c393b225aa06348c4f9eabc6c03c7d3 to your computer and use it in GitHub Desktop.
Parsing Requests
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
// basic GET param with no validation | |
$blah = Request::get('blah'); | |
// GET param with validation | |
$blah = Request::get('blah', Request::TYPE_DIGIT); | |
// POST param | |
$blah = Request::get('blah', Request::POST); | |
// POST param with validation | |
$blah = Request::get('blah', Request::POST, Request::TYPE_ALNUM); |
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 | |
{ | |
const TYPE_NONE = 0; | |
const TYPE_ALPHA = 1; | |
const TYPE_DIGIT = 2; | |
const TYPE_ALNUM = 3; | |
const GET = 10; | |
const POST = 11; | |
const COOKIE = 12; | |
const SESSION = 13; | |
public static function get($name, $method = null, $validation = null) | |
{ | |
if ($method < self::GET && $validation === null) { | |
$validation = $method; | |
$method = self::GET; | |
} else if ($method === null ) { | |
$method = self::GET; | |
} | |
if ($validation === null) { | |
$validation = self::TYPE_NONE; | |
} | |
$holder = null; | |
switch ($method) { | |
case self::GET: | |
$holder = $_GET; | |
break; | |
case self::POST: | |
$holder = $_POST; | |
break; | |
case self::COOKIE: | |
$holder = $_COOKIE; | |
break; | |
case self::SESSION: | |
$holder = $_SESSION; | |
break; | |
} | |
if (!isset($holder[$name])) { | |
return false; | |
} | |
$validator = null; | |
switch ($validation) { | |
case self::TYPE_ALNUM: | |
$validator = 'alnum'; | |
break; | |
case self::TYPE_DIGIT: | |
$validator = 'digit'; | |
break; | |
case self::TYPE_ALPHA: | |
$validator = 'alpha'; | |
break; | |
} | |
$ret_val = $holder[$name]; | |
$valid_func = 'ctype_' . $validator; | |
return (($validator === null) ? $ret_val : (( $valid_func($ret_val) ) ? $ret_val : null ) ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment