Skip to content
This repository was archived by the owner on Dec 9, 2020. It is now read-only.
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: olssonm/identity-number
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v6.2
Choose a base ref
...
head repository: olssonm/identity-number
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: master
Choose a head ref
  • 5 commits
  • 12 files changed
  • 2 contributors

Commits on Mar 9, 2020

  1. Copy the full SHA
    c78e2a5 View commit details

Commits on Jun 1, 2020

  1. Dev (#23)

    * Improved/fixed formatting, indentation and more.
    
    * Edits to Travis-syntax.
    olssonm authored Jun 1, 2020
    Copy the full SHA
    797e655 View commit details

Commits on Sep 10, 2020

  1. Laravel 8 (#24)

    * Prepare for Laravel 8
    
    * Cleanup, remove version compatibility chart
    olssonm authored Sep 10, 2020
    Copy the full SHA
    e47c69b View commit details

Commits on Dec 8, 2020

  1. Update README.md

    Add abandoned notice
    olssonm authored Dec 8, 2020
    Copy the full SHA
    aea418b View commit details

Commits on Dec 9, 2020

  1. Copy the full SHA
    21d7922 View commit details
Showing with 216 additions and 165 deletions.
  1. +10 −6 .travis.yml
  2. +1 −1 LICENSE.md
  3. +14 −13 README.md
  4. +2 −3 composer.json
  5. +3 −1 src/CoordinationNumber.php
  6. +3 −1 src/IdentityNumber.php
  7. +20 −11 src/IdentityNumberFormatter.php
  8. +6 −4 src/IdentityNumberServiceProvider.php
  9. +3 −1 src/OrganizationNumber.php
  10. +4 −1 src/Pin.php
  11. +17 −5 src/Validator.php
  12. +133 −118 tests/IdentityNumberTest.php
16 changes: 10 additions & 6 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
language: php

sudo: false
dist: xenial

os: linux

before_script:
- travis_retry composer self-update
@@ -9,12 +11,8 @@ before_script:
script:
- composer test

matrix:
jobs:
include:
- php: 7.2
env: ILLUMINATE_VERSION=5.8.*
- php: 7.2
env: ILLUMINATE_VERSION=6.0
- php: 7.2
env: ILLUMINATE_VERSION=^6.0
- php: 7.2
@@ -23,3 +21,9 @@ matrix:
env: ILLUMINATE_VERSION=^6.0
- php: 7.3
env: ILLUMINATE_VERSION=^7.0
- php: 7.3
env: ILLUMINATE_VERSION=^8.0
- php: 7.4
env: ILLUMINATE_VERSION=^7.0
- php: 7.4
env: ILLUMINATE_VERSION=^8.0
2 changes: 1 addition & 1 deletion LICENSE.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# The MIT License (MIT)

Copyright (c) 2019 Marcus Olsson <contact@marcusolsson.me>
Copyright (c) 2020 Marcus Olsson <contact@marcusolsson.me>

> Permission is hereby granted, free of charge, to any person obtaining a copy
> of this software and associated documentation files (the "Software"), to deal
27 changes: 14 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
@@ -3,6 +3,15 @@
[![Latest Version on Packagist][ico-version]][link-packagist]
[![Software License][ico-license]](LICENSE.md)
[![Build Status][ico-travis]][link-travis]
[![Scrutinizer Score][ico-scrutinizer]][link-scrutinizer]

---

⚠️ **Abandoned** This package has been abandoned in favor of [olssonm/swedish-entity](https://github.com/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").

@@ -14,16 +23,6 @@ The package does not only apply the Luhn-algorithm for the last four digits, but

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`).

## Version Compatibility

Laravel | identity-number
:--------------------------------------|:----------
`5.1.x` / `5.2.x` | `2.x`
<code>>=5.3.x &#124; <=5.7.x</code> | `^5.x`
`>=5.8.x` / `^6.0` | `^6.x` / `^6.1`

*Note: please check the corresponding readme.md for the correct documentation for each version.*

## Install

Via Composer
@@ -32,9 +31,9 @@ Via Composer
$ composer require olssonm/identity-number
```

**Together with Laravel**
**Within Laravel**

Since v5.* (for Laravel.5) this package uses Package Auto-Discovery for loading the service provider. Once installed you should see the message
This package uses Package Auto-Discovery for loading the service provider. Once installed you should see the message

```
Discovered Package: olssonm/identity-number
@@ -161,10 +160,12 @@ $ phpunit

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

© 2019 [Marcus Olsson](https://marcusolsson.me).
© 2020 [Marcus Olsson](https://marcusolsson.me).

[ico-version]: https://img.shields.io/packagist/v/olssonm/identity-number.svg?style=flat-square
[ico-license]: https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square
[ico-travis]: https://img.shields.io/travis/olssonm/identity-number/master.svg?style=flat-square
[link-packagist]: https://packagist.org/packages/olssonm/identity-number
[link-travis]: https://travis-ci.org/olssonm/identity-number
[ico-scrutinizer]: https://img.shields.io/scrutinizer/g/olssonm/identity-number.svg?style=flat-square
[link-scrutinizer]: https://scrutinizer-ci.com/g/olssonm/identity-number
5 changes: 2 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
@@ -21,11 +21,10 @@
],
"require": {
"php" : "^7.2",
"illuminate/support": "^5.8 || ^6.0 || ^7.0"
"illuminate/support": ">=6.0"
},
"require-dev": {
"phpunit/phpunit" : "^7.0 || ^8.0",
"orchestra/testbench": ">=3.8.0"
"orchestra/testbench": ">=4.0"
},
"autoload": {
"psr-4": {
4 changes: 3 additions & 1 deletion src/CoordinationNumber.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php namespace Olssonm\IdentityNumber;
<?php

namespace Olssonm\IdentityNumber;

use DateTime;

4 changes: 3 additions & 1 deletion src/IdentityNumber.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php namespace Olssonm\IdentityNumber;
<?php

namespace Olssonm\IdentityNumber;

use DateTime;

31 changes: 20 additions & 11 deletions src/IdentityNumberFormatter.php
Original file line number Diff line number Diff line change
@@ -1,37 +1,44 @@
<?php namespace Olssonm\IdentityNumber;
<?php

namespace Olssonm\IdentityNumber;

/**
* Handles the identity number formatting
* Handles the identity number formatting
*/
class IdentityNumberFormatter
{

/**
* Set value
*
* @var string
*/
private $value;

/**
* Number of characters
*
* @var integer
*/
private $characters;

/**
* If hyphens is allowed
*
* @var boolean
*/
private $useHyphen;

/**
* What type of hyphen to use ("-"/+)
*
* @var string
*/
private $typeHyphen = '-';

/**
* Format the personal identity number to make validation easier
*
* @param string $value the input value
* @param int $characters number of characters
* @param bool $useHyphen if hyphens should be used
@@ -46,19 +53,21 @@ public function __construct($value, $characters, $useHyphen = false)

/**
* Clean the value-string from unwanted characters
*
* @return \Olssonm\IdentityNumber\IdentityNumberFormatter
*/
public function clean()
{
// Clean string from invalid values
$pattern = '0123456789-+';
$this->value = preg_replace("/[^".preg_quote($pattern, "/")."]/", '', $this->value);
$this->value = preg_replace("/[^" . preg_quote($pattern, "/") . "]/", '', $this->value);

return $this;
}

/**
* Retrieve the formatted personal identity number
*
* @return string the personal identity number
*/
public function getFormatted()
@@ -75,30 +84,30 @@ public function getFormatted()
// Remove hyphen
$value = str_replace(['-', '+'], '', $value);

if(strlen($value) != 12 && strlen($value) != 10) {
if (strlen($value) != 12 && strlen($value) != 10) {
return false;
}

if($characters == 12 && strlen($value) == 10) {
if ($characters == 12 && strlen($value) == 10) {
$value = 19 . $value;
}

if($characters == 10 && strlen($value) == 12) {
if ($characters == 10 && strlen($value) == 12) {
$newNumber = null;
for ($i=0; $i < strlen($value); $i++) {
if($i > 1) {
for ($i = 0; $i < strlen($value); $i++) {
if ($i > 1) {
$newNumber .= $value[$i];
}
}
$value = $newNumber;
}

// Insert hyphen if you need to
if($useHyphen == true) {
if ($useHyphen == true) {
$newNumber = null;
for ($i=0; $i < strlen($value); $i++) {
for ($i = 0; $i < strlen($value); $i++) {
$newNumber .= $value[$i];
if($i == strlen($value) - 5) {
if ($i == strlen($value) - 5) {
$newNumber .= $this->typeHyphen;
}
}
10 changes: 6 additions & 4 deletions src/IdentityNumberServiceProvider.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php namespace Olssonm\IdentityNumber;
<?php

namespace Olssonm\IdentityNumber;

use Illuminate\Support\ServiceProvider;

@@ -14,23 +16,23 @@ public function boot()
/**
* Extend the Laravel Validator with the "identity_number" rule
*/
$this->app['validator']->extend('identity_number', function ($attribute, $value, $parameters)
$this->app['validator']->extend('identity_number', function($attribute, $value, $parameters)
{
return Pin::isValid($value, 'identity');
});

/**
* Extend the Laravel Validator with the "organization_number" rule
*/
$this->app['validator']->extend('organization_number', function ($attribute, $value, $parameters)
$this->app['validator']->extend('organization_number', function($attribute, $value, $parameters)
{
return Pin::isValid($value, 'organization');
});

/**
* Extend the Laravel Validator with the "coordination_number" rule
*/
$this->app['validator']->extend('coordination_number', function ($attribute, $value, $parameters)
$this->app['validator']->extend('coordination_number', function($attribute, $value, $parameters)
{
return Pin::isValid($value, 'coordination');
});
4 changes: 3 additions & 1 deletion src/OrganizationNumber.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php namespace Olssonm\IdentityNumber;
<?php

namespace Olssonm\IdentityNumber;

use DateTime;

5 changes: 4 additions & 1 deletion src/Pin.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php namespace Olssonm\IdentityNumber;
<?php

namespace Olssonm\IdentityNumber;

/**
* Static helper-class for accessing the validation methods statically
@@ -7,6 +9,7 @@ class Pin
{
/**
* Main method used for validation, acts as a static helper
*
* @param string $number the number
* @param string $type what type of validator to use
* @return boolean
22 changes: 17 additions & 5 deletions src/Validator.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php namespace Olssonm\IdentityNumber;
<?php

namespace Olssonm\IdentityNumber;

use DateTime;

@@ -15,13 +17,15 @@ class Validator

/**
* Current validation type
*
* @var string
*/
protected $type;

/**
* Numbers that actually passes the Luhn-algorithm but
* that are non-valid
*
* @var array
*/
protected $invalidNumbers = [
@@ -33,10 +37,14 @@ class Validator
'9999999999'
];

public function __construct() {}
public function __construct()
{
//
}

/**
* Main validation method
*
* @param string $number the number under validation
* @return boolean
*/
@@ -67,14 +75,15 @@ public function isValid($number)

/**
* Internal validator
*
* @param string $number the value under validation
* @param boolean $checkDate if date validation is to be performed
* @return boolean
*/
private function validate($number, $checkDate)
{
// Perform simple test on invalid numbers
if(in_array($number, $this->invalidNumbers)) {
if (in_array($number, $this->invalidNumbers)) {
return false;
}

@@ -93,6 +102,7 @@ private function validate($number, $checkDate)

/**
* Run the IdentityNumberFormatter on the specified number
*
* @param string $number
* @return string
*/
@@ -106,6 +116,7 @@ protected function formatNumber($number)

/**
* Perform luhn validation
*
* @param string $number
* @return boolean
*/
@@ -114,11 +125,11 @@ private function luhn($number)
settype($number, 'string');
$number = array_reverse(str_split($number));
$sum = 0;
foreach($number as $key => $number){
foreach ($number as $key => $number) {
if (!is_numeric($number)) {
return false;
}
if($key % 2) {
if ($key % 2) {
$number = $number * 2;
}
$sum += ($number >= 10 ? $number - 9 : $number);
@@ -128,6 +139,7 @@ private function luhn($number)

/**
* Validate a date as a format
*
* @param string $dateTest the date to be tested
* @param string $format the date format
* @return boolean
Loading