Skip to content

Commit

Permalink
Merge branch 'feature/tap-do-stringable' into 8.x
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed Jan 25, 2021
2 parents ae790fe + dd0d096 commit 3ab9f7a
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/Illuminate/Support/Stringable.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 Macroable, Tappable;

/**
* The underlying string value.
Expand Down Expand Up @@ -399,6 +400,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.
*
Expand Down
24 changes: 24 additions & 0 deletions tests/Support/SupportStringableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}

0 comments on commit 3ab9f7a

Please sign in to comment.