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.5] Allow passing a callback to "with" #21445

Merged
merged 1 commit into from
Sep 28, 2017
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
Allow passing a callback to "with"
  • Loading branch information
JosephSilber committed Sep 28, 2017
commit 89db1925b1d4b24a794280c41eb84bf4b9b6fa76
9 changes: 5 additions & 4 deletions src/Illuminate/Support/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -1154,13 +1154,14 @@ function windows_os()

if (! function_exists('with')) {
/**
* Return the given object. Useful for chaining.
* Return the given value, optionaly passed through the given callback.
*
* @param mixed $object
* @param mixed $value
* @param callable|null $callback
* @return mixed
*/
function with($object)
function with($value, callable $callback = null)
{
return $object;
return is_null($callback) ? $value : $callback($value);
}
}
9 changes: 9 additions & 0 deletions tests/Support/SupportHelpersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -804,6 +804,15 @@ public function testTransform()
return 10;
}));
}

public function testWith()
{
$this->assertEquals(10, with(10));

$this->assertEquals(10, with(5, function ($five) {
return $five + 5;
}));
}
}

trait SupportTestTraitOne
Expand Down