Skip to content
Snippets Groups Projects
Commit fef14ecf authored by Andreas Gohr's avatar Andreas Gohr
Browse files

Merge pull request #1365 from splitbrain/blank

add convenience function for empty check without 0
parents ee6517f1 ddd88cdd
No related branches found
No related tags found
No related merge requests found
<?php
class common_blank_test extends DokuWikiTest {
private $nope;
function test_blank() {
$tests = array(
// these are not blank
array('string', false),
array(1, false),
array(1.0, false),
array(0xff, false),
array(array('something'), false),
// these aren't either!
array('0', false),
array(' ', false),
array('0.0', false),
array(0, false),
array(0.0, false),
array(0x00, false),
array(true, false),
// but these are
array('', true),
array(array(), true),
array(null, true),
array(false, true),
array("\0", true)
);
foreach($tests as $test) {
$this->assertEquals($test[1], blank($test[0]), "using " . var_export($test[0], true));
}
}
function test_trim() {
$whitespace = " \t\r\n";
$this->assertFalse(blank($whitespace), "using default \$trim value");
$this->assertFalse(blank($whitespace, false), "using \$trim = false");
$this->assertTrue(blank($whitespace, true), "using \$trim = true");
}
function test_undefined() {
$undef = array();
$this->assertTrue(blank($var), "using undefined/unitialised variable");
$this->assertTrue(blank($undef['nope']), "using undefined array index");
$this->assertTrue(blank($this->nope), "using unset object property");
}
}
...@@ -30,6 +30,25 @@ function hsc($string) { ...@@ -30,6 +30,25 @@ function hsc($string) {
return htmlspecialchars($string, ENT_QUOTES, 'UTF-8'); return htmlspecialchars($string, ENT_QUOTES, 'UTF-8');
} }
/**
* Checks if the given input is blank
*
* This is similar to empty() but will return false for "0".
*
* @param $in
* @param bool $trim Consider a string of whitespace to be blank
* @return bool
*/
function blank(&$in, $trim = false) {
if(!isset($in)) return true;
if(is_null($in)) return true;
if(is_array($in)) return empty($in);
if($in === "\0") return true;
if($trim && trim($in) === '') return true;
if(strlen($in) > 0) return false;
return empty($in);
}
/** /**
* print a newline terminated string * print a newline terminated string
* *
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment