Skip to content

Commit

Permalink
make end nullable in findBetween methods and php doc enhancements
Browse files Browse the repository at this point in the history
  • Loading branch information
salehhashemi1992 committed Nov 2, 2023
1 parent b50d84a commit 8227316
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 18 deletions.
45 changes: 30 additions & 15 deletions src/StringHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -620,17 +620,22 @@ public static function rtrim(string|array $string, string $pattern = self::DEFAU
}

/**
* Returns the portion of the string that lies between the first occurrence of the start string
* and the last occurrence of the end string after that.
* Returns the portion of the string that lies between the first occurrence of the `$start` string
* and the last occurrence of the `$end` string after that.
*
* @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.
* @param string|null $end The string marking the end of the portion to extract.
* If the `$end` string is not provided, it defaults to the value of the `$start` string.
* @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.
* `$start` and the last occurrence of `$end`, or null if either `$start` or `$end` cannot be found.
*/
public static function findBetween(string $string, string $start, string $end): ?string
public static function findBetween(string $string, string $start, ?string $end = null): ?string
{
if ($end === null) {
$end = $start;
}

$startPos = mb_strpos($string, $start);

if ($startPos === false) {
Expand All @@ -648,16 +653,21 @@ public static function findBetween(string $string, string $start, string $end):
}

/**
* Returns the portion of the string between the initial occurrence of the 'start' string
* and the next occurrence of the 'end' string.
* Returns the portion of the string between the initial occurrence of the '$start' string
* and the next occurrence of the '$end' string.
*
* @param string $string The input string.
* @param string $start The string marking the beginning of the segment to extract.
* @param string $end The string marking the termination of the segment.
* @return string|null Extracted segment, or null if 'start' or 'end' is not present.
* @param string|null $end The string marking the termination of the segment.
* If the '$end' string is not provided, it defaults to the value of the '$start' string.
* @return string|null Extracted segment, or null if '$start' or '$end' is not present.
*/
public static function findBetweenFirst(string $string, string $start, string $end): ?string
public static function findBetweenFirst(string $string, string $start, ?string $end = null): ?string
{
if ($end === null) {
$end = $start;
}

$startPos = mb_strpos($string, $start);

if ($startPos === false) {
Expand All @@ -675,16 +685,21 @@ public static function findBetweenFirst(string $string, string $start, string $e
}

/**
* Returns the portion of the string between the latest 'start' string
* and the subsequent 'end' string.
* Returns the portion of the string between the latest '$start' string
* and the subsequent '$end' string.
*
* @param string $string The input string.
* @param string $start The string marking the beginning of the segment to extract.
* @param string $end The string marking the termination of the segment.
* @return string|null Extracted segment, or null if 'start' or 'end' is not present.
* @param string|null $end The string marking the termination of the segment.
* If the '$end' string is not provided, it defaults to the value of the '$start' string.
* @return string|null Extracted segment, or null if '$start' or '$end' is not present.
*/
public static function findBetweenLast(string $string, string $start, string $end): ?string
public static function findBetweenLast(string $string, string $start, ?string $end = null): ?string
{
if ($end === null) {
$end = $start;
}

$endPos = mb_strrpos($string, $end);

if ($endPos === false) {
Expand Down
9 changes: 6 additions & 3 deletions tests/StringHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -767,7 +767,7 @@ public function testInvalidTrimPattern(): void
/**
* @dataProvider dataProviderFindBetween
*/
public function testFindBetween(string $string, string $start, string $end, ?string $expectedResult): void
public function testFindBetween(string $string, string $start, ?string $end, ?string $expectedResult): void
{
$this->assertSame($expectedResult, StringHelper::findBetween($string, $start, $end));
}
Expand All @@ -786,6 +786,7 @@ public function dataProviderFindBetween(): array
['start only', 'start', 'end', null], // start found but no end
['end only', 'start', 'end', null], // end found but no start
['a1a2a3a', 'a', 'a', '1a2a3'], // same start and end
['a1a2a3a', 'a', null, '1a2a3'], // end is null
['spécial !@#$%^&*()', 'spé', '&*()', 'cial !@#$%^'], // Special characters
['من صالح هاشمی هستم', 'من ', ' هستم', 'صالح هاشمی'], // other languages
];
Expand All @@ -794,7 +795,7 @@ public function dataProviderFindBetween(): array
/**
* @dataProvider dataProviderFindBetweenFirst
*/
public function testFindBetweenFirst(string $string, string $start, string $end, ?string $expectedResult): void
public function testFindBetweenFirst(string $string, string $start, ?string $end, ?string $expectedResult): void
{
$this->assertSame($expectedResult, StringHelper::findBetweenFirst($string, $start, $end));
}
Expand All @@ -815,6 +816,7 @@ public function dataProviderFindBetweenFirst(): array
['start only', 'start', 'end', null], // start found but no end
['end only', 'start', 'end', null], // end found but no start
['a1a2a3a', 'a', 'a', '1'], // same start and end
['a1a2a3a', 'a', null, '1'], // end is null
['spécial !@#$%^&*()', 'spé', '&*()', 'cial !@#$%^'], // Special characters
['من صالح هاشمی هستم هستم', 'من ', ' هستم', 'صالح هاشمی'], // other languages
];
Expand All @@ -823,7 +825,7 @@ public function dataProviderFindBetweenFirst(): array
/**
* @dataProvider dataProviderFindBetweenLast
*/
public function testFindBetweenLast(string $string, string $start, string $end, ?string $expectedResult): void
public function testFindBetweenLast(string $string, string $start, ?string $end, ?string $expectedResult): void
{
$this->assertSame($expectedResult, StringHelper::findBetweenLast($string, $start, $end));
}
Expand All @@ -844,6 +846,7 @@ public function dataProviderFindBetweenLast(): array
['start only', 'start', 'end', null], // start found but no end
['end only', 'start', 'end', null], // end found but no start
['a1a2a3a', 'a', 'a', '3'], // same start and end
['a1a2a3a', 'a', null, '3'], // end is null
['spécial !@#$%^&*()', 'spé', '&*()', 'cial !@#$%^'], // Special characters
['من صالح هاشمی هستم هستم', 'من ', ' هستم', 'صالح هاشمی هستم'], // other languages
];
Expand Down

0 comments on commit 8227316

Please sign in to comment.