From 7e1041379051bfb5b103ef78a56fa972eb684891 Mon Sep 17 00:00:00 2001 From: Tim MacDonald Date: Mon, 12 Mar 2018 14:07:42 +1100 Subject: [PATCH 1/3] add filledAny() method --- .../Http/Concerns/InteractsWithInput.php | 19 +++++++++++++++++++ tests/Http/HttpRequestTest.php | 17 +++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/src/Illuminate/Http/Concerns/InteractsWithInput.php b/src/Illuminate/Http/Concerns/InteractsWithInput.php index 7e20f5d23010..cc97412b0c8f 100644 --- a/src/Illuminate/Http/Concerns/InteractsWithInput.php +++ b/src/Illuminate/Http/Concerns/InteractsWithInput.php @@ -131,6 +131,25 @@ public function filled($key) return true; } + /** + * Determine if the request contains a non-empty value for any of the given inputs. + * + * @param string|array $keys + * @return bool + */ + public function filledAny($keys) + { + $keys = is_array($keys) ? $keys : func_get_args(); + + foreach ($keys as $key) { + if ($this->filled($key)) { + return true; + } + } + + return false; + } + /** * Determine if the given input key is an empty string for "has". * diff --git a/tests/Http/HttpRequestTest.php b/tests/Http/HttpRequestTest.php index 7e59352bb881..2f32423356bf 100644 --- a/tests/Http/HttpRequestTest.php +++ b/tests/Http/HttpRequestTest.php @@ -297,6 +297,23 @@ public function testFilledMethod() $this->assertTrue($request->filled('foo.bar')); } + public function testFilledAnyMethod() + { + $request = Request::create('/', 'GET', ['name' => 'Taylor', 'age' => '', 'city' => null]); + + $this->assertTrue($request->filledAny(['name'])); + $this->assertTrue($request->filledAny('name')); + + $this->assertTrue($request->filledAny(['age', 'name'])); + $this->assertTrue($request->filledAny('age', 'name')); + + $this->assertTrue($request->filledAny(['foo', 'name'])); + $this->assertTrue($request->filledAny('foo', 'name')); + + $this->assertFalse($request->filledAny('age', 'city')); + $this->assertFalse($request->filledAny('age', 'city')); + } + public function testInputMethod() { $request = Request::create('/', 'GET', ['name' => 'Taylor']); From 4bb96d1f8a415023632064e9ec114ed32fe2ae0d Mon Sep 17 00:00:00 2001 From: Tim MacDonald Date: Mon, 12 Mar 2018 14:09:00 +1100 Subject: [PATCH 2/3] fix doc block --- src/Illuminate/Http/Concerns/InteractsWithInput.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Illuminate/Http/Concerns/InteractsWithInput.php b/src/Illuminate/Http/Concerns/InteractsWithInput.php index cc97412b0c8f..9080ba174ac9 100644 --- a/src/Illuminate/Http/Concerns/InteractsWithInput.php +++ b/src/Illuminate/Http/Concerns/InteractsWithInput.php @@ -131,7 +131,7 @@ public function filled($key) return true; } - /** + /** * Determine if the request contains a non-empty value for any of the given inputs. * * @param string|array $keys From 5403d02c8f479855eeaaeb39935c0effc1b412bb Mon Sep 17 00:00:00 2001 From: Tim MacDonald Date: Mon, 12 Mar 2018 14:16:34 +1100 Subject: [PATCH 3/3] add more test variations --- tests/Http/HttpRequestTest.php | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/tests/Http/HttpRequestTest.php b/tests/Http/HttpRequestTest.php index 2f32423356bf..773a6849afe1 100644 --- a/tests/Http/HttpRequestTest.php +++ b/tests/Http/HttpRequestTest.php @@ -304,6 +304,12 @@ public function testFilledAnyMethod() $this->assertTrue($request->filledAny(['name'])); $this->assertTrue($request->filledAny('name')); + $this->assertFalse($request->filledAny(['age'])); + $this->assertFalse($request->filledAny('age')); + + $this->assertFalse($request->filledAny(['foo'])); + $this->assertFalse($request->filledAny('foo')); + $this->assertTrue($request->filledAny(['age', 'name'])); $this->assertTrue($request->filledAny('age', 'name')); @@ -312,6 +318,9 @@ public function testFilledAnyMethod() $this->assertFalse($request->filledAny('age', 'city')); $this->assertFalse($request->filledAny('age', 'city')); + + $this->assertFalse($request->filledAny('foo', 'bar')); + $this->assertFalse($request->filledAny('foo', 'bar')); } public function testInputMethod()