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));
+ }
+
}
$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);
+ }
+
}