Skip to content
This repository has been archived by the owner on Dec 11, 2020. It is now read-only.

Commit

Permalink
Add a swiss social security number (AVS13) generator (#1533)
Browse files Browse the repository at this point in the history
* Add a Swiss Social Security Number (AVS13) generator.

* Add a Swiss Social Security Number (AVS13) generator - (includes changes)

* Set avs13 methods as static

* Add relevant documentation to the README.

* Update README to include documentation for randomDigitsString.

* Add missing tests for the randomDigitsString method

* Remove randomDigitsString function as a similar numerify() function already exists.

* Fix incorrect indentation

* Remove unwanted method documentation

* Rename test method to be consistent with the other PersonTest.php files in de_CH and it_CH.
  • Loading branch information
nhedger authored and pimjansen committed Sep 3, 2019
1 parent 9979692 commit 1cc3ca6
Show file tree
Hide file tree
Showing 9 changed files with 318 additions and 0 deletions.
25 changes: 25 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -881,6 +881,15 @@ echo $faker->cvr; // "32458723"
echo $faker->p; // "5398237590"
```

### `Faker\Provider\de_CH\Person`
```php
<?php

// Generates a random AVS13/AHV13 social security number
echo $faker->avs13; // "756.1234.5678.97" OR
echo $faker->ahv13; // "756.1234.5678.97"
```

### `Faker\Provider\de_DE\Payment`

```php
Expand Down Expand Up @@ -1135,6 +1144,14 @@ echo $faker->nationalId; // 'V11223344'
echo $faker->taxpayerIdentificationNumber; // 'J1234567891'
```

### `Faker\Provider\fr_CH\Person`
```php
<?php

// Generates a random AVS13/AHV13 social security number
echo $faker->avs13; // "756.1234.5678.97"
```

### `Faker\Provider\fr_FR\Address`

```php
Expand Down Expand Up @@ -1235,6 +1252,14 @@ echo $faker->bankAccountNumber; // "HU09904437680048220079300783"
echo $faker->nik(); // "8522246001570940"
```

### `Faker\Provider\it_CH\Person`
```php
<?php

// Generates a random AVS13/AHV13 social security number
echo $faker->avs13; // "756.1234.5678.97"
```

### `Faker\Provider\it_IT\Company`

```php
Expand Down
55 changes: 55 additions & 0 deletions src/Faker/Calculator/Ean.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace Faker\Calculator;

/**
* Utility class for validating EAN-8 and EAN-13 numbers
*
* @package Faker\Calculator
*/
class Ean
{
/** @var string EAN validation pattern */
const PATTERN = '/^(?:\d{8}|\d{13})$/';

/**
* Computes the checksum of an EAN number.
*
* @see https://en.wikipedia.org/wiki/International_Article_Number
*
* @param string $digits
* @return int
*/
public static function checksum($digits)
{
$length = strlen($digits);

$even = 0;
for ($i = $length - 1; $i >= 0; $i -= 2) {
$even += $digits[$i];
}

$odd = 0;
for ($i = $length - 2; $i >= 0; $i -= 2) {
$odd += $digits[$i];
}

return (10 - ((3 * $even + $odd) % 10)) % 10;
}

/**
* Checks whether the provided number is an EAN compliant number and that
* the checksum is correct.
*
* @param string $ean An EAN number
* @return boolean
*/
public static function isValid($ean)
{
if (!preg_match(self::PATTERN, $ean)) {
return false;
}

return self::checksum(substr($ean, 0, -1)) === intval(substr($ean, -1));
}
}
28 changes: 28 additions & 0 deletions src/Faker/Provider/de_CH/Person.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,32 @@ class Person extends \Faker\Provider\de_DE\Person
'Wagner', 'Walker', 'Walser', 'Weber', 'Wehrli', 'Weibel', 'Weiss', 'Wenger', 'Wicki', 'Widmer', 'Willi', 'Wirth', 'Wirz', 'Wittwer', 'Wolf', 'Wyss', 'Wüthrich',
'Zaugg', 'Zbinden', 'Zehnder', 'Ziegler', 'Zimmermann', 'Zwahlen', 'Zürcher',
);

/**
* Generates a valid random AVS13 (swiss social security) number
*
* This function acts as a localized alias for the function defined in the
* fr_CH provider. In the german-speaking part of Switzerland, the AVS13
* number is generally known as AHV13.
*
* @see \Faker\Provider\fr_CH\Person::avs13()
* @return string
*/
public static function ahv13()
{
return \Faker\Provider\fr_CH\Person::avs13();
}

/**
* Generates a valid random AVS13 (swiss social security) number
*
* This function acts as an alias for the function defined in the fr_CH provider.
*
* @see \Faker\Provider\fr_CH\Person::avs13()
* @return string
*/
public static function avs13()
{
return \Faker\Provider\fr_CH\Person::avs13();
}
}
25 changes: 25 additions & 0 deletions src/Faker/Provider/fr_CH/Person.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,29 @@ class Person extends \Faker\Provider\fr_FR\Person
'Waeber', 'Weber', 'Wenger', 'Widmer', 'Wyss',
'Zbinden', 'Zimmermann',
);

/**
* Generates a valid random AVS13 (swiss social security) number
*
* This function will generate a valid random AVS13 number and return it
* as a formatted string.
*
* @see https://www.zas.admin.ch/zas/fr/home/partenaires-et-institutions-/navs13.html
* @return string
*/
public static function avs13()
{
$p = array(
756,
self::numerify('####'),
self::numerify('####'),
self::numerify('#'),
);

$checksum = \Faker\Calculator\Ean::checksum(implode($p));

$avs = sprintf("%s.%s.%s.%s%s", $p[0], $p[1], $p[2], $p[3], $checksum);

return $avs;
}
}
13 changes: 13 additions & 0 deletions src/Faker/Provider/it_CH/Person.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,17 @@ class Person extends \Faker\Provider\it_IT\Person
'Weber', 'Widmer',
'Zanetti', 'Zanini', 'Zimmermann',
);

/**
* Generates a valid random AVS13 (swiss social security) number
*
* This function acts as an alias for the function defined in the fr_CH provider.
*
* @see \Faker\Provider\fr_CH\Person::avs13()
* @return string
*/
public static function avs13()
{
return \Faker\Provider\fr_CH\Person::avs13();
}
}
84 changes: 84 additions & 0 deletions test/Faker/Calculator/EanTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php


namespace Faker\Test\Calculator;


use Faker\Calculator\Ean;
use PHPUnit\Framework\TestCase;

class EanTest extends TestCase
{
public function Ean8checksumProvider()
{
return array(
array('1234567', '0'),
array('2345678', '5'),
array('3456789', '0'),
);
}

public function ean8ValidationProvider()
{
return array(
array('1234567891231', true),
array('2354698521469', true),
array('3001092650834', false),
array('3921092190838', false),
);
}

/**
* @dataProvider Ean8checksumProvider
*/
public function testChecksumEan8($partial, $checksum)
{
$this->assertEquals($checksum, Ean::checksum($partial));
}

/**
* @dataProvider ean8ValidationProvider
*/
public function testEan8Validation($ean8, $valid)
{
$this->assertTrue(Ean::isValid($ean8) === $valid);
}

public function Ean13checksumProvider()
{
return array(
array('123456789123', '1'),
array('978020137962', '4'),
array('235469852146', '9'),
array('300109265083', '5'),
array('392109219083', '7'),
);
}

public function ean13ValidationProvider()
{
return array(
array('1234567891231', true),
array('2354698521469', true),
array('3001092650834', false),
array('3921092190838', false),
);
}

/**
* @dataProvider Ean13checksumProvider
*/
public function testChecksumEan13($partial, $checksum)
{
$this->assertEquals($checksum, Ean::checksum($partial));
}

/**
* @dataProvider ean13ValidationProvider
*/
public function testEan13Validation($ean13, $valid)
{
$this->assertTrue(Ean::isValid($ean13) === $valid);
}

}
34 changes: 34 additions & 0 deletions test/Faker/Provider/de_CH/PersonTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Faker\Test\Provider\de_CH;

use Faker\Calculator\Ean;
use Faker\Generator;
use Faker\Provider\de_CH\Person;
use PHPUnit\Framework\TestCase;

class PersonTest extends TestCase
{
private $faker;

public function setUp()
{
$faker = new Generator();
$faker->addProvider(new Person($faker));
$this->faker = $faker;
}

public function testAvs13Number()
{
$avs = $this->faker->avs13;
$this->assertRegExp('/^756\.([0-9]{4})\.([0-9]{4})\.([0-9]{2})$/', $avs);
$this->assertTrue(Ean::isValid(str_replace('.', '', $avs)));
}

public function testAhv13Number()
{
$ahv = $this->faker->ahv13;
$this->assertRegExp('/^756\.([0-9]{4})\.([0-9]{4})\.([0-9]{2})$/', $ahv);
$this->assertTrue(Ean::isValid(str_replace('.', '', $ahv)));
}
}
27 changes: 27 additions & 0 deletions test/Faker/Provider/fr_CH/PersonTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Faker\Test\Provider\fr_CH;

use Faker\Calculator\Ean;
use Faker\Generator;
use Faker\Provider\fr_CH\Person;
use PHPUnit\Framework\TestCase;

class PersonTest extends TestCase
{
private $faker;

public function setUp()
{
$faker = new Generator();
$faker->addProvider(new Person($faker));
$this->faker = $faker;
}

public function testAvs13Number()
{
$avs = $this->faker->avs13;
$this->assertRegExp('/^756\.([0-9]{4})\.([0-9]{4})\.([0-9]{2})$/', $avs);
$this->assertTrue(Ean::isValid(str_replace('.', '', $avs)));
}
}
27 changes: 27 additions & 0 deletions test/Faker/Provider/it_CH/PersonTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Faker\Test\Provider\it_CH;

use Faker\Calculator\Ean;
use Faker\Generator;
use Faker\Provider\it_CH\Person;
use PHPUnit\Framework\TestCase;

class PersonTest extends TestCase
{
private $faker;

public function setUp()
{
$faker = new Generator();
$faker->addProvider(new Person($faker));
$this->faker = $faker;
}

public function testAvs13Number()
{
$avs = $this->faker->avs13;
$this->assertRegExp('/^756\.([0-9]{4})\.([0-9]{4})\.([0-9]{2})$/', $avs);
$this->assertTrue(Ean::isValid(str_replace('.', '', $avs)));
}
}

0 comments on commit 1cc3ca6

Please sign in to comment.