diff --git a/app/Http/Controllers/EjaculationController.php b/app/Http/Controllers/EjaculationController.php index 4490677..d2292e0 100644 --- a/app/Http/Controllers/EjaculationController.php +++ b/app/Http/Controllers/EjaculationController.php @@ -30,9 +30,6 @@ class EjaculationController extends Controller public function store(Request $request) { $inputs = $request->all(); - if ($request->has('note')) { - $inputs['note'] = str_replace(["\r\n", "\r"], "\n", $inputs['note']); - } $validator = Validator::make($inputs, [ 'date' => 'required|date_format:Y/m/d', @@ -113,9 +110,6 @@ class EjaculationController extends Controller $ejaculation = Ejaculation::findOrFail($id); $inputs = $request->all(); - if ($request->has('note')) { - $inputs['note'] = str_replace(["\r\n", "\r"], "\n", $inputs['note']); - } $validator = Validator::make($inputs, [ 'date' => 'required|date_format:Y/m/d', diff --git a/app/Http/Kernel.php b/app/Http/Kernel.php index 93bf68b..95714cf 100644 --- a/app/Http/Kernel.php +++ b/app/Http/Kernel.php @@ -35,6 +35,7 @@ class Kernel extends HttpKernel \Illuminate\View\Middleware\ShareErrorsFromSession::class, \App\Http\Middleware\VerifyCsrfToken::class, \Illuminate\Routing\Middleware\SubstituteBindings::class, + \App\Http\Middleware\NormalizeLineEnding::class, ], 'api' => [ diff --git a/app/Http/Middleware/NormalizeLineEnding.php b/app/Http/Middleware/NormalizeLineEnding.php new file mode 100644 index 0000000..e0ec87e --- /dev/null +++ b/app/Http/Middleware/NormalizeLineEnding.php @@ -0,0 +1,30 @@ +input() as $key => $value) { + $newInput[$key] = str_replace(["\r\n", "\r"], "\n", $value); + } + $request->replace($newInput); + + return $next($request); + } +} diff --git a/tests/Unit/Http/Middleware/NormalizeLineEndingTest.php b/tests/Unit/Http/Middleware/NormalizeLineEndingTest.php new file mode 100644 index 0000000..7d16f0b --- /dev/null +++ b/tests/Unit/Http/Middleware/NormalizeLineEndingTest.php @@ -0,0 +1,80 @@ +replace([ + 'test' => "foo\r\nbar" + ]); + + $middleware = new NormalizeLineEnding(); + + $middleware->handle($request, function (Request $request) { + $this->assertEquals("foo\nbar", $request->input('test')); + }); + } + + public function testCRtoLF() + { + $request = Request::create('/'); + $request->replace([ + 'test' => "foo\rbar" + ]); + + $middleware = new NormalizeLineEnding(); + + $middleware->handle($request, function (Request $request) { + $this->assertEquals("foo\nbar", $request->input('test')); + }); + } + + public function testLFtoLF() + { + $request = Request::create('/'); + $request->replace([ + 'test' => "foo\nbar" + ]); + + $middleware = new NormalizeLineEnding(); + + $middleware->handle($request, function (Request $request) { + $this->assertEquals("foo\nbar", $request->input('test')); + }); + } + + public function testArrayRequest() + { + $request = Request::create('/'); + $request->replace([ + 'test' => "foo\r\nbar", + 'hash' => [ + 'yuzuki' => "yuzuki\r\nyukari", + 'miku' => "hatsune\r\nmiku", + ], + 'array' => [ + "kagamine\r\nrin", + "kagamine\r\nlen" + ] + ]); + + $middleware = new NormalizeLineEnding(); + + $middleware->handle($request, function (Request $request) { + $this->assertEquals("foo\nbar", $request->input('test')); + $this->assertEquals("yuzuki\nyukari", $request->input('hash.yuzuki')); + $this->assertEquals("hatsune\nmiku", $request->input('hash.miku')); + $this->assertEquals("kagamine\nrin", $request->input('array.0')); + $this->assertEquals("kagamine\nlen", $request->input('array.1')); + }); + } +}