Created
November 6, 2023 07:07
-
-
Save elnurxf/49e25f40427836b536b5656c6c377bc0 to your computer and use it in GitHub Desktop.
Azerbaijan Car Plates Validation Rule for Laravel
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\Rules; | |
use Closure; | |
use Illuminate\Contracts\Validation\ValidationRule; | |
class AzerbaijanCarPlates implements ValidationRule | |
{ | |
private const LICENSE_NUMBER_EXPRESSIONS = [ | |
'\d{2}[A-Z]{2}\d{3}', // 99-XX-999 - Avtomobil qeydiyyat nişanları | |
'[A-Z]{1}\d{3}\d{3}', // X-999-999 - Xarici vətəndaşa / şirkətə nümayəndiliyi aid avtomobil qeydiyyat nişanı | |
'\d{3}[A-Z]{1}\d{3}', // 999-X-999 - Diplomatik avtomobillər üçün qeydiyyat nişanları | |
'[A-Z]{2}\d{3}', // XX-999 - Tranzit avtomobillər üçün qeydiyyat nişanları | |
]; | |
/** | |
* Run the validation rule. | |
* | |
* @param \Closure(string): \Illuminate\Translation\PotentiallyTranslatedString $fail | |
*/ | |
public function validate(string $attribute, mixed $value, Closure $fail): void | |
{ | |
$licenseNumber = mb_strtoupper(str_replace(['-', ' '], '', $value)); | |
preg_match( | |
'/'.implode('|', self::LICENSE_NUMBER_EXPRESSIONS).'/', | |
$licenseNumber, | |
$matches | |
); | |
if(!in_array($licenseNumber, $matches)) { | |
$fail(__('Avtomobilin nömrəsi səhvdir')); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment