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

Commit 1cc3ca6

Browse files
nhedgerpimjansen
authored andcommitted
Add a swiss social security number (AVS13) generator (#1533)
* 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.
1 parent 9979692 commit 1cc3ca6

File tree

9 files changed

+318
-0
lines changed

9 files changed

+318
-0
lines changed

readme.md

+25
Original file line numberDiff line numberDiff line change
@@ -881,6 +881,15 @@ echo $faker->cvr; // "32458723"
881881
echo $faker->p; // "5398237590"
882882
```
883883

884+
### `Faker\Provider\de_CH\Person`
885+
```php
886+
<?php
887+
888+
// Generates a random AVS13/AHV13 social security number
889+
echo $faker->avs13; // "756.1234.5678.97" OR
890+
echo $faker->ahv13; // "756.1234.5678.97"
891+
```
892+
884893
### `Faker\Provider\de_DE\Payment`
885894

886895
```php
@@ -1135,6 +1144,14 @@ echo $faker->nationalId; // 'V11223344'
11351144
echo $faker->taxpayerIdentificationNumber; // 'J1234567891'
11361145
```
11371146

1147+
### `Faker\Provider\fr_CH\Person`
1148+
```php
1149+
<?php
1150+
1151+
// Generates a random AVS13/AHV13 social security number
1152+
echo $faker->avs13; // "756.1234.5678.97"
1153+
```
1154+
11381155
### `Faker\Provider\fr_FR\Address`
11391156

11401157
```php
@@ -1235,6 +1252,14 @@ echo $faker->bankAccountNumber; // "HU09904437680048220079300783"
12351252
echo $faker->nik(); // "8522246001570940"
12361253
```
12371254

1255+
### `Faker\Provider\it_CH\Person`
1256+
```php
1257+
<?php
1258+
1259+
// Generates a random AVS13/AHV13 social security number
1260+
echo $faker->avs13; // "756.1234.5678.97"
1261+
```
1262+
12381263
### `Faker\Provider\it_IT\Company`
12391264

12401265
```php

src/Faker/Calculator/Ean.php

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
namespace Faker\Calculator;
4+
5+
/**
6+
* Utility class for validating EAN-8 and EAN-13 numbers
7+
*
8+
* @package Faker\Calculator
9+
*/
10+
class Ean
11+
{
12+
/** @var string EAN validation pattern */
13+
const PATTERN = '/^(?:\d{8}|\d{13})$/';
14+
15+
/**
16+
* Computes the checksum of an EAN number.
17+
*
18+
* @see https://en.wikipedia.org/wiki/International_Article_Number
19+
*
20+
* @param string $digits
21+
* @return int
22+
*/
23+
public static function checksum($digits)
24+
{
25+
$length = strlen($digits);
26+
27+
$even = 0;
28+
for ($i = $length - 1; $i >= 0; $i -= 2) {
29+
$even += $digits[$i];
30+
}
31+
32+
$odd = 0;
33+
for ($i = $length - 2; $i >= 0; $i -= 2) {
34+
$odd += $digits[$i];
35+
}
36+
37+
return (10 - ((3 * $even + $odd) % 10)) % 10;
38+
}
39+
40+
/**
41+
* Checks whether the provided number is an EAN compliant number and that
42+
* the checksum is correct.
43+
*
44+
* @param string $ean An EAN number
45+
* @return boolean
46+
*/
47+
public static function isValid($ean)
48+
{
49+
if (!preg_match(self::PATTERN, $ean)) {
50+
return false;
51+
}
52+
53+
return self::checksum(substr($ean, 0, -1)) === intval(substr($ean, -1));
54+
}
55+
}

src/Faker/Provider/de_CH/Person.php

+28
Original file line numberDiff line numberDiff line change
@@ -86,4 +86,32 @@ class Person extends \Faker\Provider\de_DE\Person
8686
'Wagner', 'Walker', 'Walser', 'Weber', 'Wehrli', 'Weibel', 'Weiss', 'Wenger', 'Wicki', 'Widmer', 'Willi', 'Wirth', 'Wirz', 'Wittwer', 'Wolf', 'Wyss', 'Wüthrich',
8787
'Zaugg', 'Zbinden', 'Zehnder', 'Ziegler', 'Zimmermann', 'Zwahlen', 'Zürcher',
8888
);
89+
90+
/**
91+
* Generates a valid random AVS13 (swiss social security) number
92+
*
93+
* This function acts as a localized alias for the function defined in the
94+
* fr_CH provider. In the german-speaking part of Switzerland, the AVS13
95+
* number is generally known as AHV13.
96+
*
97+
* @see \Faker\Provider\fr_CH\Person::avs13()
98+
* @return string
99+
*/
100+
public static function ahv13()
101+
{
102+
return \Faker\Provider\fr_CH\Person::avs13();
103+
}
104+
105+
/**
106+
* Generates a valid random AVS13 (swiss social security) number
107+
*
108+
* This function acts as an alias for the function defined in the fr_CH provider.
109+
*
110+
* @see \Faker\Provider\fr_CH\Person::avs13()
111+
* @return string
112+
*/
113+
public static function avs13()
114+
{
115+
return \Faker\Provider\fr_CH\Person::avs13();
116+
}
89117
}

src/Faker/Provider/fr_CH/Person.php

+25
Original file line numberDiff line numberDiff line change
@@ -87,4 +87,29 @@ class Person extends \Faker\Provider\fr_FR\Person
8787
'Waeber', 'Weber', 'Wenger', 'Widmer', 'Wyss',
8888
'Zbinden', 'Zimmermann',
8989
);
90+
91+
/**
92+
* Generates a valid random AVS13 (swiss social security) number
93+
*
94+
* This function will generate a valid random AVS13 number and return it
95+
* as a formatted string.
96+
*
97+
* @see https://www.zas.admin.ch/zas/fr/home/partenaires-et-institutions-/navs13.html
98+
* @return string
99+
*/
100+
public static function avs13()
101+
{
102+
$p = array(
103+
756,
104+
self::numerify('####'),
105+
self::numerify('####'),
106+
self::numerify('#'),
107+
);
108+
109+
$checksum = \Faker\Calculator\Ean::checksum(implode($p));
110+
111+
$avs = sprintf("%s.%s.%s.%s%s", $p[0], $p[1], $p[2], $p[3], $checksum);
112+
113+
return $avs;
114+
}
90115
}

src/Faker/Provider/it_CH/Person.php

+13
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,17 @@ class Person extends \Faker\Provider\it_IT\Person
8585
'Weber', 'Widmer',
8686
'Zanetti', 'Zanini', 'Zimmermann',
8787
);
88+
89+
/**
90+
* Generates a valid random AVS13 (swiss social security) number
91+
*
92+
* This function acts as an alias for the function defined in the fr_CH provider.
93+
*
94+
* @see \Faker\Provider\fr_CH\Person::avs13()
95+
* @return string
96+
*/
97+
public static function avs13()
98+
{
99+
return \Faker\Provider\fr_CH\Person::avs13();
100+
}
88101
}

test/Faker/Calculator/EanTest.php

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?php
2+
3+
4+
namespace Faker\Test\Calculator;
5+
6+
7+
use Faker\Calculator\Ean;
8+
use PHPUnit\Framework\TestCase;
9+
10+
class EanTest extends TestCase
11+
{
12+
public function Ean8checksumProvider()
13+
{
14+
return array(
15+
array('1234567', '0'),
16+
array('2345678', '5'),
17+
array('3456789', '0'),
18+
);
19+
}
20+
21+
public function ean8ValidationProvider()
22+
{
23+
return array(
24+
array('1234567891231', true),
25+
array('2354698521469', true),
26+
array('3001092650834', false),
27+
array('3921092190838', false),
28+
);
29+
}
30+
31+
/**
32+
* @dataProvider Ean8checksumProvider
33+
*/
34+
public function testChecksumEan8($partial, $checksum)
35+
{
36+
$this->assertEquals($checksum, Ean::checksum($partial));
37+
}
38+
39+
/**
40+
* @dataProvider ean8ValidationProvider
41+
*/
42+
public function testEan8Validation($ean8, $valid)
43+
{
44+
$this->assertTrue(Ean::isValid($ean8) === $valid);
45+
}
46+
47+
public function Ean13checksumProvider()
48+
{
49+
return array(
50+
array('123456789123', '1'),
51+
array('978020137962', '4'),
52+
array('235469852146', '9'),
53+
array('300109265083', '5'),
54+
array('392109219083', '7'),
55+
);
56+
}
57+
58+
public function ean13ValidationProvider()
59+
{
60+
return array(
61+
array('1234567891231', true),
62+
array('2354698521469', true),
63+
array('3001092650834', false),
64+
array('3921092190838', false),
65+
);
66+
}
67+
68+
/**
69+
* @dataProvider Ean13checksumProvider
70+
*/
71+
public function testChecksumEan13($partial, $checksum)
72+
{
73+
$this->assertEquals($checksum, Ean::checksum($partial));
74+
}
75+
76+
/**
77+
* @dataProvider ean13ValidationProvider
78+
*/
79+
public function testEan13Validation($ean13, $valid)
80+
{
81+
$this->assertTrue(Ean::isValid($ean13) === $valid);
82+
}
83+
84+
}
+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace Faker\Test\Provider\de_CH;
4+
5+
use Faker\Calculator\Ean;
6+
use Faker\Generator;
7+
use Faker\Provider\de_CH\Person;
8+
use PHPUnit\Framework\TestCase;
9+
10+
class PersonTest extends TestCase
11+
{
12+
private $faker;
13+
14+
public function setUp()
15+
{
16+
$faker = new Generator();
17+
$faker->addProvider(new Person($faker));
18+
$this->faker = $faker;
19+
}
20+
21+
public function testAvs13Number()
22+
{
23+
$avs = $this->faker->avs13;
24+
$this->assertRegExp('/^756\.([0-9]{4})\.([0-9]{4})\.([0-9]{2})$/', $avs);
25+
$this->assertTrue(Ean::isValid(str_replace('.', '', $avs)));
26+
}
27+
28+
public function testAhv13Number()
29+
{
30+
$ahv = $this->faker->ahv13;
31+
$this->assertRegExp('/^756\.([0-9]{4})\.([0-9]{4})\.([0-9]{2})$/', $ahv);
32+
$this->assertTrue(Ean::isValid(str_replace('.', '', $ahv)));
33+
}
34+
}
+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace Faker\Test\Provider\fr_CH;
4+
5+
use Faker\Calculator\Ean;
6+
use Faker\Generator;
7+
use Faker\Provider\fr_CH\Person;
8+
use PHPUnit\Framework\TestCase;
9+
10+
class PersonTest extends TestCase
11+
{
12+
private $faker;
13+
14+
public function setUp()
15+
{
16+
$faker = new Generator();
17+
$faker->addProvider(new Person($faker));
18+
$this->faker = $faker;
19+
}
20+
21+
public function testAvs13Number()
22+
{
23+
$avs = $this->faker->avs13;
24+
$this->assertRegExp('/^756\.([0-9]{4})\.([0-9]{4})\.([0-9]{2})$/', $avs);
25+
$this->assertTrue(Ean::isValid(str_replace('.', '', $avs)));
26+
}
27+
}
+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace Faker\Test\Provider\it_CH;
4+
5+
use Faker\Calculator\Ean;
6+
use Faker\Generator;
7+
use Faker\Provider\it_CH\Person;
8+
use PHPUnit\Framework\TestCase;
9+
10+
class PersonTest extends TestCase
11+
{
12+
private $faker;
13+
14+
public function setUp()
15+
{
16+
$faker = new Generator();
17+
$faker->addProvider(new Person($faker));
18+
$this->faker = $faker;
19+
}
20+
21+
public function testAvs13Number()
22+
{
23+
$avs = $this->faker->avs13;
24+
$this->assertRegExp('/^756\.([0-9]{4})\.([0-9]{4})\.([0-9]{2})$/', $avs);
25+
$this->assertTrue(Ean::isValid(str_replace('.', '', $avs)));
26+
}
27+
}

0 commit comments

Comments
 (0)