Skip to content

Commit 8ea50bc

Browse files
authored
[9.x] Add missing Str::wrap() static method (#44207)
* Move wrap to Str static method * Add tests
1 parent 3bbc0e7 commit 8ea50bc

File tree

3 files changed

+19
-1
lines changed

3 files changed

+19
-1
lines changed

src/Illuminate/Support/Str.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,18 @@ public static function finish($value, $cap)
338338
return preg_replace('/(?:'.$quoted.')+$/u', '', $value).$cap;
339339
}
340340

341+
/**
342+
* Wrap the string with the given strings.
343+
*
344+
* @param string $before
345+
* @param string|null $after
346+
* @return string
347+
*/
348+
public static function wrap($value, $before, $after = null)
349+
{
350+
return $before.$value.($after ??= $before);
351+
}
352+
341353
/**
342354
* Determine if a given string matches a given pattern.
343355
*

src/Illuminate/Support/Stringable.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1060,7 +1060,7 @@ public function wordCount()
10601060
*/
10611061
public function wrap($before, $after = null)
10621062
{
1063-
return new static($before.$this->value.($after ??= $before));
1063+
return new static(Str::wrap($this->value, $before, $after));
10641064
}
10651065

10661066
/**

tests/Support/SupportStrTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,12 @@ public function testFinish()
352352
$this->assertSame('abcbbc', Str::finish('abcbbcbc', 'bc'));
353353
}
354354

355+
public function testWrap()
356+
{
357+
$this->assertEquals('"value"', Str::wrap('value', '"'));
358+
$this->assertEquals('foo-bar-baz', Str::wrap('-bar-', 'foo', 'baz'));
359+
}
360+
355361
public function testIs()
356362
{
357363
$this->assertTrue(Str::is('/', '/'));

0 commit comments

Comments
 (0)