Skip to content

Commit

Permalink
[6.x] Add "missing" method to Request class (#30320)
Browse files Browse the repository at this point in the history
* Update InteractsWithInput.php

* Update HttpRequestTest.php

* Update InteractsWithInput.php

* Update InteractsWithInput.php
  • Loading branch information
TitasGailius authored and taylorotwell committed Oct 18, 2019
1 parent 19c2a10 commit 91553d8
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
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

0 comments on commit 91553d8

Please sign in to comment.