From f4c7912753cbcc80104ea844593f7fdd1ca0fed7 Mon Sep 17 00:00:00 2001 From: BinotaLIU Date: Sun, 24 Jan 2021 16:00:40 +0800 Subject: [PATCH 1/3] add Str::pipe --- src/Illuminate/Support/Stringable.php | 11 +++++++++++ tests/Support/SupportStringableTest.php | 24 ++++++++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/src/Illuminate/Support/Stringable.php b/src/Illuminate/Support/Stringable.php index c24d19e6a923..1b9e1a0b6933 100644 --- a/src/Illuminate/Support/Stringable.php +++ b/src/Illuminate/Support/Stringable.php @@ -399,6 +399,17 @@ public function parseCallback($default = null) return Str::parseCallback($this->value, $default); } + /** + * Call the given callback and return a new string. + * + * @param callable $callback + * @return static + */ + public function pipe(callable $callback) + { + return new static(call_user_func($callback, $this)); + } + /** * Get the plural form of an English word. * diff --git a/tests/Support/SupportStringableTest.php b/tests/Support/SupportStringableTest.php index 1986d050de9c..851b2c0617ac 100644 --- a/tests/Support/SupportStringableTest.php +++ b/tests/Support/SupportStringableTest.php @@ -545,4 +545,28 @@ public function testChunk() $this->assertInstanceOf(Collection::class, $chunks); $this->assertSame(['foo', 'bar', 'baz'], $chunks->all()); } + + public function testTap() + { + $stringable = $this->stringable('foobarbaz'); + + $fromTheTap = ''; + + $stringable = $stringable->tap(function (Stringable $string) use (&$fromTheTap) { + $fromTheTap = $string->substr(0, 3); + }); + + $this->assertSame('foo', (string) $fromTheTap); + $this->assertSame('foobarbaz', (string) $stringable); + } + + public function testPipe() + { + $callback = function ($stringable) { + return 'bar'; + }; + + $this->assertInstanceOf(Stringable::class, $this->stringable('foo')->pipe($callback)); + $this->assertSame('bar', (string) $this->stringable('foo')->pipe($callback)); + } } From 7f1138e362c6e124f4e1b327fc14d83eb80fffee Mon Sep 17 00:00:00 2001 From: BinotaLIU Date: Sun, 24 Jan 2021 16:01:02 +0800 Subject: [PATCH 2/3] make Stringable Tappable --- src/Illuminate/Support/Stringable.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Illuminate/Support/Stringable.php b/src/Illuminate/Support/Stringable.php index 1b9e1a0b6933..b718f98a090c 100644 --- a/src/Illuminate/Support/Stringable.php +++ b/src/Illuminate/Support/Stringable.php @@ -4,11 +4,12 @@ use Closure; use Illuminate\Support\Traits\Macroable; +use Illuminate\Support\Traits\Tappable; use Symfony\Component\VarDumper\VarDumper; class Stringable { - use Macroable; + use Tappable, Macroable; /** * The underlying string value. From dd0d096499349a88dd6b99cb6b985350c15833a3 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Sun, 24 Jan 2021 19:06:30 -0600 Subject: [PATCH 3/3] formatting --- src/Illuminate/Support/Stringable.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Illuminate/Support/Stringable.php b/src/Illuminate/Support/Stringable.php index b718f98a090c..e945beaf7614 100644 --- a/src/Illuminate/Support/Stringable.php +++ b/src/Illuminate/Support/Stringable.php @@ -9,7 +9,7 @@ class Stringable { - use Tappable, Macroable; + use Macroable, Tappable; /** * The underlying string value.