Skip to content

Commit

Permalink
feat(strings): add new method betweenFirst
Browse files Browse the repository at this point in the history
  • Loading branch information
Awilum committed Apr 3, 2022
1 parent 05b9d8b commit d250a7d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
25 changes: 22 additions & 3 deletions src/Strings.php
Original file line number Diff line number Diff line change
Expand Up @@ -952,11 +952,30 @@ public function lastSegment(string $delimiter = ' '): self
public function between(string $from, string $to): self
{
if ($from === '' || $to === '') {
$this->string = $this->string;
} else {
$this->string = static::create((string) static::create($this->string, $this->encoding)->after($from), $this->encoding)->beforeLast($to)->toString();
return $this;
}

$this->string = static::create((string) static::create($this->string, $this->encoding)->after($from), $this->encoding)->beforeLast($to)->toString();

return $this;
}

/**
* Get the portion of a string between first two given values.
*
* @param string $from From
* @param string $to To
*
* @return self Returns instance of The Strings class.
*/
public function betweenFirst(string $from, string $to): self
{
if ($from === '' || $to === '') {
return $this;
}

$this->string = static::create((string) static::create($this->string, $this->encoding)->after($from), $this->encoding)->before($to)->toString();

return $this;
}

Expand Down
8 changes: 8 additions & 0 deletions tests/StringsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,14 @@
$this->assertEquals('SG-1 returns from an off-world mission', Strings::create('SG-1 returns from an off-world mission')->between('', ''));
});

test('test betweenFirst() method', function (): void {
$this->assertEquals('fòô', Strings::create('fòô')->betweenFirst('', ''));
$this->assertEquals('fòô', Strings::create('fòô')->betweenFirst('', 'ô'));
$this->assertEquals('fòô', Strings::create('fòô')->betweenFirst('f', ''));
$this->assertEquals('ò', Strings::create('fòô')->betweenFirst('f', 'ô'));
$this->assertEquals('ò', Strings::create('[ò]ab[ô]')->betweenFirst('[', ']'));
});

test('test before() method', function (): void {
$this->assertEquals('SG-1 returns from an off-world ', Strings::create('SG-1 returns from an off-world mission')->before('mission'));
$this->assertEquals('fòô ', Strings::create('fòô bàřs')->before('bàřs'));
Expand Down

0 comments on commit d250a7d

Please sign in to comment.