byrokrat / id
Data types for swedish personal identity and corporation id numbers
Installs: 87 788
Dependents: 5
Suggesters: 0
Security: 0
Stars: 14
Watchers: 4
Forks: 3
Open Issues: 0
Requires
- php: >=7.3
Requires (Dev)
README
Personal Identity
Data types for swedish personal identity and corporation id numbers.
Installation
composer require byrokrat/id
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
IdInterface
The base interface. Look here for a complete API reference.PersonalId
The identification number of a swedish individual (personnummer).CoordinationId
Identifier for non-citizens (samordningsnummer).FakeId
Replacement usable when a person must have an id, but is not registered with the swedish authorities.
OrganizationId
Identifies a swedish company or organization (organisationsnummer).NullId
Null object implementation.
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
CoordinationIdFactory
FakeIdFactory
OrganizationIdFactory
NullIdFactory
FailingIdFactory
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
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
Characters that are not formatting tokens are returned as they are by the formatter.
Definitions
Swedish sources on the construction and usage of id numbers:
Symfony Bundle
To use as validation rules in your Symfony project see the third party package IdentityNumberValidatorBundle.