From e523885cf71e11441fbd8441d6ec2c180974abb7 Mon Sep 17 00:00:00 2001 From: Kennedy Tedesco Date: Mon, 25 Sep 2017 16:59:47 -0300 Subject: [PATCH] [5.5] Add anyFilled() to Request Currently I have a macro for it. Looks like a good idea put it on the Request class. --- .../Http/Concerns/InteractsWithInput.php | 19 +++++++++++++++++++ tests/Http/HttpRequestTest.php | 15 +++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/src/Illuminate/Http/Concerns/InteractsWithInput.php b/src/Illuminate/Http/Concerns/InteractsWithInput.php index c66c49e7f6bd..87004f019d8f 100644 --- a/src/Illuminate/Http/Concerns/InteractsWithInput.php +++ b/src/Illuminate/Http/Concerns/InteractsWithInput.php @@ -129,6 +129,25 @@ public function filled($key) return true; } + /** + * Determine if any of the given inputs are not empty. + * + * @param string|array $key + * @return bool + */ + public function anyFilled($key) + { + $keys = is_array($key) ? $key : func_get_args(); + + foreach ($keys as $value) { + if (! $this->isEmptyString($value)) { + 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 01f03453f8e2..935841af250d 100644 --- a/tests/Http/HttpRequestTest.php +++ b/tests/Http/HttpRequestTest.php @@ -282,6 +282,21 @@ public function testFilledMethod() $this->assertTrue($request->filled('foo.bar')); } + public function testAnyFilledMethod() + { + $request = Request::create('/', 'GET', ['name' => 'Taylor', 'age' => '', 'city' => null]); + $this->assertTrue($request->anyFilled('name')); + $this->assertTrue($request->anyFilled('age', 'name')); + $this->assertTrue($request->anyFilled(['age', 'city', 'name'])); + $this->assertFalse($request->anyFilled(['age', 'city'])); + $this->assertFalse($request->anyFilled('age')); + + $request = Request::create('/', 'GET', ['foo' => ['bar' => 'baz', 'baz' => '']]); + $this->assertTrue($request->anyFilled('foo.bar')); + $this->assertTrue($request->anyFilled(['foo.baz', 'foo.bar'])); + $this->assertFalse($request->anyFilled('foo.baz')); + } + public function testInputMethod() { $request = Request::create('/', 'GET', ['name' => 'Taylor']);