Skip to content

Commit

Permalink
Merge branch 'pascalbaljet/7.x' into 7.x
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed Aug 14, 2020
2 parents f228374 + 9ed3046 commit c17b6a1
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 0 deletions.
32 changes: 32 additions & 0 deletions src/Illuminate/Http/Concerns/InteractsWithInput.php
Expand Up @@ -106,6 +106,22 @@ public function hasAny($keys)
return Arr::hasAny($input, $keys);
}

/**
* Apply the callback if the request contains the given input item key.
*
* @param string $key
* @param callable $callback
* @return $this|mixed
*/
public function whenHas($key, callable $callback)
{
if ($this->has($key)) {
return $callback(data_get($this->all(), $key)) ?: $this;
}

return $this;
}

/**
* Determine if the request contains a non-empty value for an input item.
*
Expand Down Expand Up @@ -163,6 +179,22 @@ public function anyFilled($keys)
return false;
}

/**
* Apply the callback if the request contains a non-empty value for the given input item key.
*
* @param string $key
* @param callable $callback
* @return $this|mixed
*/
public function whenFilled($key, callable $callback)
{
if ($this->filled($key)) {
return $callback(data_get($this->all(), $key)) ?: $this;
}

return $this;
}

/**
* Determine if the request is missing a given input item key.
*
Expand Down
56 changes: 56 additions & 0 deletions tests/Http/HttpRequestTest.php
Expand Up @@ -300,6 +300,62 @@ public function testHasMethod()
$this->assertTrue($request->has('foo.baz'));
}

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

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

$request->whenHas('name', function ($value) use (&$name) {
$name = $value;
});

$request->whenHas('age', function ($value) use (&$age) {
$age = $value;
});

$request->whenHas('city', function ($value) use (&$city) {
$city = $value;
});

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

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

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

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

$request->whenFilled('name', function ($value) use (&$name) {
$name = $value;
});

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

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

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

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

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

0 comments on commit c17b6a1

Please sign in to comment.