aboutsummaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
-rw-r--r--_test/tests/inc/common_blank.test.php52
-rw-r--r--inc/common.php19
2 files changed, 71 insertions, 0 deletions
diff --git a/_test/tests/inc/common_blank.test.php b/_test/tests/inc/common_blank.test.php
new file mode 100644
index 000000000..9df35936a
--- /dev/null
+++ b/_test/tests/inc/common_blank.test.php
@@ -0,0 +1,52 @@
+<?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");
+ }
+
+}
diff --git a/inc/common.php b/inc/common.php
index 17facd249..55916dc05 100644
--- a/inc/common.php
+++ b/inc/common.php
@@ -31,6 +31,25 @@ function hsc($string) {
}
/**
+ * 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
*
* You can give an indention as optional parameter