Skip to content

Commit

Permalink
refactor findBetween method and add other tests
Browse files Browse the repository at this point in the history
  • Loading branch information
salehhashemi1992 committed Oct 30, 2023
1 parent caa2029 commit 26032ad
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 7 deletions.
1 change: 1 addition & 0 deletions framework/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Yii Framework 2 Change Log
- Enh #20030: Improve performance of handling `ErrorHandler::$memoryReserveSize` (antonshevelev, rob006)
- Enh #20042: Add empty array check to `ActiveQueryTrait::findWith()` (renkas)
- Enh #20032: Added `mask` method for string masking with multibyte support (salehhashemi1992)
- Enh #20034: Added `findBetween` to retrieve a substring that lies between two strings (salehhashemi1992)


2.0.49.2 October 12, 2023
Expand Down
7 changes: 4 additions & 3 deletions framework/helpers/BaseStringHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -535,22 +535,23 @@ public static function mask($string, $start, $length, $mask = '*') {
* @param string $string The input string.
* @param string $start The string marking the start of the portion to extract.
* @param string $end The string marking the end of the portion to extract.
* @return string The portion of the string between the first occurrence of start and the last occurrence of end.
* @return string|null The portion of the string between the first occurrence of
* start and the last occurrence of end, or null if either start or end cannot be found.
*/
public static function findBetween($string, $start, $end)

Check warning on line 541 in framework/helpers/BaseStringHelper.php

View check run for this annotation

Codecov / codecov/patch

framework/helpers/BaseStringHelper.php#L541

Added line #L541 was not covered by tests
{
$startPos = mb_strpos($string, $start);

Check warning on line 543 in framework/helpers/BaseStringHelper.php

View check run for this annotation

Codecov / codecov/patch

framework/helpers/BaseStringHelper.php#L543

Added line #L543 was not covered by tests

if ($startPos === false) {
return '';
return null;

Check warning on line 546 in framework/helpers/BaseStringHelper.php

View check run for this annotation

Codecov / codecov/patch

framework/helpers/BaseStringHelper.php#L545-L546

Added lines #L545 - L546 were not covered by tests
}

// Cut the string from the start position
$subString = mb_substr($string, $startPos + mb_strlen($start));
$endPos = mb_strrpos($subString, $end);

Check warning on line 551 in framework/helpers/BaseStringHelper.php

View check run for this annotation

Codecov / codecov/patch

framework/helpers/BaseStringHelper.php#L550-L551

Added lines #L550 - L551 were not covered by tests

if ($endPos === false) {
return '';
return null;

Check warning on line 554 in framework/helpers/BaseStringHelper.php

View check run for this annotation

Codecov / codecov/patch

framework/helpers/BaseStringHelper.php#L553-L554

Added lines #L553 - L554 were not covered by tests
}

return mb_substr($subString, 0, $endPos);

Check warning on line 557 in framework/helpers/BaseStringHelper.php

View check run for this annotation

Codecov / codecov/patch

framework/helpers/BaseStringHelper.php#L557

Added line #L557 was not covered by tests
Expand Down
12 changes: 8 additions & 4 deletions tests/framework/helpers/StringHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -522,14 +522,18 @@ public function testFindBetween($string, $start, $end, $expectedResult)
public function dataProviderFindBetween()
{
return [
['hello world hello', 'hello ', ' world', ''], // end before start
['This is a sample string', 'is ', ' string', 'is a sample'], // normal case
['hello world hello', ' hello', ' world', null], // end before start
['This is a sample string', ' is ', ' string', 'a sample'], // normal case
['startendstart', 'start', 'end', ''], // end before start
['startmiddleend', 'start', 'end', 'middle'], // normal case
['startend', 'start', 'end', ''], // end immediately follows start
['multiple start start end end', 'start ', ' end', 'start end'], // multiple starts and ends
['', 'start', 'end', ''], // empty string
['no delimiters here', 'start', 'end', ''], // no start and end
['', 'start', 'end', null], // empty string
['no delimiters here', 'start', 'end', null], // no start and end
['start only', 'start', 'end', null], // start found but no end
['end only', 'start', 'end', null], // end found but no start
['spécial !@#$%^&*()', 'spé', '&*()', 'cial !@#$%^'], // Special characters
['من صالح هاشمی هستم', 'من ', ' هستم', 'صالح هاشمی'], // other languages
];
}
}

0 comments on commit 26032ad

Please sign in to comment.