Skip to content

Instantly share code, notes, and snippets.

@jm42
Created October 23, 2014 06:46
Show Gist options
  • Save jm42/62ef6f12749d49b02fb4 to your computer and use it in GitHub Desktop.
Save jm42/62ef6f12749d49b02fb4 to your computer and use it in GitHub Desktop.
Input Filter
<?php
// This class I used several times before:
// @see https://gist.github.com/jm42/e7e788d7e257ce1f93c7
// @see https://gist.github.com/jm42/4fc873e91b3835ade45f
class InputFilter implements \ArrayAccess {
private $input;
private $cache = [];
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];
}
return $this->cache[$offset] = filter_input($this->input, $offset);
}
public function offsetSet($offset, $value) {
$this->cache[$offset] = $value;
}
public function offsetUnset($offset) {
unset($this->cache[$offset]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment