Skip to content

Commit

Permalink
Merge pull request #1461 from pimlie/feature-wildcard-parameter
Browse files Browse the repository at this point in the history
[8.0] Make wildcard string a function parameter.
  • Loading branch information
yajra authored Oct 22, 2017
2 parents 9e96c9d + 1e1a032 commit 6f36642
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
17 changes: 15 additions & 2 deletions src/Utilities/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -280,12 +280,25 @@ public static function extractColumnName($str, $wantsAlias)
*/
public static function wildcardLikeString($str, $lowercase = true)
{
$wild = '%';
return static::wildcardString($str, '%', $lowercase);
}

/**
* Adds wildcards to the given string.
*
* @param string $str
* @param string $wildcard
* @param bool $lowercase
* @return string
*/
public static function wildcardString($str, $wildcard, $lowercase = true)
{
$wild = $wildcard;
$chars = preg_split('//u', $str, -1, PREG_SPLIT_NO_EMPTY);

if (count($chars) > 0) {
foreach ($chars as $char) {
$wild .= $char . '%';
$wild .= $char . $wildcard;
}
}

Expand Down
18 changes: 18 additions & 0 deletions tests/Unit/HelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -247,4 +247,22 @@ public function test_replace_pattern_with_keyword()
$result = Helper::replacePatternWithKeyword($subject, $keyword, '$1');
$this->assertEquals(['foo in ?', ['bar']], $result);
}

public function test_wildcard_like_string()
{
$str = 'keyword';

$keyword = Helper::wildcardLikeString($str);

$this->assertEquals('%k%e%y%w%o%r%d%', $keyword);
}

public function test_wildcard_string()
{
$str = 'Keyword';

$keyword = Helper::wildcardString($str, '.*', true);

$this->assertEquals('.*k.*e.*y.*w.*o.*r.*d.*', $keyword);
}
}

0 comments on commit 6f36642

Please sign in to comment.