Combined case sensitive and insensitive functions

Added tests
This commit is contained in:
Tony Murray
2016-08-31 00:11:04 -05:00
parent 38901bfe83
commit 9b93fc5fd7
3 changed files with 123 additions and 69 deletions

View File

@@ -1375,33 +1375,24 @@ function ResolveGlues($tables, $target, $x = 0, $hist = array(), $last = array()
/**
* Determine if a given string contains a given substring.
*
* @param string $haystack
* @param string|array $needles
* @param string $haystack
* @param string|array $needles
* @param bool $case_insensitive
* @return bool
*/
function str_contains($haystack, $needles)
function str_contains($haystack, $needles, $case_insensitive = false)
{
foreach ((array) $needles as $needle) {
if ($needle != '' && strpos($haystack, $needle) !== false) {
return true;
if ($case_insensitive) {
foreach ((array) $needles as $needle) {
if ($needle != '' && stripos($haystack, $needle) !== false) {
return true;
}
}
}
return false;
}
/**
* Determine if a given string contains a given substring.
* Case Insensitive.
*
* @param string $haystack
* @param string|array $needles
* @return bool
*/
function str_icontains($haystack, $needles)
{
foreach ((array) $needles as $needle) {
if ($needle != '' && stripos($haystack, $needle) !== false) {
return true;
} else {
foreach ((array) $needles as $needle) {
if ($needle != '' && strpos($haystack, $needle) !== false) {
return true;
}
}
}
return false;
@@ -1410,34 +1401,25 @@ function str_icontains($haystack, $needles)
/**
* Determine if a given string ends with a given substring.
*
* @param string $haystack
* @param string|array $needles
* @param string $haystack
* @param string|array $needles
* @param bool $case_insensitive
* @return bool
*/
function ends_with($haystack, $needles)
function ends_with($haystack, $needles, $case_insensitive = false)
{
foreach ((array)$needles as $needle) {
if ((string)$needle === substr($haystack, -strlen($needle))) {
return true;
if ($case_insensitive) {
$lower_haystack = strtolower($haystack);
foreach ((array)$needles as $needle) {
if (strtolower($needle) === substr($lower_haystack, -strlen($needle))) {
return true;
}
}
}
return false;
}
/**
* Determine if a given string ends with a given substring.
* Case insensitive.
*
* @param string $haystack
* @param string|array $needles
* @return bool
*/
function iends_with($haystack, $needles)
{
$lower_haystack = strtolower($haystack);
foreach ((array)$needles as $needle) {
if (strtolower($needle) === substr($lower_haystack, -strlen($needle))) {
return true;
} else {
foreach ((array)$needles as $needle) {
if ((string)$needle === substr($haystack, -strlen($needle))) {
return true;
}
}
}
return false;
@@ -1448,31 +1430,22 @@ function iends_with($haystack, $needles)
*
* @param string $haystack
* @param string|array $needles
* @param bool $case_insensitive
* @return bool
*/
function starts_with($haystack, $needles)
function starts_with($haystack, $needles, $case_insensitive = false)
{
foreach ((array)$needles as $needle) {
if ($needle != '' && strpos($haystack, $needle) === 0) {
return true;
}
}
return false;
}
/**
* Determine if a given string starts with a given substring.
* Case insensitive.
*
* @param string $haystack
* @param string|array $needles
* @return bool
*/
function istarts_with($haystack, $needles)
{
foreach ((array)$needles as $needle) {
if ($needle != '' && stripos($haystack, $needle) === 0) {
return true;
if ($case_insensitive) {
foreach ((array)$needles as $needle) {
if ($needle != '' && stripos($haystack, $needle) === 0) {
return true;
}
}
} else {
foreach ((array)$needles as $needle) {
if ($needle != '' && strpos($haystack, $needle) === 0) {
return true;
}
}
}
return false;

View File

@@ -0,0 +1,80 @@
<?php
/**
* CommonFunctionsTest.php
*
* -Description-
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @package LibreNMS
* @link http://librenms.org
* @copyright 2016 Tony Murray
* @author Tony Murray <murraytony@gmail.com>
*/
namespace LibreNMS\Tests;
class CommonFunctionsTest extends \PHPUnit_Framework_TestCase
{
public function testStrContains()
{
$data = 'This is a test. Just Testing.';
$this->assertTrue(str_contains($data, 'Just'));
$this->assertFalse(str_contains($data, 'just'));
$this->assertTrue(str_contains($data, 'juSt', true));
$this->assertFalse(str_contains($data, 'nope', true));
$this->assertTrue(str_contains($data, array('not', 'this', 'This')));
$this->assertFalse(str_contains($data, array('not', 'this')));
$this->assertTrue(str_contains($data, array('not', 'thIs'), true));
$this->assertFalse(str_contains($data, array('not', 'anything'), true));
}
public function testStartsWith()
{
$data = 'This is a test. Just Testing that.';
$this->assertTrue(starts_with($data, 'This'));
$this->assertFalse(starts_with($data, 'this'));
$this->assertTrue(starts_with($data, 'thIs', true));
$this->assertFalse(starts_with($data, 'test', true));
$this->assertTrue(starts_with($data, array('this', 'Test', 'This')));
$this->assertFalse(starts_with($data, array('this', 'Test')));
$this->assertTrue(starts_with($data, array('Test', 'no', 'thiS'), true));
$this->assertFalse(starts_with($data, array('just', 'Test'), true));
}
public function testEndsWith()
{
$data = 'This is a test. Just Testing';
$this->assertTrue(ends_with($data, 'Testing'));
$this->assertFalse(ends_with($data, 'testing'));
$this->assertTrue(ends_with($data, 'testIng', true));
$this->assertFalse(ends_with($data, 'test', true));
$this->assertTrue(ends_with($data, array('this', 'Testing', 'This')));
$this->assertFalse(ends_with($data, array('this', 'Test')));
$this->assertTrue(ends_with($data, array('this', 'tesTing', 'no'), true));
$this->assertFalse(ends_with($data, array('this', 'Test'), true));
}
}

View File

@@ -30,5 +30,6 @@ require_once $install_dir . '/includes/defaults.inc.php';
$classLoader->registerDir($install_dir . '/tests', 'LibreNMS\Tests');
require_once $install_dir . '/includes/common.php';
require_once $install_dir . '/includes/rrdtool.inc.php';
require_once $install_dir . '/includes/syslog.php';