PHPackages                             byrokrat/id - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. byrokrat/id

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

byrokrat/id
===========

Data types for swedish personal identity and corporation id numbers

2.1.1(5y ago)1597.7k↓36.8%35UnlicensePHPPHP &gt;=7.3

Since Jan 27Pushed 5y ago3 watchersCompare

[ Source](https://github.com/byrokrat/id)[ Packagist](https://packagist.org/packages/byrokrat/id)[ Docs](https://github.com/byrokrat/id)[ RSS](/packages/byrokrat-id/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (2)Versions (8)Used By (5)

[![byrokrat](res/logo.svg)](res/logo.svg)

Personal Identity
=================

[](#personal-identity)

[![Packagist Version](https://camo.githubusercontent.com/7703185ab6ab4be49519a68b794511ec0f67e18e4ee51a42904ae659e30e4ea2/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6279726f6b7261742f69642e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/byrokrat/id)[![Build Status](https://camo.githubusercontent.com/db8e26eab8683e9a663a63d78ac725f18fbe7fc4ad7b921b5b207bb2e9aa042a/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f6279726f6b7261742f69642f6d61737465722e7376673f7374796c653d666c61742d737175617265)](https://travis-ci.com/github/byrokrat/id)[![Quality Score](https://camo.githubusercontent.com/9a0766e59852866e877300a9b410b55d55a8fefe0dec6fdc6c503744f5d26619/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f672f6279726f6b7261742f69642e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/byrokrat/id)

Data types for swedish personal identity and corporation id numbers.

Installation
------------

[](#installation)

```
composer require byrokrat/id
```

Usage
-----

[](#usage)

```
use byrokrat\id\PersonalId;
use byrokrat\id\IdInterface;

$id = new PersonalId('940323-2383');

// outputs 940323-2383
echo $id;

// outputs 940323-2383
echo $id->format(IdInterface::FORMAT_10_DIGITS);

// outputs 199403232383
echo $id->format(IdInterface::FORMAT_12_DIGITS);

// outputs 940323
echo $id->format('ymd');

// outputs something like 25
echo $id->getAge();

// outputs 1 (true)
echo $id->isFemale();
```

```
use byrokrat\id\OrganizationId;
use byrokrat\id\IdInterface;

$id = new OrganizationId('835000-0892');

// outputs 835000-0892
echo $id->format(IdInterface::FORMAT_10_DIGITS);

// outputs 008350000892
echo $id->format(IdInterface::FORMAT_12_DIGITS);

// outputs 1 (true)
echo $id->isSexUndefined();

// outputs 1 (true)
echo $id->isNonProfit();
```

### Class hierarchy

[](#class-hierarchy)

- [`IdInterface`](src/IdInterface.php) The base interface. Look here for a complete API reference.
    - [`PersonalId`](src/PersonalId.php) The identification number of a swedish individual ([personnummer](http://sv.wikipedia.org/wiki/Personnummer_i_Sverige)).
        - [`CoordinationId`](src/CoordinationId.php) Identifier for non-citizens ([samordningsnummer](http://sv.wikipedia.org/wiki/Samordningsnummer#Sverige)).
        - [`FakeId`](src/FakeId.php) Replacement usable when a person must have an id, but is not registered with the swedish authorities.
    - [`OrganizationId`](src/OrganizationId.php) Identifies a swedish company or organization ([organisationsnummer](http://sv.wikipedia.org/wiki/Organisationsnummer)).
    - [`NullId`](src/NullId.php) Null object implementation.

### Creating ID objects

[](#creating-id-objects)

Creating ID objects can be complicated.

- A personal id can be a coordination id, if the individual identified is not a swedish citizen.
- A corporation id can be a personal id if the corporation is registered with a single individual (egenföretagare).
- A single individual company can use a coordination id if the individual is not a swedish citizen.
- At times you may wish to process persons without a valid swedish personal id, using the `FakeId` implementation.

To solve these difficulties a decoratable set of factories is included. Create a factory with the abilities you need by chaining factory objects at creation time.

```
use byrokrat\id\PersonalIdFactory;
use byrokrat\id\CoordinationIdFactory;

$factory = new PersonalIdFactory(new CoordinationIdFactory);

$id = $factory->createId('940323-2383');
```

In this example the factory will first try to create a `PersonalId`, if this fails it will try to create a `CoordinationId`, if this fails it will throw an Exception.

The following factories are included:

- [`PersonalIdFactory`](src/PersonalIdFactory.php)
- [`CoordinationIdFactory`](src/CoordinationIdFactory.php)
- [`FakeIdFactory`](src/FakeIdFactory.php)
- [`OrganizationIdFactory`](src/OrganizationIdFactory.php)
- [`NullIdFactory`](src/NullIdFactory.php)
- [`FailingIdFactory`](src/FailingIdFactory.php)

### Controlling the delimiter and century of ids containing dates

[](#controlling-the-delimiter-and-century-of-ids-containing-dates)

In order to controll the computation of dates you may specify at what time parsing takes place by passing a datetime object.

```
use byrokrat\id\PersonalIdFactory;

$factory = new PersonalIdFactory;

// Year interpreted as 2010 as parsing is done 2020
$young = $factory->createId('1001012382', new \DateTime('20200101'));

// Year interpreted as 1910 as parsing is done 1990
$older = $factory->createId('1001012382', new \DateTime('19900101'));

// outputs 2010
echo $young->format('Y');

// outputs 1910
echo $older->format('Y');
```

Specifying parse date also affects what delimiter is used.

```
use byrokrat\id\PersonalIdFactory;

$factory = new PersonalIdFactory;

// Delimiter is '+' as parsing is done in 2050
$id = $factory->createId('194001079120', new \DateTime('20500101'));

// outputs 400107+9120
echo $id;
```

### Formatting

[](#formatting)

Ids can be printed in custom formats using the `format()` method, where `$formatStr`is a mix of format tokens and non-formatting characters (for a list of formatting tokens se below).

```
echo $id->format($formatStr);
```

If you need to format a large number of ids a formatter object can be created.

```
use byrokrat\id\Formatter\Formatter;
use byrokrat\id\PersonalId;

$formatter = new Formatter('y');

// outputs 82
echo $formatter->format(new PersonalId('940323-2383'));
```

#### Formatting tokens

[](#formatting-tokens)

Characters that are not formatting tokens are returned as they are by the formatter.

TokenDescription`C`Century, 2 digits (00 if not applicable)`S`Part of serial number before delimiter, 6 digits`-`Date and control string delimiter (- or +)`s`Part of serial number after delimiter, 3 digits`k`Check digit`X`Sex, F, M or O (empty if not applicable)`L`Legal form (empty if not applicable)`B`Birth county (empty if not applicable)`\`Escape the following character**The following tokens are only valid for ids containing a date**`A`Current age*Year*`Y`A full numeric representation of a year, 4 digits`y`A two digit representation of a year*Month*`m`Numeric representation of a month, with leading zeros, 2 digits`n`Numeric representation of a month, without leading zeros, 1 through 12`F`A full textual representation of a month, such as January or March`M`A short textual representation of a month, three letters, Jan through Dec`t`Number of days in the given month 28 through 31*Week*`W`ISO-8601 week number of year, weeks starting on Monday*Day*`d`Day of the month, 2 digits with leading zeros`j`Day of the month without leading zeros, 1 to 31`l`(lowercase 'L') A full textual representation of the day of the week`D`A textual representation of a day, three letters Mon through Sun`w`Numeric representation of the day of the week 0 (for Sunday) through 6`N`ISO-8601 numeric representation of the day of the week 1 (for Monday) through 7`z`The day of the year (starting from 0), 0 through 365Definitions
-----------

[](#definitions)

Swedish sources on the construction and usage of id numbers:

- [Folkbokföringslagen](https://www.riksdagen.se/sv/dokument-lagar/dokument/svensk-forfattningssamling/folkbokforingslag-1991481_sfs-1991-481#P18)
- [Statistiska centralbyrån on personal identity numbers](https://www.scb.se/contentassets/8d9d985ca9c84c6e8d879cc89a8ae479/ov9999_2016a01_br_be96br1601.pdf)

Symfony Bundle
--------------

[](#symfony-bundle)

To use as validation rules in your Symfony project see the third party package [IdentityNumberValidatorBundle](https://github.com/jongotlin/IdentityNumberValidatorBundle).

###  Health Score

38

—

LowBetter than 85% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity39

Limited adoption so far

Community19

Small or concentrated contributor base

Maturity63

Established project with proven stability

 Bus Factor1

Top contributor holds 93.6% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~357 days

Recently: every ~393 days

Total

7

Last Release

1988d ago

Major Versions

1.1.0 → 2.0.02018-07-20

PHP version history (4 changes)1.0.0PHP &gt;=5.4

1.1.0PHP &gt;=5.5

2.0.0PHP &gt;=7.1

2.1.1PHP &gt;=7.3

### Community

Maintainers

![](https://www.gravatar.com/avatar/b0af6567c7f4f5518a422192675445464fdcf18acbc9459b3205da9fd446f8d0?d=identicon)[hanneskod](/maintainers/hanneskod)

---

Top Contributors

[![hanneskod](https://avatars.githubusercontent.com/u/1369274?v=4)](https://github.com/hanneskod "hanneskod (102 commits)")[![jongotlin](https://avatars.githubusercontent.com/u/165154?v=4)](https://github.com/jongotlin "jongotlin (7 commits)")

---

Tags

organisationsnummerpersonal-identity-numberpersonnummeridPersonnummerPersonal idpersonal identity numberOrganisationsnummerCorporation id

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/byrokrat-id/health.svg)

```
[![Health](https://phpackages.com/badges/byrokrat-id/health.svg)](https://phpackages.com/packages/byrokrat-id)
```

###  Alternatives

[personnummer/personnummer

Validate Swedish social security numbers

34739.3k3](/packages/personnummer-personnummer)[cybercog/laravel-optimus

An Optimus bridge for Laravel. Id obfuscation based on Knuth's multiplicative hashing method.

192564.1k](/packages/cybercog-laravel-optimus)[pugx/shortid-php

An implementation of shortid in PHP

52588.5k3](/packages/pugx-shortid-php)[olssonm/swedish-entity

Validator and formatter for Swedish personnummer and organisationsnummer. With Laravel validators.

15196.1k](/packages/olssonm-swedish-entity)[alicfeng/identity-card

IdentityCard Library

1228.5k](/packages/alicfeng-identity-card)[identifier/identifier

Common Interfaces and Factories for Identifiers

3226.2k1](/packages/identifier-identifier)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
