Skip to content

Commit 3ab9f7a

Browse files
committed
Merge branch 'feature/tap-do-stringable' into 8.x
2 parents ae790fe + dd0d096 commit 3ab9f7a

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

src/Illuminate/Support/Stringable.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@
44

55
use Closure;
66
use Illuminate\Support\Traits\Macroable;
7+
use Illuminate\Support\Traits\Tappable;
78
use Symfony\Component\VarDumper\VarDumper;
89

910
class Stringable
1011
{
11-
use Macroable;
12+
use Macroable, Tappable;
1213

1314
/**
1415
* The underlying string value.
@@ -399,6 +400,17 @@ public function parseCallback($default = null)
399400
return Str::parseCallback($this->value, $default);
400401
}
401402

403+
/**
404+
* Call the given callback and return a new string.
405+
*
406+
* @param callable $callback
407+
* @return static
408+
*/
409+
public function pipe(callable $callback)
410+
{
411+
return new static(call_user_func($callback, $this));
412+
}
413+
402414
/**
403415
* Get the plural form of an English word.
404416
*

tests/Support/SupportStringableTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -545,4 +545,28 @@ public function testChunk()
545545
$this->assertInstanceOf(Collection::class, $chunks);
546546
$this->assertSame(['foo', 'bar', 'baz'], $chunks->all());
547547
}
548+
549+
public function testTap()
550+
{
551+
$stringable = $this->stringable('foobarbaz');
552+
553+
$fromTheTap = '';
554+
555+
$stringable = $stringable->tap(function (Stringable $string) use (&$fromTheTap) {
556+
$fromTheTap = $string->substr(0, 3);
557+
});
558+
559+
$this->assertSame('foo', (string) $fromTheTap);
560+
$this->assertSame('foobarbaz', (string) $stringable);
561+
}
562+
563+
public function testPipe()
564+
{
565+
$callback = function ($stringable) {
566+
return 'bar';
567+
};
568+
569+
$this->assertInstanceOf(Stringable::class, $this->stringable('foo')->pipe($callback));
570+
$this->assertSame('bar', (string) $this->stringable('foo')->pipe($callback));
571+
}
548572
}

0 commit comments

Comments
 (0)