Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.8] Added Tappable trait #28507

Merged
merged 2 commits into from
May 14, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}