Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[8.0] Make wildcard string a function parameter. #1461

Merged
merged 1 commit into from
Oct 22, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}