Skip to content

Instantly share code, notes, and snippets.

@elnurxf
Created November 6, 2023 07:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elnurxf/49e25f40427836b536b5656c6c377bc0 to your computer and use it in GitHub Desktop.
Save elnurxf/49e25f40427836b536b5656c6c377bc0 to your computer and use it in GitHub Desktop.
Azerbaijan Car Plates Validation Rule for Laravel
<?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