Created
June 14, 2018 17:43
-
-
Save samolabams/ed67bc7782ace3f6cbd80959bcdee007 to your computer and use it in GitHub Desktop.
A laravel middleware that can transform request data
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
<?php | |
namespace App\Http\Middleware; | |
use Closure; | |
use Symfony\Component\HttpFoundation\ParameterBag; | |
class TransformData | |
{ | |
/** | |
* Handle an incoming request. | |
* | |
* @param \Illuminate\Http\Request $request | |
* @param \Closure $next | |
* @return mixed | |
*/ | |
public function handle($request, Closure $next) | |
{ | |
if ($request->isJson()) { | |
$this->clean($request->json()); | |
} else { | |
$this->clean($request->request); | |
} | |
return $next($request); | |
} | |
/** | |
* Clean the request's data by removing mask from phonenumber. | |
* | |
* @param \Symfony\Component\HttpFoundation\ParameterBag $bag | |
* @return void | |
*/ | |
private function clean(ParameterBag $bag) | |
{ | |
$bag->replace($this->cleanData($bag->all())); | |
} | |
/** | |
* Check the parameters and clean the number | |
* | |
* @param array $data | |
* @return array | |
*/ | |
private function cleanData(array $data) | |
{ | |
return collect($data)->map(function ($value, $key) { | |
if ($key == 'payment_amount') { | |
return preg_replace("/([^0-9])/", '', $value); | |
} else { | |
return $value; | |
} | |
})->all(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
i use it in laravel and php 7 work fine but not work in php 8 and laravel 8