olssonm/swedish-entity
Composer 安装命令:
composer require olssonm/swedish-entity
包简介
Validator and formatter for Swedish personnummer and organisationsnummer. With Laravel validators.
关键字:
README 文档
README
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
composer require olssonm/swedish-entity
Usage
Validation
<?php use Olssonm\SwedishEntity\Person; (new Person('600411-8177'))->valid() // true
<?php use Olssonm\SwedishEntity\Organization; (new Organization('556016-0680'))->valid() // true
Automatically detect the entity type:
⚠️ If the
detect-method fails, anOlssonm\SwedishEntity\Exceptions\DetectExceptionwill be thrown.
<?php use Olssonm\SwedishEntity\Entity; $entity = Entity::detect('600411-8177'); var_dump(get_class($entity)) // Olssonm\SwedishEntity\Person
Formatting
⚠️ Formatting an invalid entity will result in an exception. You should make sure to validate it beforehand.
Person
<?php use Olssonm\SwedishEntity\Person; (new Person('071012-9735'))->format($characters = 12, $separator = true) // 20071012-9735 (new Person('071012+9735'))->format($characters = 12, $separator = true) // 19071012+9735 (new Person('200710129735'))->format() // 071012-9735
Organization
<?php use Olssonm\SwedishEntity\Organization; (new Organization('5560160680'))->format($separator = true) // 556016-0680 (new Organization('556016-0680'))->format($separator = false) // 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.
<?php $this->validate($request, [ 'number' => 'required|entity:organization' ]);
You may also omit the parameter and the validator will fallback to any
<?php $this->validate($request, [ 'number' => 'required|entity' ]);
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
<?php use Illuminate\Support\Facades\Validator; $validator = Validator::make($request->all(), [ 'number' => 'required|entity:person' ], [ 'number.entity' => "Invalid personnummer!" ]);
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:
'organization_number' => [ 'required_with:company_name', 'entity:organization', ],
or
'organization_number' => [ 'required', 'entity', ],
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
<?php use Olssonm\SwedishEntity\Person; $person = new Person('600411-8177'); $person->gender; // 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
<?php use Olssonm\SwedishEntity\Organization; $organization = new Organization('212000-1355'); $organization->type; // 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:
<?php use Olssonm\SwedishEntity\Entity; $number = Entity::clean(' 212000-1355a'); // '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
PersonandOrganizationwill format with 10 digits (and a separator) by default viaformat(). - Use the
detect-method to automatically validate both types
If you need to after the validation check type;
<?php use Olssonm\SwedishEntity\Entity; use Olssonm\SwedishEntity\Person; use Olssonm\SwedishEntity\Organization; use Olssonm\SwedishEntity\Exceptions\DetectException try { $entity = Entity::detect('600411-8177'); } catch (DetectException $e) { // Handle exception } // PHP < 8 if(get_class($entity) == Person::class) { // Do stuff for person } elseif(get_class($entity) == Organization::class) { // Do stuff for organization } // PHP 8 if($entity::class == Person::class) { // Do stuff for person } elseif($entity::class == Organization::class) { // Do stuff for organization }
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.
olssonm/swedish-entity 适用场景与选型建议
olssonm/swedish-entity 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 208.71k 次下载、GitHub Stars 达 15, 最近一次更新时间为 2020 年 12 月 08 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「package」 「laravel」 「Swedish」 「Personnummer」 「Organisationsnummer」 「Social security number」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 olssonm/swedish-entity 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 olssonm/swedish-entity 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 olssonm/swedish-entity 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Validate Swedish social security numbers
Validate Swedish Organisationsnummer
A Symfony2 Bundle to slugify swedish texts
Validate personal identity numbers
Swedish language extension for flarum.
Rounding prices in Magento 2
统计信息
- 总下载量: 208.71k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 15
- 点击次数: 43
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-12-08