From 8f7f7eeb8a196bf77bf6f215151390dcf8641503 Mon Sep 17 00:00:00 2001 From: tiennt Date: Sun, 27 Aug 2023 15:52:30 +0700 Subject: [PATCH] feat(TrimStrings): support nested attribute --- .../Http/Middleware/TrimStrings.php | 5 +- .../Http/Middleware/TrimStringsTest.php | 64 +++++++++++++++++++ 2 files changed, 68 insertions(+), 1 deletion(-) diff --git a/src/Illuminate/Foundation/Http/Middleware/TrimStrings.php b/src/Illuminate/Foundation/Http/Middleware/TrimStrings.php index 7ffcd5eaa1f7..f5f036da3c10 100644 --- a/src/Illuminate/Foundation/Http/Middleware/TrimStrings.php +++ b/src/Illuminate/Foundation/Http/Middleware/TrimStrings.php @@ -3,6 +3,7 @@ namespace Illuminate\Foundation\Http\Middleware; use Closure; +use Illuminate\Support\Str; class TrimStrings extends TransformsRequest { @@ -49,7 +50,9 @@ public function handle($request, Closure $next) */ protected function transform($key, $value) { - if (in_array($key, $this->except, true) || ! is_string($value)) { + $attribute = Str::afterLast($key, '.'); + + if (in_array($key, $this->except, true) || in_array($attribute, $this->except, true) || ! is_string($value)) { return $value; } diff --git a/tests/Foundation/Http/Middleware/TrimStringsTest.php b/tests/Foundation/Http/Middleware/TrimStringsTest.php index 4eac95c101f3..c0862916f0a1 100644 --- a/tests/Foundation/Http/Middleware/TrimStringsTest.php +++ b/tests/Foundation/Http/Middleware/TrimStringsTest.php @@ -17,6 +17,22 @@ public function testTrimStringsIgnoringExceptAttribute() 'xyz' => ' 456 ', 'foo' => ' 789 ', 'bar' => ' 010 ', + /**/ + 'temp' => [ + 'abc' => ' 123 ', + 'xyz' => ' 456 ', + 'foo' => ' 789 ', + 'bar' => ' 010 ', + ], + /**/ + 'temps' => [ + [ + 'abc' => ' 123 ', + 'xyz' => ' 456 ', + 'foo' => ' 789 ', + 'bar' => ' 010 ', + ], + ], ]); $symfonyRequest->server->set('REQUEST_METHOD', 'GET'); $request = Request::createFromBase($symfonyRequest); @@ -26,6 +42,16 @@ public function testTrimStringsIgnoringExceptAttribute() $this->assertSame('456', $request->get('xyz')); $this->assertSame(' 789 ', $request->get('foo')); $this->assertSame(' 010 ', $request->get('bar')); + /**/ + $this->assertSame('123', $request->input('temp.abc')); + $this->assertSame('456', $request->input('temp.xyz')); + $this->assertSame(' 789 ', $request->input('temp.foo')); + $this->assertSame(' 010 ', $request->input('temp.bar')); + /**/ + $this->assertSame('123', $request->input('temps.0.abc')); + $this->assertSame('456', $request->input('temps.0.xyz')); + $this->assertSame(' 789 ', $request->input('temps.0.foo')); + $this->assertSame(' 010 ', $request->input('temps.0.bar')); }); } @@ -42,6 +68,28 @@ public function testTrimStringsNBSP() 'bar' => '  だ   ', 'baz' => '  ム   ', 'binary' => " \xE9 ", + /**/ + 'temp' => [ + 'abc' => '  123   ', + 'zwnbsp' => ' ha ', + 'xyz' => 'だ', + 'foo' => 'ム', + 'bar' => '  だ   ', + 'baz' => '  ム   ', + 'binary' => " \xE9 ", + ], + /**/ + 'temps' => [ + [ + 'abc' => '  123   ', + 'zwnbsp' => ' ha ', + 'xyz' => 'だ', + 'foo' => 'ム', + 'bar' => '  だ   ', + 'baz' => '  ム   ', + 'binary' => " \xE9 ", + ], + ], ]); $symfonyRequest->server->set('REQUEST_METHOD', 'GET'); $request = Request::createFromBase($symfonyRequest); @@ -54,6 +102,22 @@ public function testTrimStringsNBSP() $this->assertSame('だ', $request->get('bar')); $this->assertSame('ム', $request->get('baz')); $this->assertSame("\xE9", $request->get('binary')); + /**/ + $this->assertSame('123', $request->input('temp.abc')); + $this->assertSame('ha', $request->input('temp.zwnbsp')); + $this->assertSame('だ', $request->input('temp.xyz')); + $this->assertSame('ム', $request->input('temp.foo')); + $this->assertSame('だ', $request->input('temp.bar')); + $this->assertSame('ム', $request->input('temp.baz')); + $this->assertSame("\xE9", $request->input('temp.binary')); + /**/ + $this->assertSame('123', $request->input('temps.0.abc')); + $this->assertSame('ha', $request->input('temps.0.zwnbsp')); + $this->assertSame('だ', $request->input('temps.0.xyz')); + $this->assertSame('ム', $request->input('temps.0.foo')); + $this->assertSame('だ', $request->input('temps.0.bar')); + $this->assertSame('ム', $request->input('temps.0.baz')); + $this->assertSame("\xE9", $request->input('temps.0.binary')); }); } }