rooberthh/identity

A php package to identify swedish personal numbers or organization numbers from a string.

Installs: 0

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 0

Forks: 0

pkg:composer/rooberthh/identity

dev-main 2026-01-10 15:16 UTC

This package is auto-updated.

Last update: 2026-01-11 06:18:52 UTC


README

A PHP package to validate and identify Swedish personal numbers (personnummer) and organization numbers (organisationsnummer).

Usage

Auto-detect identity type

use Rooberthh\Identity\Identity;

$identity = Identity::identify('770604-0016');
// Returns PersonalNumber instance

$identity = Identity::identify('556074-7569');
// Returns OrganizationNumber instance

Safe identification (no exceptions)

$identity = Identity::tryIdentify('invalid-number');
// Returns null instead of throwing exception

if ($identity = Identity::tryIdentify($input)) {
    // Valid identity
}

Personal number validation

use Rooberthh\Identity\PersonalNumber;

// Validate without creating object
if (PersonalNumber::isValid('770604-0016')) {
    // Valid personal number
}

// Create instance (throws exception if invalid)
$person = new PersonalNumber('770604-0016');

Personal number formatting

$person = new PersonalNumber('7706040016');

$person->shortFormat();        // "7706040016"
$person->shortFormat(true);    // "770604-0016"
$person->longFormat();         // "197706040016"
$person->longFormat(true);     // "19770604-0016"

Extract metadata from personal number

$person = new PersonalNumber('770604-0016');

$person->getBirthDate();       // DateTimeImmutable: 1977-06-04
$person->getAge();             // int (calculated from today)
$person->getGender();          // Gender::Male
$person->isMale();             // true
$person->isFemale();           // false
$person->isOfAge(18);          // true

Centenarians (100+ years old)

// The + separator indicates a person born 100+ years ago
$person = new PersonalNumber('770604+0016');

$person->isCentenarian();      // true
$person->getBirthDate();       // DateTimeImmutable: 1877-06-04
$person->shortFormat(true);    // "770604+0016"

Coordination numbers (samordningsnummer)

// Coordination numbers have day + 60
$person = new PersonalNumber('770664-0005');

$person->isCoordinationNumber(); // true
$person->getBirthDate();         // DateTimeImmutable: 1977-06-04

Organization number validation

use Rooberthh\Identity\OrganizationNumber;

if (OrganizationNumber::isValid('556074-7569')) {
    // Valid organization number
}

$org = new OrganizationNumber('5560747569');
$org->shortFormat(true);       // "556074-7569"

Error handling

use Rooberthh\Identity\IdentityException;

try {
    $person = new PersonalNumber('invalid');
} catch (IdentityException $e) {
    // "'invalid' is not valid personal number."
}