Last active
May 15, 2021 09:35
-
-
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"
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; | |
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