olssonm / identity-number
Laravel validator for Swedish personal identity numbers / social security numbers (personnummer)
Installs: 114 692
Dependents: 1
Suggesters: 0
Security: 0
Stars: 17
Watchers: 2
Forks: 2
Open Issues: 0
Requires
- php: ^7.2
- illuminate/support: >=6.0
Requires (Dev)
- orchestra/testbench: >=4.0
README
⚠️ Abandoned This package has been abandoned in favor of olssonm/swedish-entity.
While the package will be usable for the forseeable future, it will not be updated. Please migrate to olssonm/swedish-entity
when possible.
Validator for Swedish "personnummer" (a.k.a. personal identity number, social security number or simply "PIN").
This validator also handles Swedish organization numbers and the temporary personal identity number known as "Samordningsnummer" (a.k.a. coordination number).
For use either as a standalone package, or with Laravel.
The package does not only apply the Luhn-algorithm for the last four digits, but also checks that the date of birth is a valid date.
Of course you can throw pretty much any format you wish at the validator, ie. 10-digit variant (7712112775
) or the 12-digit variant (197712112775
) and with or without a hyphen (771211-2775
, 19771211-2775
).
Install
Via Composer
$ composer require olssonm/identity-number
Within Laravel
This package uses Package Auto-Discovery for loading the service provider. Once installed you should see the message
Discovered Package: olssonm/identity-number
Else, per standard Laravel-procedure, just register the package in your providers array:
'providers' => [ Olssonm\IdentityNumber\IdentityNumberServiceProvider::class, ]
Usage
Standalone
The package is usable straight out of the box once installed with composer:
use Olssonm\IdentityNumber\Pin;
Personnummer ("personal identity number")
Pin::isValid('19771211-2775'); // Defaults to identity number // true Pin::isValid('19771211-2775', 'identity'); // Identity validation specified // true
Organisationsnummer ("organization number")
Pin::isValid('556016-0681', 'organization') // true
Samordningsnummer ("coordination number")
Pin::isValid('19671180-2850', 'coordination'); // true
The coordination-number validator handles the same way as the personal identity-validator but does not run a check/validation on the date of birth.
The IdentityNumberFormatter-class
The IdentityNumberFormatter-class allows you to format a PIN/identity for your custom needs. The class is also used internally for validation, but if you want to make sure you save the same value in the database as you pass for validation – you should also apply the formatting before validating.
use Olssonm\IdentityNumber\IdentityNumberFormatter; // Format to a 10-character PIN without a seperator $formatter = new IdentityNumberFormatter('19860210-7313', 10, false); // Get the formatted output $formatter->getFormatted(); // 8602107313 // You can also clean the number from all unwanted characters (new IdentityNumberFormatter('a19860210 - 7313', 12, true))->clean()->getFormatted(); // 19860210-7313
Laravel validators
The package extends the Illuminate\Validator
via a service provider, so all you have to do is use the identity_number
-, coordination_number
- and organization_number
-rules, just as you would with any other rule.
// Personal identity numbers public function store(Request $request) { $this->validate($request, [ 'number' => 'required|identity_number' ]); } // Coordination numbers public function store(Request $request) { $this->validate($request, [ 'number' => 'required|coordination_number' ]); } // Organization numbers public function store(Request $request) { $this->validate($request, [ 'number' => 'required|organization_number' ]); }
And of course, you can roll your own error messages:
$validator = Validator::make($request->all(), [ 'number' => 'required|identity_number' ], [ 'number.identity_number' => "Hey! That's not a personnummer!" ]); if($validator->fails()) { return $this->returnWithErrorAndInput($validator); }
If you're using the validation throughout your application, you also might want to put the error message in your lang-files.
Testing
$ composer test
or
$ phpunit
License
The MIT License (MIT). Please see License File for more information.
© 2020 Marcus Olsson.