Skip to content

Instantly share code, notes, and snippets.

@AmirFayaz
Last active May 15, 2021 09:35
Show Gist options
  • Save AmirFayaz/12bc535aef91a557412306c7e3cd14d0 to your computer and use it in GitHub Desktop.
Save AmirFayaz/12bc535aef91a557412306c7e3cd14d0 to your computer and use it in GitHub Desktop.
A Middleware for Laravel/Lumen, Which "Trims" Requests, Removes "White Spaces" and Converts "Persian/Arabic Numbers to English Ones"
<?php
namespace App\Http\Middleware;
use Closure;
class RequestBeautifier
{
protected $except = [
'password',
'password_confirmation',
];
public function handle($request, Closure $next, $guard = null)
{
foreach ($request->all() as $key => $value)
{
!in_array( (string)$key,array_values($this->except) ) && $request->merge([$key => $this->beautify($value)]);
}
return $next($request);
}
public function beautify($input)
{
if(is_array($input))
{
foreach ($input as $key => $inp)
{
$input[$key] = !in_array((string)$key,$this->except) ? $this->beautify($inp) : $inp;
}
return $input;
}
$input = preg_replace('/\s+/', ' ', trim($input));
$persian = ['۰', '۱', '۲', '۳', '۴', '٤', '۵', '٥', '٦', '۶', '۷', '۸', '۹'];
$english = [ 0 , 1 , 2 , 3 , 4 , 4 , 5 , 5 , 6 , 6 , 7 , 8 , 9 ];
return str_replace($persian, $english, $input);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment