From: Tim Otten Date: Wed, 10 Feb 2016 02:57:31 +0000 (-0800) Subject: CRM_Utils_String - startsWith(), endsWith(), filterByWildcards() X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=83d511e6d8cc94a1298e507a443748f25c6d2661;p=civicrm-core.git CRM_Utils_String - startsWith(), endsWith(), filterByWildcards() --- diff --git a/CRM/Utils/String.php b/CRM/Utils/String.php index fb99bfea70..6d598e792f 100644 --- a/CRM/Utils/String.php +++ b/CRM/Utils/String.php @@ -794,4 +794,66 @@ class CRM_Utils_String { return ltrim($output); } + /** + * Determine if $string starts with $fragment. + * + * @param string $string + * The long string. + * @param string $fragment + * The fragment to look for. + * @return bool + */ + public static function startsWith($string, $fragment) { + if ($fragment === '') { + return TRUE; + } + $len = strlen($fragment); + return substr($string, 0, $len) === $fragment; + } + + /** + * Determine if $string ends with $fragment. + * + * @param string $string + * The long string. + * @param string $fragment + * The fragment to look for. + * @return bool + */ + public static function endsWith($string, $fragment) { + if ($fragment === '') { + return TRUE; + } + $len = strlen($fragment); + return substr($string, -1 * $len) === $fragment; + } + + /** + * @param string|array $patterns + * @param array $allStrings + * @param bool $allowNew + * Whether to return new, unrecognized names. + * @return array + */ + public static function filterByWildcards($patterns, $allStrings, $allowNew = FALSE) { + $patterns = (array) $patterns; + $result = array(); + foreach ($patterns as $pattern) { + if (!\CRM_Utils_String::endsWith($pattern, '*')) { + if ($allowNew || in_array($pattern, $allStrings)) { + $result[] = $pattern; + } + } + else { + $prefix = rtrim($pattern, '*'); + foreach ($allStrings as $key) { + if (\CRM_Utils_String::startsWith($key, $prefix)) { + $result[] = $key; + } + } + } + } + return array_values(array_unique($result)); + } + } diff --git a/tests/phpunit/CRM/Utils/StringTest.php b/tests/phpunit/CRM/Utils/StringTest.php index 4578f1c3a0..1aed89c67a 100644 --- a/tests/phpunit/CRM/Utils/StringTest.php +++ b/tests/phpunit/CRM/Utils/StringTest.php @@ -161,4 +161,66 @@ class CRM_Utils_StringTest extends CiviUnitTestCase { $this->assertTrue($expected === $actual); } + public function startEndCases() { + $cases = array(); + $cases[] = array('startsWith', 'foo', '', TRUE); + $cases[] = array('startsWith', 'foo', 'f', TRUE); + $cases[] = array('startsWith', 'foo', 'fo', TRUE); + $cases[] = array('startsWith', 'foo', 'foo', TRUE); + $cases[] = array('startsWith', 'foo', 'fooo', FALSE); + $cases[] = array('startsWith', 'foo', 'o', FALSE); + $cases[] = array('endsWith', 'foo', 'f', FALSE); + $cases[] = array('endsWith', 'foo', '', TRUE); + $cases[] = array('endsWith', 'foo', 'o', TRUE); + $cases[] = array('endsWith', 'foo', 'oo', TRUE); + $cases[] = array('endsWith', 'foo', 'foo', TRUE); + $cases[] = array('endsWith', 'foo', 'fooo', FALSE); + $cases[] = array('endsWith', 'foo*', '*', TRUE); + return $cases; + } + + /** + * @param string $func + * One of: 'startsWith' or 'endsWith'. + * @param $string + * @param $fragment + * @param $expectedResult + * @dataProvider startEndCases + */ + public function testStartEndWith($func, $string, $fragment, $expectedResult) { + $actualResult = \CRM_Utils_String::$func($string, $fragment); + $this->assertEquals($expectedResult, $actualResult, "Checking $func($string,$fragment)"); + } + + public function wildcardCases() { + $cases = array(); + $cases[] = array('*', array('foo.bar.1', 'foo.bar.2', 'foo.whiz', 'bang.bang')); + $cases[] = array('foo.*', array('foo.bar.1', 'foo.bar.2', 'foo.whiz')); + $cases[] = array('foo.bar.*', array('foo.bar.1', 'foo.bar.2')); + $cases[] = array(array('foo.bar.*', 'foo.bar.2'), array('foo.bar.1', 'foo.bar.2')); + $cases[] = array(array('foo.bar.2', 'foo.w*'), array('foo.bar.2', 'foo.whiz')); + return $cases; + } + + /** + * @param $patterns + * @param $expectedResults + * @dataProvider wildcardCases + */ + public function testFilterByWildCards($patterns, $expectedResults) { + $data = array('foo.bar.1', 'foo.bar.2', 'foo.whiz', 'bang.bang'); + + $actualResults = CRM_Utils_String::filterByWildcards($patterns, $data); + $this->assertEquals($expectedResults, $actualResults); + + $patterns = (array) $patterns; + $patterns[] = 'noise'; + + $actualResults = CRM_Utils_String::filterByWildcards($patterns, $data, FALSE); + $this->assertEquals($expectedResults, $actualResults); + + $actualResults = CRM_Utils_String::filterByWildcards($patterns, $data, TRUE); + $this->assertEquals(array_merge($expectedResults, array('noise')), $actualResults); + } + }