Skip to content

Commit

Permalink
treat empty string inputs as unset for int and bool
Browse files Browse the repository at this point in the history
  • Loading branch information
splitbrain committed Jun 28, 2012
1 parent 0189bd8 commit 5d0aaf9
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
12 changes: 11 additions & 1 deletion _test/tests/inc/input.test.php
Expand Up @@ -95,6 +95,11 @@ public function test_int() {
$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() {
Expand Down Expand Up @@ -155,6 +160,11 @@ public function test_bool() {

$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() {
Expand Down Expand Up @@ -203,4 +213,4 @@ public function test_ref(){
$this->assertEquals('bla',$test);
}

}
}
5 changes: 4 additions & 1 deletion inc/Input.class.php
Expand Up @@ -118,6 +118,7 @@ public function &ref($name, $default = '', $nonempty = false) {
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];
Expand Down Expand Up @@ -151,6 +152,8 @@ public function str($name, $default = '', $nonempty = false) {
*/
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];
Expand Down Expand Up @@ -223,4 +226,4 @@ public function set($name, $value) {
parent::set($name, $value);
$_REQUEST[$name] = $value;
}
}
}

0 comments on commit 5d0aaf9

Please sign in to comment.