Skip to content

Commit

Permalink
Merge pull request #28507 from pascalbaljet/5.8
Browse files Browse the repository at this point in the history
[5.8] Added Tappable trait
  • Loading branch information
taylorotwell authored May 14, 2019
2 parents e459f37 + 9c90c78 commit b1fce05
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/Illuminate/Support/Traits/Tappable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Illuminate\Support\Traits;

trait Tappable
{
/**
* Call the given Closure with this instance then return the instance.
*
* @param callable|null $callback
* @return mixed
*/
public function tap($callback = null)
{
return tap($this, $callback);
}
}
47 changes: 47 additions & 0 deletions tests/Support/SupportTappableTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace Illuminate\Tests\Support;

use PHPUnit\Framework\TestCase;
use Illuminate\Support\Traits\Tappable;

class SupportTappableTest extends TestCase
{
public function testTappableClassWithCallback()
{
$name = TappableClass::make()->tap(function ($tappable) {
$tappable->setName('MyName');
})->getName();

$this->assertEquals('MyName', $name);
}

public function testTappableClassWithoutCallback()
{
$name = TappableClass::make()->tap()->setName('MyName')->getName();

$this->assertEquals('MyName', $name);
}
}

class TappableClass
{
use Tappable;

private $name;

public static function make()
{
return new static;
}

public function setName($name)
{
$this->name = $name;
}

public function getName()
{
return $this->name;
}
}

0 comments on commit b1fce05

Please sign in to comment.