diff options
author | Anika Henke <anika@selfthinker.org> | 2012-06-29 17:51:09 +0100 |
---|---|---|
committer | Anika Henke <anika@selfthinker.org> | 2012-06-29 17:51:09 +0100 |
commit | 0c06a181819249c6a4a2a6c60e13f739df1f2253 (patch) | |
tree | 859377c572d0acbfc520b02304ef515bf3aebbe0 /inc/Input.class.php | |
parent | ef7e36e4fd2a168977754f0aac1d855fb651f104 (diff) | |
parent | 5d0aaf958325f500ce69cfb79e69eb0d8f83fdeb (diff) | |
download | rpg-0c06a181819249c6a4a2a6c60e13f739df1f2253.tar.gz rpg-0c06a181819249c6a4a2a6c60e13f739df1f2253.tar.bz2 |
Merge branch 'master' of github.com:splitbrain/dokuwiki into frontend_improvements
Conflicts:
lib/tpl/dokuwiki/css/basic.css
Diffstat (limited to 'inc/Input.class.php')
-rw-r--r-- | inc/Input.class.php | 229 |
1 files changed, 229 insertions, 0 deletions
diff --git a/inc/Input.class.php b/inc/Input.class.php new file mode 100644 index 000000000..f4174404a --- /dev/null +++ b/inc/Input.class.php @@ -0,0 +1,229 @@ +<?php + +/** + * Encapsulates access to the $_REQUEST array, making sure used parameters are initialized and + * have the correct type. + * + * All function access the $_REQUEST array by default, if you want to access $_POST or $_GET + * explicitly use the $post and $get members. + * + * @author Andreas Gohr <andi@splitbrain.org> + */ +class Input { + + /** @var PostInput Access $_POST parameters */ + public $post; + /** @var GetInput Access $_GET parameters */ + public $get; + + protected $access; + + /** + * Intilizes the Input class and it subcomponents + */ + function __construct() { + $this->access = &$_REQUEST; + $this->post = new PostInput(); + $this->get = new GetInput(); + } + + /** + * Check if a parameter was set + * + * Basically a wrapper around isset. When called on the $post and $get subclasses, + * the parameter is set to $_POST or $_GET and to $_REQUEST + * + * @see isset + * @param string $name Parameter name + * @return bool + */ + public function has($name) { + return isset($this->access[$name]); + } + + /** + * Remove a parameter from the superglobals + * + * Basically a wrapper around unset. When NOT called on the $post and $get subclasses, + * the parameter will also be removed from $_POST or $_GET + * + * @see isset + * @param string $name Parameter name + * @return bool + */ + public function remove($name) { + if(isset($this->access[$name])) { + unset($this->access[$name]); + } + // also remove from sub classes + if(isset($this->post) && isset($_POST[$name])) { + unset($_POST[$name]); + } + if(isset($this->get) && isset($_GET[$name])) { + unset($_GET[$name]); + } + } + + /** + * Access a request parameter without any type conversion + * + * @param string $name Parameter name + * @param mixed $default Default to return if parameter isn't set + * @param bool $nonempty Return $default if parameter is set but empty() + * @return mixed + */ + public function param($name, $default = null, $nonempty = false) { + if(!isset($this->access[$name])) return $default; + if($nonempty && empty($this->access[$name])) return $default; + return $this->access[$name]; + } + + /** + * Sets a parameter + * + * @param string $name Parameter name + * @param mixed $value Value to set + */ + public function set($name, $value) { + $this->access[$name] = $value; + } + + /** + * Get a reference to a request parameter + * + * This avoids copying data in memory, when the parameter is not set it will be created + * and intialized with the given $default value before a reference is returned + * + * @param string $name Parameter name + * @param mixed $default If parameter is not set, initialize with this value + * @param bool $nonempty Init with $default if parameter is set but empty() + * @return &mixed + */ + public function &ref($name, $default = '', $nonempty = false) { + if(!isset($this->access[$name]) || ($nonempty && empty($this->access[$name]))) { + $this->set($name, $default); + } + + return $this->access[$name]; + } + + /** + * Access a request parameter as int + * + * @param string $name Parameter name + * @param mixed $default Default to return if parameter isn't set or is an array + * @param bool $nonempty Return $default if parameter is set but empty() + * @return int + */ + public function int($name, $default = 0, $nonempty = false) { + if(!isset($this->access[$name])) return $default; + if(is_array($this->access[$name])) return $default; + if($this->access[$name] === '') return $default; + if($nonempty && empty($this->access[$name])) return $default; + + return (int) $this->access[$name]; + } + + /** + * Access a request parameter as string + * + * @param string $name Parameter name + * @param mixed $default Default to return if parameter isn't set or is an array + * @param bool $nonempty Return $default if parameter is set but empty() + * @return string + */ + public function str($name, $default = '', $nonempty = false) { + if(!isset($this->access[$name])) return $default; + if(is_array($this->access[$name])) return $default; + if($nonempty && empty($this->access[$name])) return $default; + + return (string) $this->access[$name]; + } + + /** + * Access a request parameter as bool + * + * Note: $nonempty is here for interface consistency and makes not much sense for booleans + * + * @param string $name Parameter name + * @param mixed $default Default to return if parameter isn't set + * @param bool $nonempty Return $default if parameter is set but empty() + * @return bool + */ + public function bool($name, $default = false, $nonempty = false) { + if(!isset($this->access[$name])) return $default; + if(is_array($this->access[$name])) return $default; + if($this->access[$name] === '') return $default; + if($nonempty && empty($this->access[$name])) return $default; + + return (bool) $this->access[$name]; + } + + /** + * Access a request parameter as array + * + * @param string $name Parameter name + * @param mixed $default Default to return if parameter isn't set + * @param bool $nonempty Return $default if parameter is set but empty() + * @return array + */ + public function arr($name, $default = array(), $nonempty = false) { + if(!isset($this->access[$name])) return $default; + if(!is_array($this->access[$name])) return $default; + if($nonempty && empty($this->access[$name])) return $default; + + return (array) $this->access[$name]; + } + +} + +/** + * Internal class used for $_POST access in Input class + */ +class PostInput extends Input { + protected $access; + + /** + * Initialize the $access array, remove subclass members + */ + function __construct() { + $this->access = &$_POST; + } + + /** + * Sets a parameter in $_POST and $_REQUEST + * + * @param string $name Parameter name + * @param mixed $value Value to set + */ + public function set($name, $value) { + parent::set($name, $value); + $_REQUEST[$name] = $value; + } +} + +/** + * Internal class used for $_GET access in Input class + + */ +class GetInput extends Input { + protected $access; + + /** + * Initialize the $access array, remove subclass members + */ + function __construct() { + $this->access = &$_GET; + } + + /** + * Sets a parameter in $_GET and $_REQUEST + * + * @param string $name Parameter name + * @param mixed $value Value to set + */ + public function set($name, $value) { + parent::set($name, $value); + $_REQUEST[$name] = $value; + } +} |