From d9e9c1bb60342ce88be150cc05de21dabfe130b2 Mon Sep 17 00:00:00 2001 From: Andreas Gohr Date: Thu, 9 Aug 2012 23:28:11 +0200 Subject: extract method for Input class makes it easier to access our do parameters --- inc/Input.class.php | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) (limited to 'inc') diff --git a/inc/Input.class.php b/inc/Input.class.php index f4174404a..35aecdb45 100644 --- a/inc/Input.class.php +++ b/inc/Input.class.php @@ -175,6 +175,40 @@ class Input { return (array) $this->access[$name]; } + /** + * Create a simple key from an array key + * + * This is useful to access keys where the information is given as an array key or as a single array value. + * For example when the information was submitted as the name of a submit button. + * + * This function directly changes the access array. + * + * Eg. $_REQUEST['do']['save']='Speichern' becomes $_REQUEST['do'] = 'save' + * + * This function returns the $INPUT object itself for easy chaining + * + * @param $name + * @return Input + */ + public function extract($name){ + if(!isset($this->access[$name])) return $this; + if(!is_array($this->access[$name])) return $this; + $keys = array_keys($this->access[$name]); + if(!$keys){ + // this was an empty array + $this->remove($name); + return $this; + } + // get the first key + $value = array_shift($keys); + if($value === 0){ + // we had a numeric array, assume the value is not in the key + $value = array_shift($this->access[$name]); + } + + $this->set($name, $value); + return $this; + } } /** -- cgit v1.2.3