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

[8.x] Ability to return the default value of a request whenHas and whenFilled methods #38060

Merged
merged 3 commits into from
Jul 19, 2021
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
14 changes: 12 additions & 2 deletions src/Illuminate/Http/Concerns/InteractsWithInput.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,14 +112,19 @@ public function hasAny($keys)
*
* @param string $key
* @param callable $callback
* @param callable|null $default
* @return $this|mixed
*/
public function whenHas($key, callable $callback)
public function whenHas($key, callable $callback, callable $default = null)
{
if ($this->has($key)) {
return $callback(data_get($this->all(), $key)) ?: $this;
}

if ($default) {
return $default();
}

return $this;
}

Expand Down Expand Up @@ -185,14 +190,19 @@ public function anyFilled($keys)
*
* @param string $key
* @param callable $callback
* @param callable|null $default
* @return $this|mixed
*/
public function whenFilled($key, callable $callback)
public function whenFilled($key, callable $callback, callable $default = null)
{
if ($this->filled($key)) {
return $callback(data_get($this->all(), $key)) ?: $this;
}

if ($default) {
return $default();
}

return $this;
}

Expand Down
18 changes: 16 additions & 2 deletions tests/Http/HttpRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ public function testWhenHasMethod()
{
$request = Request::create('/', 'GET', ['name' => 'Taylor', 'age' => '', 'city' => null]);

$name = $age = $city = $foo = false;
$name = $age = $city = $foo = $bar = false;

$request->whenHas('name', function ($value) use (&$name) {
$name = $value;
Expand All @@ -322,17 +322,24 @@ public function testWhenHasMethod()
$foo = 'test';
});

$request->whenHas('bar', function () use (&$bar) {
$bar = 'test';
}, function () use (&$bar) {
$bar = true;
});

$this->assertSame('Taylor', $name);
$this->assertSame('', $age);
$this->assertNull($city);
$this->assertFalse($foo);
$this->assertTrue($bar);
}

public function testWhenFilledMethod()
{
$request = Request::create('/', 'GET', ['name' => 'Taylor', 'age' => '', 'city' => null]);

$name = $age = $city = $foo = false;
$name = $age = $city = $foo = $bar = false;

$request->whenFilled('name', function ($value) use (&$name) {
$name = $value;
Expand All @@ -350,10 +357,17 @@ public function testWhenFilledMethod()
$foo = 'test';
});

$request->whenFilled('bar', function () use (&$bar) {
$bar = 'test';
}, function () use (&$bar) {
$bar = true;
});

$this->assertSame('Taylor', $name);
$this->assertFalse($age);
$this->assertFalse($city);
$this->assertFalse($foo);
$this->assertTrue($bar);
}

public function testMissingMethod()
Expand Down