Installation
composer require avkluchko/government-bundle
Ensure your Laravel project meets the PHP 7.4+ requirement (x64 recommended for checksum validation).
First Use Case Validate a Russian government identifier (e.g., OGRN, INN, or SNILS) in a controller or form request:
use AVKluchko\GovernmentBundle\Validator\OGRNValidator;
public function validateOGRN(OGRNValidator $validator, Request $request) {
$ogrn = $request->input('ogrn');
if (!$validator->isValid($ogrn)) {
return back()->withErrors(['ogrn' => 'Invalid OGRN']);
}
// Proceed with valid OGRN
}
Where to Look First
Validator/OGRNValidator.php, INNValidator.php, and SNILSValidator.php for core logic.tests/ for edge cases (e.g., leading zeros, checksum validation).GovernmentBundle.php for service registration.Form Validation Integrate validators into Laravel’s built-in validation:
use AVKluchko\GovernmentBundle\Validator\INNValidator;
public function rules() {
return [
'inn' => ['required', function ($attribute, $value, $fail) {
$validator = app(INNValidator::class);
if (!$validator->isValid($value)) {
$fail('The '.$attribute.' must be a valid INN.');
}
}]
];
}
Service Container Binding Bind validators to Laravel’s container for reusable access:
$this->app->bind(INNValidator::class, function ($app) {
return new INNValidator();
});
API Request Validation Use in API middleware or DTOs:
public function handle(Request $request, Closure $next) {
$snilsValidator = app(SNILSValidator::class);
if (!$snilsValidator->isValid($request->snils)) {
return response()->json(['error' => 'Invalid SNILS'], 400);
}
return $next($request);
}
Database Constraints
Combine with Laravel’s database validation (e.g., unique rules) for backend checks:
$validator = Validator::make($data, [
'ogrn' => ['required', 'string', function ($attribute, $value, $fail) {
if (!app(OGRNValidator::class)->isValid($value)) {
$fail('Invalid OGRN format or checksum.');
}
}]
]);
Custom Rules
Extend Laravel’s FormRequest for reusable validation:
use AVKluchko\GovernmentBundle\Validator\INNValidator;
public function rules() {
return [
'inn' => ['required', new ValidINN($this->app->make(INNValidator::class))]
];
}
class ValidINN implements Rule {
protected $validator;
public function __construct(INNValidator $validator) {
$this->validator = $validator;
}
public function passes($attribute, $value) {
return $this->validator->isValid($value);
}
}
Checksum Limitations
Input Sanitization
$cleanInput = preg_replace('/[^0-9]/', '', $input);
$validator->isValid($cleanInput);
Version-Specific Behavior
123456789 → 0123456789). Ensure tests account for this.Symfony Dependency
AppKernel).Validator Logic
isValid() methods in Validator/ for checksum algorithms. For OGRN, the checksum uses modulo-11 arithmetic.$sum = 0;
for ($i = 0; $i < 12; $i++) {
$sum += $ogrn[$i] * (13 - $i);
}
$checksum = 11 - ($sum % 11);
Edge Cases
123-456-789 0X). Test with hyphens/spaces removed.strlen() checks if strict length is needed.Performance
Custom Validators
Extend AbstractValidator (if exposed) or create a wrapper:
class CustomOGRNValidator extends OGRNValidator {
public function isValid($ogrn, bool $strict = true) {
// Add custom logic (e.g., blacklist checks)
return parent::isValid($ogrn) && !$this->isBlacklisted($ogrn);
}
}
Localization
resources/lang/:
'validation' => [
'attributes' => [
'ogrn' => 'ОГРН',
],
'custom' => [
'ogrn' => [
'invalid' => 'Некорректный ОГРН.',
],
],
],
Testing
$validator = $this->createMock(INNValidator::class);
$validator->method('isValid')->willReturn(false);
$this->app->instance(INNValidator::class, $validator);
Configuration
AppServiceProvider:
public function register() {
$this->app->singleton(INNValidator::class, function () {
return new INNValidator(['custom_rule' => true]);
});
}
How can I help you explore Laravel packages today?