olssonm/identity-number
最新稳定版本:v7.0
Composer 安装命令:
composer require olssonm/identity-number
包简介
Laravel validator for Swedish personal identity numbers / social security numbers (personnummer)
关键字:
README 文档
README
⚠️ Abandoned This package has been abandoned in favor of olssonm/swedish-entity.
While the package will be usable for the forseeable future, it will not be updated. Please migrate to olssonm/swedish-entity when possible.
Validator for Swedish "personnummer" (a.k.a. personal identity number, social security number or simply "PIN").
This validator also handles Swedish organization numbers and the temporary personal identity number known as "Samordningsnummer" (a.k.a. coordination number).
For use either as a standalone package, or with Laravel.
The package does not only apply the Luhn-algorithm for the last four digits, but also checks that the date of birth is a valid date.
Of course you can throw pretty much any format you wish at the validator, ie. 10-digit variant (7712112775) or the 12-digit variant (197712112775) and with or without a hyphen (771211-2775, 19771211-2775).
Install
Via Composer
$ composer require olssonm/identity-number
Within Laravel
This package uses Package Auto-Discovery for loading the service provider. Once installed you should see the message
Discovered Package: olssonm/identity-number
Else, per standard Laravel-procedure, just register the package in your providers array:
'providers' => [ Olssonm\IdentityNumber\IdentityNumberServiceProvider::class, ]
Usage
Standalone
The package is usable straight out of the box once installed with composer:
use Olssonm\IdentityNumber\Pin;
Personnummer ("personal identity number")
Pin::isValid('19771211-2775'); // Defaults to identity number // true Pin::isValid('19771211-2775', 'identity'); // Identity validation specified // true
Organisationsnummer ("organization number")
Pin::isValid('556016-0681', 'organization') // true
Samordningsnummer ("coordination number")
Pin::isValid('19671180-2850', 'coordination'); // true
The coordination-number validator handles the same way as the personal identity-validator but does not run a check/validation on the date of birth.
The IdentityNumberFormatter-class
The IdentityNumberFormatter-class allows you to format a PIN/identity for your custom needs. The class is also used internally for validation, but if you want to make sure you save the same value in the database as you pass for validation – you should also apply the formatting before validating.
use Olssonm\IdentityNumber\IdentityNumberFormatter; // Format to a 10-character PIN without a seperator $formatter = new IdentityNumberFormatter('19860210-7313', 10, false); // Get the formatted output $formatter->getFormatted(); // 8602107313 // You can also clean the number from all unwanted characters (new IdentityNumberFormatter('a19860210 - 7313', 12, true))->clean()->getFormatted(); // 19860210-7313
Laravel validators
The package extends the Illuminate\Validator via a service provider, so all you have to do is use the identity_number-, coordination_number- and organization_number-rules, just as you would with any other rule.
// Personal identity numbers public function store(Request $request) { $this->validate($request, [ 'number' => 'required|identity_number' ]); } // Coordination numbers public function store(Request $request) { $this->validate($request, [ 'number' => 'required|coordination_number' ]); } // Organization numbers public function store(Request $request) { $this->validate($request, [ 'number' => 'required|organization_number' ]); }
And of course, you can roll your own error messages:
$validator = Validator::make($request->all(), [ 'number' => 'required|identity_number' ], [ 'number.identity_number' => "Hey! That's not a personnummer!" ]); if($validator->fails()) { return $this->returnWithErrorAndInput($validator); }
If you're using the validation throughout your application, you also might want to put the error message in your lang-files.
Testing
$ composer test
or
$ phpunit
License
The MIT License (MIT). Please see License File for more information.
© 2020 Marcus Olsson.
olssonm/identity-number 适用场景与选型建议
olssonm/identity-number 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 139.33k 次下载、GitHub Stars 达 17, 最近一次更新时间为 2026 年 01 月 04 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「package」 「laravel」 「Swedish」 「Personnummer」 「Social security number」 「olssonm」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 olssonm/identity-number 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 olssonm/identity-number 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 olssonm/identity-number 相关的其它包
同方向 / 同关键字的高下载量 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
统计信息
- 总下载量: 139.33k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 17
- 点击次数: 30
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-01-04