Skip to content

Instantly share code, notes, and snippets.

@samolabams
Created June 14, 2018 17:43
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save samolabams/ed67bc7782ace3f6cbd80959bcdee007 to your computer and use it in GitHub Desktop.
Save samolabams/ed67bc7782ace3f6cbd80959bcdee007 to your computer and use it in GitHub Desktop.
A laravel middleware that can transform request data
<?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();
}
}
@vahidalvandi
Copy link

i use it in laravel and php 7 work fine but not work in php 8 and laravel 8

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment