Skip to content

Commit

Permalink
[8.x] Ability to return the default value of a request whenHas and wh…
Browse files Browse the repository at this point in the history
…enFilled methods (#38060)

* Add the ability to return the default value of a request whenHas and whenFilled methods

* Change functionality and test fixes

Co-authored-by: Olek Kaim <aleksander.kaim@ftggroup.com>
  • Loading branch information
olekjs and Olek Kaim authored Jul 19, 2021
1 parent 5880e9f commit c7824d4
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
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

0 comments on commit c7824d4

Please sign in to comment.