diff --git a/src/Util/U.php b/src/Util/U.php index d64abcb1..4545f712 100644 --- a/src/Util/U.php +++ b/src/Util/U.php @@ -413,6 +413,7 @@ public static function success_out($output) { // http://stackoverflow.com/questions/834303/startswith-and-endswith-public static functions-in-php public static function startsWith($haystack, $needle) { // search backwards starting from haystack length characters from the end + if ( !is_string($haystack) || !is_string($needle) ) return false; return $needle === "" || strrpos($haystack, $needle, -strlen($haystack)) !== FALSE; } diff --git a/tests/Util/UTest.php b/tests/Util/UTest.php index 1d1d7b4d..532283ce 100644 --- a/tests/Util/UTest.php +++ b/tests/Util/UTest.php @@ -306,6 +306,17 @@ public function testSha256() { $this->assertEquals($out, null); } + public function testStartsWith() { + $this->assertTrue(U::startsWith("csev@umich.edu", "csev")); + $this->assertTrue(U::startsWith("csev@umich.edu", "")); + $this->assertFalse(U::startsWith("csev@umich.edu", "cxev")); + $this->assertFalse(U::startsWith("edu", "@umich.edu")); + $this->assertFalse(U::startsWith(null, "@umich.edu")); + $this->assertFalse(U::startsWith(null, null)); + $this->assertFalse(U::startsWith("edu", null)); + } + + public function testEndsWith() { $this->assertTrue(U::endsWith("csev@umich.edu", "@umich.edu")); $this->assertTrue(U::endsWith("csev@umich.edu", ""));