Skip to content

Commit

Permalink
CRM_Utils_String - startsWith(), endsWith(), filterByWildcards()
Browse files Browse the repository at this point in the history
  • Loading branch information
totten committed Jan 18, 2017
1 parent d9df759 commit 92a49a3
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 0 deletions.
62 changes: 62 additions & 0 deletions CRM/Utils/String.php
Original file line number Diff line number Diff line change
Expand Up @@ -793,4 +793,66 @@ public static function htmlAttributes($attributes) {
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));
}

}
62 changes: 62 additions & 0 deletions tests/phpunit/CRM/Utils/StringTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,4 +165,66 @@ public function testStrToBool($input, $expected) {
$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);
}

}

0 comments on commit 92a49a3

Please sign in to comment.