Skip to content

Instantly share code, notes, and snippets.

@jm42
Last active August 29, 2015 14:06
Show Gist options
  • Save jm42/4fc873e91b3835ade45f to your computer and use it in GitHub Desktop.
Save jm42/4fc873e91b3835ade45f to your computer and use it in GitHub Desktop.
Array Filter Input
<?php
class ArrayFilterInput implements \ArrayAccess
{
protected $input;
protected $cache = array();
public function __construct($input)
{
$this->input = $input;
}
public function offsetExists($offset)
{
if (array_key_exists($offset, $this->cache)) {
return true;
}
return filter_has_var($this->input, $offset);
}
public function offsetGet($offset)
{
if (array_key_exists($offset, $this->cache)) {
return $this->cache[$offset];
}
$value = filter_input($this->input, $offset);
if ($value === false) {
throw new \LogicException('Error');
}
return $this->cache[$offset] = $value;
}
public function offsetSet($offset, $value)
{
$this->cache[$offset] = $value;
}
public function offsetUnset($offset)
{
unset($this->cache[$offset]);
}
}
<?php
class ArrayFilterInputTest extends \PHPUnit_Framework_TestCase
{
public function getMockForArrayFilterInput($input)
{
$object = $this->getMockForAbstractClass('ArrayFilterInput');
$reflection = new ReflectionClass($object);
$reflectionProperty = $reflection->getProperty('input');
$reflectionProperty->setAccessible(true);
$reflectionProperty->setValue($object, $input);
return $object;
}
public function testExists()
{
$server = $this->getMockForArrayFilterInput(INPUT_SERVER);
$this->assertFalse(isset($server['NOT_FOUND']));
$this->assertTrue(isset($server['PHP_SELF']));
}
public function testGet()
{
$server = $this->getMockForArrayFilterInput(INPUT_SERVER);
$this->assertEquals($_SERVER['PHP_SELF'], $server['PHP_SELF']);
$this->assertEquals($_SERVER['SCRIPT_NAME'], $server['SCRIPT_NAME']);
}
public function testSet()
{
$server = $this->getMockForArrayFilterInput(INPUT_SERVER);
$server['NEW_VALUE'] = 'Good Morning PHP';
$this->assertEquals('Good Morning PHP', $server['NEW_VALUE']);
}
public function testUnset()
{
$server = $this->getMockForArrayFilterInput(INPUT_SERVER);
$server['NEW_VALUE'] = 'Good Morning PHP';
unset($server['PHP_SELF']);
unset($server['NEW_VALUE']);
$this->assertTrue(isset($server['PHP_SELF']));
$this->assertFalse(isset($server['NEW_VALUE']));
$this->assertNull($server['NEW_VALUE']);
}
}
<?php
/**
* When PHP INI directive `variables_order` is set to "" [0] then the
* superglobals are disabled but filter_input is still usable to
* retrieve it's values.
*
* [0]: https://bugs.php.net/bug.php?id=68008
*/
require_once __DIR__ . '/ArrayFilterInput.php';
$_GET = new ArrayFilterInput(INPUT_GET);
$_POST = new ArrayFilterInput(INPUT_POST);
$_REQUEST = new ArrayFilterInput(INPUT_REQUEST);
//$_FILES = null;
$_COOKIE = new ArrayFilterInput(INPUT_COOKIE);
$_SESSION = new ArrayFilterInput(INPUT_SESSION);
$_SERVER = new ArrayFilterInput(INPUT_SERVER);
$_ENV = new ArrayFilterInput(INPUT_ENV);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment