I like the idea of โโcreating an InputFilter class that implements ArrayAccess. This is more object oriented and customizable because you can add methods as you like for customization and work with the same main filtering object.
$get = new InputFilter($_GET); echo $get->value_integer('variable_name');
What is also nice is that it is reused for $ _POST, etc. You just need to do something like $post = new InputFilter($_POST); . And you can also use it for other input sources.
Or, if you have a fairly new version of php, you can later implement filter_input (), as suggested by @Arkh. IMO, having its own class, feels much more reusable and durable.
<?php // empty for now, fill in later if desired class InputFilterException extends Exception {} /* * Use the ArrayAccess interface as a template. * * Usage examples: * $controller->get = InputFilter($_GET); * echo $controller->get->value_string_html('variable'); * $controller->post = InputFilter($_POST); * echo $controller->get->value_integer('variable'); */ class InputFilter implements ArrayAccess { protected $data; function __construct( $data ) { if( !is_array($data) ) { throw new InputFilterException ("Only arrays are allowed here"); } $this->data = $data; } // do not actually use these function __get( $offset ) { throw new InputFilterException( "Don't use as an array, use functions ->string() ->int() etc: ['" . $offset . "']" ); } function __set( $offset, $value ) { throw new InputFilterException( "Don't modify directly: ['" . $offset . "'] = \"" . $value . "\"" ); } // implement ArrayAccess function offsetExists( $offset ) { return isset( $this->data[$offset]) ); } function offsetSet( $offset, $value ) { $this->data[$offset] = $value; } function offsetUnset( $offset ) { unset( $this->data[$offset] ); } function offsetGet( $offset ) { throw new InputFilterException ("Don't use this object as an array, but were an array : ". $offset); } protected function getValue( $offset ) { if( is_array($this->data[$offset]) ) { throw new InputFilterException ("must use the asArray() function"); } return $this->data[$offset]; } function data_count() { return count($this->data); } public function set_value( $offset, $data ) { $this->offsetSet( $offset, $data ); } // get an array *in* the data public function asArray($offset) { if( !is_array ($this->data[$offset]) ) { throw new InputFilterException("only use asArray() for arrays"); } return new Filter( $this->data[$offset] ); } // validators... function is_set( $offset ) { return $this->offsetExists($offset); } function is_empty( $offset ) { return $this->is_set($offset) && strlen($this->data[$offset]) == 0; } function is_numeric( $offset ) { return $this->is_set($offset) && is_numeric($this->data[$offset]); } function is_integer( $offset ) { if( !$this->is_set($offset) ) { return false; } elseif( is_numeric($this->data[$offset]) ) { $int_value = intval($this->data[$offset]); return $int_value == $this->data[$offset]; } elseif( strlen($this->data[$offset]) == 0 ) { return true; } return false; } function is_array( $offset ) { return $this->is_set($offset) && is_array($this->data[$offset]); } // return data formatted function value_string( $offset ) { return $this->getValue($offset); } function value_string_html( $offset ) { return htmlentities( $this->getValue($offset), null, 'UTF-8' ); } function value_integer( $offset ) { return intval( trim($this->getValue ($offset)) ); } function value_numeric( $offset ) { return doubleval($this->getValue ($offset)); } function value_alphanumeric( $offset ) { return preg_replace("*[^A-Za-z0-9]*", "", $this->getValue ($offset)); } function value_unfiltered( $offset ) { return $this->getValue( $offset ); } } ?>
snarkback
source share