Skip to content

Instantly share code, notes, and snippets.

@xeoncross
Created December 6, 2011 19:23
Show Gist options
  • Save xeoncross/1439538 to your computer and use it in GitHub Desktop.
Save xeoncross/1439538 to your computer and use it in GitHub Desktop.
A simple, easy-to-type, default-value, input class for PHP
<?php
/**
* Here is a non-magic-method version for those that need better auto-complete and faster access times.
* It also filters the input to be valid UTF-8 just when needed thanks to phunction.
*/
class input
{
public static $encoding = 'UTF-8';
public static function server($key, $default = NULL, $control = true)
{
return isset($_SERVER[$key]) ? self::filter($_SERVER[$key], $control) : $default;
}
public static function post($key, $default = NULL, $control = true)
{
return isset($_POST[$key]) ? self::filter($_POST[$key], $control) : $default;
}
public static function get($key, $default = NULL, $control = true)
{
return isset($_GET[$key]) ? self::filter($_GET[$key], $control) : $default;
}
public static function cookie($key, $default = NULL, $control = true)
{
return isset($_COOKIE[$key]) ? self::filter($_COOKIE[$key], $control) : $default;
}
public static function session($key, $default = NULL, $control = true)
{
return isset($_SESSION[$key]) ? self::filter($_SESSION[$key], $control) : $default;
}
public static function files($key, $default = NULL, $control = true)
{
return isset($_FILES[$key]) ? self::filter($_FILES[$key], $control) : $default;
}
public static function request($key, $default = NULL, $control = true)
{
return isset($_REQUEST[$key]) ? self::filter($_REQUEST[$key], $control) : $default;
}
public static function env($key, $default = NULL, $control = true)
{
return isset($_ENV[$key]) ? self::filter($_ENV[$key], $control) : $default; // don't forget about getenv()!
}
// https://github.com/alixaxel/phunction/blob/master/phunction.php#L262
public static function filter($data, $control = true)
{
if (is_array($data) === true)
{
$result = array();
foreach ($data as $key => $value)
{
$result[self::filter($key, $control)] = self::filter($value, $control);
}
return $result;
}
else if (is_string($data) === true)
{
$encoding = static::$encoding;
if (function_exists('mb_detect_encoding') === true)
{
$encoding = mb_detect_encoding($data, 'auto');
}
// Check for non-ascii characters before we invoke the slow iconv!
if(preg_match('/[^\x00-\x7F]/S', $data))
{
$data = @iconv(($encoding === false) ? 'UTF-8' : $encoding, 'UTF-8//IGNORE', $data);
}
if($data)
{
return ($control === true) ? preg_replace('~\p{C}+~u', '', $data) : preg_replace(array('~\r\n?~', '~[^\P{C}\t\n]+~u'), array("\n", ''), $data);
}
return false;
}
return $data;
}
}
<?php
class i
{
public static function __callStatic($method, $args)
{
// Function calls are slow
//$method = '_' . strtoupper($method);
$types = array(
'session' => '_SESSION',
'post' => '_POST',
'get' => '_GET',
'server' => '_SERVER',
'files' => '_FILES',
'cookie' => '_COOKIE',
'env' => '_ENV',
'request' => '_REQUEST'
);
$method = $types[$method];
if(isset($GLOBALS[$method][$args[0]]))
{
return $GLOBALS[$method][$args[0]];
}
return isset($args[1]) ? $args[1] : NULL;
}
}
$_POST['item_1'] = 'value';
$_GET['item_1'] = 'value';
$_SESSION['item_1'] = 'value';
$_REQUEST['item_1'] = 'value';
$_ENV['item_1'] = 'value';
$_COOKIE['item_1'] = 'value';
$_FILES['item_1'] = 'value';
$GLOBALS['cool']['value'] = 'yes'; // Other test
header('Content-Type: text/plain');
$time = microtime(TRUE);
for ($i = 0; $i < 100; $i++)
{
i::server('HTTP_HOST');
i::post("item_$i");
i::get("item_$i");
i::session("item_$i");
// vs
//if(isset($_POST["item_$i"])) {}
}
print (microtime(TRUE) - $time). " ms\n";
var_dump(i::server('HTTP_HOST', 'default_if_not_found'));
var_dump(i::post("item_1", 'default_if_not_found'));
var_dump(i::get("item_1", 'default_if_not_found'));
var_dump(i::session("item_1", 'default_if_not_found'));
var_dump(i::files("item_1", 'default_if_not_found'));
var_dump(i::env("item_1", 'default_if_not_found'));
var_dump(i::cookie("item_1", 'default_if_not_found'));
var_dump(i::request("item_1", 'default_if_not_found'));
//var_dump(i::cool('value')); // FAIL! See above
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment