From 5d0aaf958325f500ce69cfb79e69eb0d8f83fdeb Mon Sep 17 00:00:00 2001 From: Andreas Gohr <andi@splitbrain.org> Date: Thu, 28 Jun 2012 17:17:24 +0200 Subject: [PATCH] treat empty string inputs as unset for int and bool --- _test/tests/inc/input.test.php | 12 +++++++++++- inc/Input.class.php | 5 ++++- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/_test/tests/inc/input.test.php b/_test/tests/inc/input.test.php index 627af3a2b..761b7ddbc 100644 --- a/_test/tests/inc/input.test.php +++ b/_test/tests/inc/input.test.php @@ -95,6 +95,11 @@ class input_test extends DokuWikiTest { $this->assertSame(1, $INPUT->get->int('get', false)); $this->assertSame(0, $INPUT->int('array')); + + $this->assertSame(0, $INPUT->int('zero', -1)); + $this->assertSame(-1, $INPUT->int('empty', -1)); + $this->assertSame(-1, $INPUT->int('zero', -1, true)); + $this->assertSame(-1, $INPUT->int('empty', -1, true)); } public function test_arr() { @@ -155,6 +160,11 @@ class input_test extends DokuWikiTest { $this->assertSame(false, $INPUT->post->bool('get')); $this->assertSame(true, $INPUT->post->bool('post')); + + $this->assertSame(false, $INPUT->bool('zero', -1)); + $this->assertSame(-1, $INPUT->bool('empty', -1)); + $this->assertSame(-1, $INPUT->bool('zero', -1, true)); + $this->assertSame(-1, $INPUT->bool('empty', -1, true)); } public function test_remove() { @@ -203,4 +213,4 @@ class input_test extends DokuWikiTest { $this->assertEquals('bla',$test); } -} \ No newline at end of file +} diff --git a/inc/Input.class.php b/inc/Input.class.php index 1ea5e031f..f4174404a 100644 --- a/inc/Input.class.php +++ b/inc/Input.class.php @@ -118,6 +118,7 @@ class Input { 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]; @@ -151,6 +152,8 @@ class Input { */ 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]; @@ -223,4 +226,4 @@ class GetInput extends Input { parent::set($name, $value); $_REQUEST[$name] = $value; } -} \ No newline at end of file +} -- GitLab