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

[6.x] Add "missing" method to Request class #30320

Merged
merged 4 commits into from
Oct 18, 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
13 changes: 13 additions & 0 deletions src/Illuminate/Http/Concerns/InteractsWithInput.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,19 @@ public function anyFilled($keys)
return false;
}

/**
* Determine if the request is missing a given input item key.
*
* @param string|array $key
* @return bool
*/
public function missing($key)
{
$keys = is_array($key) ? $key : func_get_args();

return ! $this->has($keys);
}

/**
* Determine if the given input key is an empty string for "has".
*
Expand Down
25 changes: 25 additions & 0 deletions tests/Http/HttpRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,31 @@ public function testHasMethod()
$this->assertTrue($request->has('foo.baz'));
}

public function testMissingMethod()
{
$request = Request::create('/', 'GET', ['name' => 'Taylor', 'age' => '', 'city' => null]);
$this->assertFalse($request->missing('name'));
$this->assertFalse($request->missing('age'));
$this->assertFalse($request->missing('city'));
$this->assertTrue($request->missing('foo'));
$this->assertTrue($request->missing('name', 'email'));

$request = Request::create('/', 'GET', ['name' => 'Taylor', 'email' => 'foo']);
$this->assertFalse($request->missing('name'));
$this->assertFalse($request->missing('name', 'email'));

$request = Request::create('/', 'GET', ['foo' => ['bar', 'bar']]);
$this->assertFalse($request->missing('foo'));

$request = Request::create('/', 'GET', ['foo' => '', 'bar' => null]);
$this->assertFalse($request->missing('foo'));
$this->assertFalse($request->missing('bar'));

$request = Request::create('/', 'GET', ['foo' => ['bar' => null, 'baz' => '']]);
$this->assertFalse($request->missing('foo.bar'));
$this->assertFalse($request->missing('foo.baz'));
}

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