Skip to content
This repository has been archived by the owner on Dec 9, 2020. It is now read-only.
/ identity-number Public archive

Validator for Swedish personal identity numbers (personnummer). For use "standalone" or with Laravel.

License

Notifications You must be signed in to change notification settings

olssonm/identity-number

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

61 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Swedish "personnummer" validator for Laravel

Latest Version on Packagist Software License Build Status Scrutinizer Score


⚠️ 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.

About

Validator for Swedish personal identity numbers (personnummer). For use "standalone" or with Laravel.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages