Repo rebuild
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Countries\Rules;
|
||||
|
||||
use Closure;
|
||||
use Illuminate\Contracts\Validation\ValidationRule;
|
||||
use Modules\Countries\Models\CountrySubdivision;
|
||||
|
||||
class ValidIso3166SubdivisionCode implements ValidationRule
|
||||
{
|
||||
public function __construct(private readonly bool $activeOnly = true) {}
|
||||
|
||||
public function validate(string $attribute, mixed $value, Closure $fail): void
|
||||
{
|
||||
if (! is_string($value) || ! preg_match('/^[A-Z]{2}-[A-Z0-9]{1,6}$/', strtoupper($value))) {
|
||||
$fail('The :attribute must be a valid ISO 3166-2 subdivision code.');
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$query = CountrySubdivision::query()->where('code', strtoupper($value));
|
||||
|
||||
if ($this->activeOnly) {
|
||||
$query->where('is_active', true);
|
||||
}
|
||||
|
||||
if (! $query->exists()) {
|
||||
$fail('The :attribute must be a supported ISO 3166-2 subdivision code.');
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user