Merge pull request #154 from shikorism/feature/normalize-line-ending
改行コードを正規化する処理をミドルウェア化
This commit is contained in:
commit
a72190eda5
@ -30,9 +30,6 @@ class EjaculationController extends Controller
|
|||||||
public function store(Request $request)
|
public function store(Request $request)
|
||||||
{
|
{
|
||||||
$inputs = $request->all();
|
$inputs = $request->all();
|
||||||
if ($request->has('note')) {
|
|
||||||
$inputs['note'] = str_replace(["\r\n", "\r"], "\n", $inputs['note']);
|
|
||||||
}
|
|
||||||
|
|
||||||
$validator = Validator::make($inputs, [
|
$validator = Validator::make($inputs, [
|
||||||
'date' => 'required|date_format:Y/m/d',
|
'date' => 'required|date_format:Y/m/d',
|
||||||
@ -113,9 +110,6 @@ class EjaculationController extends Controller
|
|||||||
$ejaculation = Ejaculation::findOrFail($id);
|
$ejaculation = Ejaculation::findOrFail($id);
|
||||||
|
|
||||||
$inputs = $request->all();
|
$inputs = $request->all();
|
||||||
if ($request->has('note')) {
|
|
||||||
$inputs['note'] = str_replace(["\r\n", "\r"], "\n", $inputs['note']);
|
|
||||||
}
|
|
||||||
|
|
||||||
$validator = Validator::make($inputs, [
|
$validator = Validator::make($inputs, [
|
||||||
'date' => 'required|date_format:Y/m/d',
|
'date' => 'required|date_format:Y/m/d',
|
||||||
|
@ -35,6 +35,7 @@ class Kernel extends HttpKernel
|
|||||||
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
|
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
|
||||||
\App\Http\Middleware\VerifyCsrfToken::class,
|
\App\Http\Middleware\VerifyCsrfToken::class,
|
||||||
\Illuminate\Routing\Middleware\SubstituteBindings::class,
|
\Illuminate\Routing\Middleware\SubstituteBindings::class,
|
||||||
|
\App\Http\Middleware\NormalizeLineEnding::class,
|
||||||
],
|
],
|
||||||
|
|
||||||
'api' => [
|
'api' => [
|
||||||
|
30
app/Http/Middleware/NormalizeLineEnding.php
Normal file
30
app/Http/Middleware/NormalizeLineEnding.php
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Http\Middleware;
|
||||||
|
|
||||||
|
use Closure;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* リクエスト内の改行コードを正規化する。
|
||||||
|
* @package App\Http\Middleware
|
||||||
|
*/
|
||||||
|
class NormalizeLineEnding
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Handle an incoming request.
|
||||||
|
*
|
||||||
|
* @param \Illuminate\Http\Request $request
|
||||||
|
* @param \Closure $next
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
public function handle($request, Closure $next)
|
||||||
|
{
|
||||||
|
$newInput = [];
|
||||||
|
foreach ($request->input() as $key => $value) {
|
||||||
|
$newInput[$key] = str_replace(["\r\n", "\r"], "\n", $value);
|
||||||
|
}
|
||||||
|
$request->replace($newInput);
|
||||||
|
|
||||||
|
return $next($request);
|
||||||
|
}
|
||||||
|
}
|
80
tests/Unit/Http/Middleware/NormalizeLineEndingTest.php
Normal file
80
tests/Unit/Http/Middleware/NormalizeLineEndingTest.php
Normal file
@ -0,0 +1,80 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Tests\Unit\Http\Middleware;
|
||||||
|
|
||||||
|
use App\Http\Middleware\NormalizeLineEnding;
|
||||||
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||||
|
use Illuminate\Foundation\Testing\WithFaker;
|
||||||
|
use Illuminate\Http\Request;
|
||||||
|
use Tests\TestCase;
|
||||||
|
|
||||||
|
class NormalizeLineEndingTest extends TestCase
|
||||||
|
{
|
||||||
|
public function testCRLFtoLF()
|
||||||
|
{
|
||||||
|
$request = Request::create('/');
|
||||||
|
$request->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'));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user