This repository has been archived by the owner on Dec 11, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
Showing
9 changed files
with
318 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))); | ||
} | ||
} |