项目作者: olssonm

项目描述 :
Validate, format and extract data for Swedish personnummer (social security numbers) and organisationsnummer (organisational numbers)
高级语言: PHP
项目地址: git://github.com/olssonm/swedish-entity.git
创建时间: 2020-12-07T13:51:37Z
项目社区:https://github.com/olssonm/swedish-entity

开源协议:MIT License

下载


Swedish Entity

Latest Version on Packagist
PHP version
Build Status
Software License

Validate, format and extract data for Swedish personnummer (social security numbers) and organisationsnummer (organizational numbers).

This package also handles the temporary personal identity number known as “Samordningsnummer” (a.k.a. coordination number).

Also includes validators for Laravel.

The benefits of this package – while not always strictly according to the standard – is the ability to format using both short/long (10 or 12 characters) without or with a separator (i.e. 11/13 characters).

Note that companies always consists of 10/11 characters (with or without an optional separator).

This package use the excellent personnummer/php-package as it’s basis for the social security-handling, but with some additional attributes and methods.

Installation

  1. composer require olssonm/swedish-entity

Usage

Validation

  1. <?php
  2. use Olssonm\SwedishEntity\Person;
  3. (new Person('600411-8177'))->valid()
  4. // true
  1. <?php
  2. use Olssonm\SwedishEntity\Organization;
  3. (new Organization('556016-0680'))->valid()
  4. // true

Automatically detect the entity type:

⚠️ If the detect-method fails, an Olssonm\SwedishEntity\Exceptions\DetectException will be thrown.

  1. <?php
  2. use Olssonm\SwedishEntity\Entity;
  3. $entity = Entity::detect('600411-8177');
  4. var_dump(get_class($entity))
  5. // Olssonm\SwedishEntity\Person

Formatting

⚠️ Formatting an invalid entity will result in an exception. You should make sure to validate it beforehand.

Person

  1. <?php
  2. use Olssonm\SwedishEntity\Person;
  3. (new Person('071012-9735'))->format($characters = 12, $separator = true)
  4. // 20071012-9735
  5. (new Person('071012+9735'))->format($characters = 12, $separator = true)
  6. // 19071012+9735
  7. (new Person('200710129735'))->format()
  8. // 071012-9735

Organization

  1. <?php
  2. use Olssonm\SwedishEntity\Organization;
  3. (new Organization('5560160680'))->format($separator = true)
  4. // 556016-0680
  5. (new Organization('556016-0680'))->format($separator = false)
  6. // 5560160680

Laravel validators

The package registers the “entity” rule, which accepts the parameters any, organization, person or for special cases person_only_coordination and person_only_ssn.

  1. <?php
  2. $this->validate($request, [
  3. 'number' => 'required|entity:organization'
  4. ]);

You may also omit the parameter and the validator will fallback to any

  1. <?php
  2. $this->validate($request, [
  3. 'number' => 'required|entity'
  4. ]);

Use person_only_coordination and person_only_ssn if you for any reason only want to allow coordination numbers (samordningsnummer) and/or only SSN/personnummer.

I.e. the coordination number 850361-8335 will validate with the rule person_only_coordination, while it will fail with person_only_ssn.

This is useful when validation against for instance BankID, as they do not allow coordination numbers at the moment.

Custom messages

  1. <?php
  2. use Illuminate\Support\Facades\Validator;
  3. $validator = Validator::make($request->all(), [
  4. 'number' => 'required|entity:person'
  5. ], [
  6. 'number.entity' => "Invalid personnummer!"
  7. ]);

Implicit validation

For the validator to run when the social security/organizational number is missing or an empty string (note, does not apply to null) you will need to implicitly state so with a required rule, i.e:

  1. 'organization_number' => [
  2. 'required_with:company_name',
  3. 'entity:organization',
  4. ],

or

  1. 'organization_number' => [
  2. 'required',
  3. 'entity',
  4. ],

Attributes

Person

Attribute Comment type
ssn The SSN of the entity string
century Birthyear century string
year Birthyear string
month Birthmonth string
day Birthday string
num The “last four digits” string
check The checksum verifier string
age Age string
birthday Entitys birthday DateTime
gender Gender (Male/Female) string
type Type of ssn * string

Either “Samordningsnummer” or “Personnummer”*

Example

  1. <?php
  2. use Olssonm\SwedishEntity\Person;
  3. $person = new Person('600411-8177');
  4. $person->gender;
  5. // Male

Organization

Attribute Comment type
org_no The org. no. of the entity string
check The checksum verifier string
type Type of organisation* string

One of the following: “Dödsbon”, “Stat, landsting och kommuner”, “Aktiebolag”, “Enkelt bolag”, “Ekonomiska föreningar”, “Ideella föreningar och stiftelser” and “Handelsbolag, kommanditbolag och enkla bolag”. Note: Organizations starting with 0, 3 and 4, while technically a valid number – it is uncertain if they exist, and will return and empty string.*

Example

  1. <?php
  2. use Olssonm\SwedishEntity\Organization;
  3. $organization = new Organization('212000-1355');
  4. $organization->type;
  5. // Stat, landsting och kommuner

Clean-helper

The Entity-class contains a clean-helper that can be useful for removing illegal characters from a social security- or organisational number:

  1. <?php
  2. use Olssonm\SwedishEntity\Entity;
  3. $number = Entity::clean(' 212000-1355a');
  4. // '212000-1355'

Note that this is not automatically applied, so you will need to clean the string before validation.

Gotcha moments

Enskild firma

EF (Enskild firma) – while technically a company/organization, uses the proprietors personnummer. Therefore that number will not validate as company/organization. Instead of using a custom solution for this (as Creditsafe, Bisnode and others do – by adding additional numbers/characters to the organizational number/social security number), a way to handle this would be:

  • Work with 10 digits when expecting both people and companies (preferably with a separator). Hint: both Person and Organization will format with 10 digits (and a separator) by default via format().
  • Use the detect-method to automatically validate both types

If you need to after the validation check type;

  1. <?php
  2. use Olssonm\SwedishEntity\Entity;
  3. use Olssonm\SwedishEntity\Person;
  4. use Olssonm\SwedishEntity\Organization;
  5. use Olssonm\SwedishEntity\Exceptions\DetectException
  6. try {
  7. $entity = Entity::detect('600411-8177');
  8. } catch (DetectException $e) {
  9. // Handle exception
  10. }
  11. // PHP < 8
  12. if(get_class($entity) == Person::class) {
  13. // Do stuff for person
  14. } elseif(get_class($entity) == Organization::class) {
  15. // Do stuff for organization
  16. }
  17. // PHP 8
  18. if($entity::class == Person::class) {
  19. // Do stuff for person
  20. } elseif($entity::class == Organization::class) {
  21. // Do stuff for organization
  22. }

Other/misc.

You can use personnummer.se while working with this package and building your app to generate random Swedish personnummer/social security numbers.

License

The MIT License (MIT). Please see the License File for more information.

© 2022-2025 Marcus Olsson.