-
Notifications
You must be signed in to change notification settings - Fork 11.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ship middleware to trim strings and convert empty strings to null.
- Loading branch information
1 parent
4643459
commit f578bbc
Showing
3 changed files
with
150 additions
and
0 deletions.
There are no files selected for viewing
18 changes: 18 additions & 0 deletions
18
src/Illuminate/Foundation/Http/Middleware/ConvertEmptyStringsToNull.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
namespace Illuminate\Foundation\Http\Middleware; | ||
|
||
class ConvertEmptyStringsToNull extends TransformsRequest | ||
{ | ||
/** | ||
* Transform the given value. | ||
* | ||
* @param string $key | ||
* @param mixed $value | ||
* @return mixed | ||
*/ | ||
protected function transform($key, $value) | ||
{ | ||
return is_string($value) && $value === '' ? null : $value; | ||
} | ||
} |
101 changes: 101 additions & 0 deletions
101
src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
<?php | ||
|
||
namespace Illuminate\Foundation\Http\Middleware; | ||
|
||
use Closure; | ||
use Symfony\Component\HttpFoundation\ParameterBag; | ||
|
||
class TransformsRequest | ||
{ | ||
/** | ||
* The additional attributes passed to the middleware. | ||
* | ||
* @var array | ||
*/ | ||
protected $attributes = []; | ||
|
||
/** | ||
* Handle an incoming request. | ||
* | ||
* @param \Illuminate\Http\Request $request | ||
* @param \Closure $next | ||
* @return mixed | ||
*/ | ||
public function handle($request, Closure $next, ...$attributes) | ||
{ | ||
$this->attributes = $attributes; | ||
|
||
$this->clean($request); | ||
|
||
return $next($request); | ||
} | ||
|
||
/** | ||
* Clean the request's data. | ||
* | ||
* @param \Illuminate\Http\Request $request | ||
* @return void | ||
*/ | ||
protected function clean($request) | ||
{ | ||
$this->cleanParameterBag($request->query); | ||
|
||
$this->cleanParameterBag($request->request); | ||
|
||
if ($request->isJson()) { | ||
$this->cleanParameterBag($request->json()); | ||
} | ||
} | ||
|
||
/** | ||
* Clean the data in the parameter bag. | ||
* | ||
* @param \Symfony\Component\HttpFoundation\ParameterBag $bag | ||
* @return void | ||
*/ | ||
protected function cleanParameterBag(ParameterBag $bag) | ||
{ | ||
$bag->replace($this->cleanArray($bag->all())); | ||
} | ||
|
||
/** | ||
* Clean the data in the given array. | ||
* | ||
* @param array $data | ||
* @return array | ||
*/ | ||
protected function cleanArray(array $data) | ||
{ | ||
return collect($data)->map(function ($value, $key) { | ||
return $this->cleanValue($key, $value); | ||
})->all(); | ||
} | ||
|
||
/** | ||
* Clean the given value. | ||
* | ||
* @param string $key | ||
* @param mixed $value | ||
* @return mixed | ||
*/ | ||
protected function cleanValue($key, $value) | ||
{ | ||
if (is_array($value)) { | ||
return $this->cleanArray($value); | ||
} | ||
|
||
return $this->transform($key, $value); | ||
} | ||
|
||
/** | ||
* Transform the given value. | ||
* | ||
* @param string $key | ||
* @param mixed $value | ||
* @return mixed | ||
*/ | ||
protected function transform($key, $value) | ||
{ | ||
return $value; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
namespace Illuminate\Foundation\Http\Middleware; | ||
|
||
class TrimStrings extends TransformsRequest | ||
{ | ||
/** | ||
* The attributes that should not be trimmed. | ||
* | ||
* @var array | ||
*/ | ||
protected $except = [ | ||
// | ||
]; | ||
|
||
/** | ||
* Transform the given value. | ||
* | ||
* @param string $key | ||
* @param mixed $value | ||
* @return mixed | ||
*/ | ||
protected function transform($key, $value) | ||
{ | ||
if (in_array($key, $this->except)) { | ||
return $value; | ||
} | ||
|
||
return is_string($value) ? trim($value) : $value; | ||
} | ||
} |
f578bbc
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@taylorotwell is there any reason why
ConvertEmptyStringsToNull
doesn't include exceptions likeTrimStrings
does?