summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndreas Gohr <andi@splitbrain.org>2012-08-09 23:28:11 +0200
committerAndreas Gohr <andi@splitbrain.org>2012-08-09 23:28:11 +0200
commitd9e9c1bb60342ce88be150cc05de21dabfe130b2 (patch)
tree421113cb9653d7df54a64ee61b5e06d38cf5cb82
parent80a47290a7a01f2a320d09d387eea690ce1f62b4 (diff)
downloadrpg-d9e9c1bb60342ce88be150cc05de21dabfe130b2.tar.gz
rpg-d9e9c1bb60342ce88be150cc05de21dabfe130b2.tar.bz2
extract method for Input class
makes it easier to access our do parameters
-rw-r--r--_test/tests/inc/input.test.php19
-rw-r--r--inc/Input.class.php34
2 files changed, 52 insertions, 1 deletions
diff --git a/_test/tests/inc/input.test.php b/_test/tests/inc/input.test.php
index 761b7ddbc..59b5ea4b9 100644
--- a/_test/tests/inc/input.test.php
+++ b/_test/tests/inc/input.test.php
@@ -12,7 +12,8 @@ class input_test extends DokuWikiTest {
'zero' => '0',
'one' => '1',
'empty' => '',
- 'emptya' => array()
+ 'emptya' => array(),
+ 'do' => array('save' => 'Speichern'),
);
public function test_str() {
@@ -213,4 +214,20 @@ class input_test extends DokuWikiTest {
$this->assertEquals('bla',$test);
}
+ public function test_extract(){
+ $_REQUEST = $this->data;
+ $_POST = $this->data;
+ $_GET = $this->data;
+ $INPUT = new Input();
+
+ $this->assertEquals('save', $INPUT->extract('do')->str('do'));
+ $this->assertEquals('', $INPUT->extract('emptya')->str('emptya'));
+ $this->assertEquals('foo', $INPUT->extract('string')->str('string'));
+ $this->assertEquals('foo', $INPUT->extract('array')->str('array'));
+
+ $this->assertEquals('save', $INPUT->post->extract('do')->str('do'));
+ $this->assertEquals('', $INPUT->post->extract('emptya')->str('emptya'));
+ $this->assertEquals('foo', $INPUT->post->extract('string')->str('string'));
+ $this->assertEquals('foo', $INPUT->post->extract('array')->str('array'));
+ }
}
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;
+ }
}
/**