Skip to content

Commit 1cd73b0

Browse files
committed
feat(strings): add new method wrap
1 parent 5a1669b commit 1cd73b0

2 files changed

Lines changed: 23 additions & 0 deletions

File tree

src/Strings.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1892,6 +1892,24 @@ public function format(...$args): self
18921892
return $this;
18931893
}
18941894

1895+
/**
1896+
* Wrap the string with the given strings.
1897+
*
1898+
* @param string $before String to wrap before.
1899+
* @param string|null $after String to wrap after.
1900+
*
1901+
* @return self Returns instance of the Strings class.
1902+
*
1903+
* @access public
1904+
*/
1905+
public function wrap($before, $after = null)
1906+
{
1907+
$this->string = $before . $this->string . ($after ??= $before);
1908+
1909+
return $this;
1910+
}
1911+
1912+
18951913
/**
18961914
* Returns true if the string matches a given pattern.
18971915
*

tests/StringsTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,11 @@
510510
$this->assertEquals('test - FÒÔ BÀŘS', $strings2->toString());
511511
});
512512

513+
test('test wrap() method', function(): void {
514+
$this->assertEquals('<< Stranger Things >>', Strings::create('Stranger Things')->wrap('<< ', ' >>')->toString());
515+
$this->assertEquals('#Stranger Things#', Strings::create('Stranger Things')->wrap('#')->toString());
516+
});
517+
513518
test('test replaceSubstr() method', function (): void {
514519
$this->assertEquals('19-84', Strings::create('1984')->replaceSubstr('-', 2, 0)->toString());
515520
$this->assertEquals('fòô bàř bàz', Strings::create('fòô bàz')->replaceSubstr('bàř ', 4, 0)->toString());

0 commit comments

Comments
 (0)