From 415e14dbb0118c49d61422954261597833525baa Mon Sep 17 00:00:00 2001 From: Iraldo Date: Wed, 13 Oct 2021 18:11:36 -0500 Subject: [PATCH 1/4] feat: now the request collection option allows a subset of inputs --- src/Illuminate/Http/Concerns/InteractsWithInput.php | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/Illuminate/Http/Concerns/InteractsWithInput.php b/src/Illuminate/Http/Concerns/InteractsWithInput.php index 147ca7b76e84..fb29e0e6121f 100644 --- a/src/Illuminate/Http/Concerns/InteractsWithInput.php +++ b/src/Illuminate/Http/Concerns/InteractsWithInput.php @@ -299,12 +299,15 @@ public function boolean($key = null, $default = false) /** * Retrieve input from the request as a collection. * - * @param string|null $key + * @param array|string|null $key * @return \Illuminate\Support\Collection */ public function collect($key = null) { - return collect($this->input($key)); + if (is_array($key)) + return collect($this->only($key)); + else + return collect($this->input($key)); } /** From a7667f60444a3f4c538ddf5b56e587219cb37d00 Mon Sep 17 00:00:00 2001 From: Iraldo Date: Wed, 13 Oct 2021 18:20:23 -0500 Subject: [PATCH 2/4] feat: now the request collection option allows a subset of inputs --- src/Illuminate/Http/Concerns/InteractsWithInput.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Illuminate/Http/Concerns/InteractsWithInput.php b/src/Illuminate/Http/Concerns/InteractsWithInput.php index fb29e0e6121f..20efd7817839 100644 --- a/src/Illuminate/Http/Concerns/InteractsWithInput.php +++ b/src/Illuminate/Http/Concerns/InteractsWithInput.php @@ -304,10 +304,11 @@ public function boolean($key = null, $default = false) */ public function collect($key = null) { - if (is_array($key)) + if (is_array($key)) { return collect($this->only($key)); - else + } else { return collect($this->input($key)); + } } /** From 627bff694356b1d17603d45adc263f68a7a08dc3 Mon Sep 17 00:00:00 2001 From: Iraldo Date: Thu, 14 Oct 2021 15:46:38 -0500 Subject: [PATCH 3/4] feat: added some tests and improvements --- src/Illuminate/Http/Concerns/InteractsWithInput.php | 6 +----- tests/Http/HttpRequestTest.php | 7 +++++++ 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/src/Illuminate/Http/Concerns/InteractsWithInput.php b/src/Illuminate/Http/Concerns/InteractsWithInput.php index 20efd7817839..d375b910ed7c 100644 --- a/src/Illuminate/Http/Concerns/InteractsWithInput.php +++ b/src/Illuminate/Http/Concerns/InteractsWithInput.php @@ -304,11 +304,7 @@ public function boolean($key = null, $default = false) */ public function collect($key = null) { - if (is_array($key)) { - return collect($this->only($key)); - } else { - return collect($this->input($key)); - } + return collect(is_array($key) ? $this->only($key) : $this->input($key)); } /** diff --git a/tests/Http/HttpRequestTest.php b/tests/Http/HttpRequestTest.php index 17bbc5927b22..168b2c30bb0b 100644 --- a/tests/Http/HttpRequestTest.php +++ b/tests/Http/HttpRequestTest.php @@ -521,6 +521,13 @@ public function testCollectMethod() $request = Request::create('/', 'GET', []); $this->assertInstanceOf(Collection::class, $request->collect()); $this->assertTrue($request->collect()->isEmpty()); + + $request = Request::create('/', 'GET', ['users' => [1, 2, 3], 'roles' => [4, 5, 6]]); + $this->assertInstanceOf(Collection::class, $request->collect(['users'])); + $this->assertTrue($request->collect(['developers'])->isEmpty()); + $this->assertTrue($request->collect(['roles'])->isNotEmpty()); + $this->assertEquals(['roles' => [4, 5, 6]], $request->collect(['roles'])->all()); + $this->assertEquals(['users' => [1, 2, 3], 'roles' => [4, 5, 6]], $request->collect()->all()); } public function testArrayAccess() From 9697198402a2be72b9db5bea6ad680cc1735732e Mon Sep 17 00:00:00 2001 From: Iraldo Date: Fri, 15 Oct 2021 09:26:00 -0500 Subject: [PATCH 4/4] feat: testing improvements - more request parameters are added - more tests are added with different keys --- tests/Http/HttpRequestTest.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tests/Http/HttpRequestTest.php b/tests/Http/HttpRequestTest.php index 168b2c30bb0b..787c789eed69 100644 --- a/tests/Http/HttpRequestTest.php +++ b/tests/Http/HttpRequestTest.php @@ -522,12 +522,14 @@ public function testCollectMethod() $this->assertInstanceOf(Collection::class, $request->collect()); $this->assertTrue($request->collect()->isEmpty()); - $request = Request::create('/', 'GET', ['users' => [1, 2, 3], 'roles' => [4, 5, 6]]); + $request = Request::create('/', 'GET', ['users' => [1, 2, 3], 'roles' => [4, 5, 6], 'foo' => ['bar', 'baz'], 'email' => 'test@example.com']); $this->assertInstanceOf(Collection::class, $request->collect(['users'])); $this->assertTrue($request->collect(['developers'])->isEmpty()); $this->assertTrue($request->collect(['roles'])->isNotEmpty()); $this->assertEquals(['roles' => [4, 5, 6]], $request->collect(['roles'])->all()); - $this->assertEquals(['users' => [1, 2, 3], 'roles' => [4, 5, 6]], $request->collect()->all()); + $this->assertEquals(['users' => [1, 2, 3], 'email' => 'test@example.com'], $request->collect(['users', 'email'])->all()); + $this->assertEquals(collect(['roles' => [4, 5, 6], 'foo' => ['bar', 'baz']]), $request->collect(['roles', 'foo'])); + $this->assertEquals(['users' => [1, 2, 3], 'roles' => [4, 5, 6], 'foo' => ['bar', 'baz'], 'email' => 'test@example.com'], $request->collect()->all()); } public function testArrayAccess()