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.
Merge pull request #1462 from aykutaras/master
Turkish identity number for tr_TR
- Loading branch information
Showing
8 changed files
with
255 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,52 @@ | ||
<?php | ||
|
||
namespace Faker\Calculator; | ||
|
||
use InvalidArgumentException; | ||
|
||
class TCNo | ||
{ | ||
/** | ||
* Generates Turkish Identity Number Checksum | ||
* Gets first 9 digit as prefix and calcuates checksums | ||
* | ||
* https://en.wikipedia.org/wiki/Turkish_Identification_Number | ||
* | ||
* @param string $identityPrefix | ||
* @return string Checksum (two digit) | ||
*/ | ||
public static function checksum($identityPrefix) | ||
{ | ||
if (strlen((string)$identityPrefix) !== 9) { | ||
throw new InvalidArgumentException('Argument should be an integer and should be 9 digits.'); | ||
} | ||
|
||
$oddSum = 0; | ||
$evenSum = 0; | ||
|
||
$identityArray = array_map('intval', str_split($identityPrefix)); // Creates array from int | ||
foreach ($identityArray as $index => $digit) { | ||
if ($index % 2 == 0) { | ||
$evenSum += $digit; | ||
} else { | ||
$oddSum += $digit; | ||
} | ||
} | ||
|
||
$tenthDigit = (7 * $evenSum - $oddSum) % 10; | ||
$eleventhDigit = ($evenSum + $oddSum + $tenthDigit) % 10; | ||
|
||
return $tenthDigit . $eleventhDigit; | ||
} | ||
|
||
/** | ||
* Checks whether an TCNo has a valid checksum | ||
* | ||
* @param string $tcNo | ||
* @return boolean | ||
*/ | ||
public static function isValid($tcNo) | ||
{ | ||
return self::checksum(substr($tcNo, 0, -2)) === substr($tcNo, -2, 2); | ||
} | ||
} |
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,54 @@ | ||
<?php | ||
|
||
namespace Faker\Test\Calculator; | ||
|
||
use Faker\Calculator\TCNo; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class TCNoTest extends TestCase | ||
{ | ||
public function checksumProvider() | ||
{ | ||
return array( | ||
array('553006348', '82'), | ||
array('350630743', '78'), | ||
array('550600932', '88'), | ||
array('487932947', '70'), | ||
array('168113862', '40') | ||
); | ||
} | ||
|
||
/** | ||
* @dataProvider checksumProvider | ||
* @param $tcNo | ||
* @param $checksum | ||
*/ | ||
public function testChecksum($tcNo, $checksum) | ||
{ | ||
$this->assertEquals($checksum, TCNo::checksum($tcNo), $tcNo); | ||
} | ||
|
||
public function validatorProvider() | ||
{ | ||
return array( | ||
array('22978160678', true), | ||
array('26480045324', true), | ||
array('47278360658', true), | ||
array('34285002510', true), | ||
array('19874561012', true), | ||
|
||
array('11111111111', false), | ||
array('11234567899', false), | ||
); | ||
} | ||
|
||
/** | ||
* @dataProvider validatorProvider | ||
* @param $tcNo | ||
* @param $isValid | ||
*/ | ||
public function testIsValid($tcNo, $isValid) | ||
{ | ||
$this->assertEquals($isValid, TCNo::isValid($tcNo), $tcNo); | ||
} | ||
} |
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,28 @@ | ||
<?php | ||
|
||
namespace Faker\Test\Provider\tr_TR; | ||
|
||
use Faker\Provider\tr_TR\Company; | ||
use Faker\Generator; | ||
|
||
class CompanyTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
|
||
/** | ||
* @var Generator | ||
*/ | ||
private $faker; | ||
|
||
public function setUp() | ||
{ | ||
$faker = new Generator(); | ||
$faker->addProvider(new Company($faker)); | ||
$this->faker = $faker; | ||
} | ||
|
||
public function testCompany() | ||
{ | ||
$company = $this->faker->companyField; | ||
$this->assertNotNull($company); | ||
} | ||
} |
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,29 @@ | ||
<?php | ||
|
||
|
||
namespace Faker\Provider\tr_TR; | ||
|
||
use Faker\Generator; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class PaymentTest extends TestCase | ||
{ | ||
/** | ||
* @var Generator | ||
*/ | ||
private $faker; | ||
|
||
public function setUp() | ||
{ | ||
$faker = new Generator(); | ||
$faker->addProvider(new Payment($faker)); | ||
$this->faker = $faker; | ||
} | ||
|
||
public function testBankAccountNumber() | ||
{ | ||
$accNo = $this->faker->bankAccountNumber; | ||
$this->assertEquals(substr($accNo, 0, 2), 'TR'); | ||
$this->assertEquals(26, strlen($accNo)); | ||
} | ||
} |
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\tr_TR; | ||
|
||
use Faker\Calculator\TCNo; | ||
use Faker\Provider\tr_TR\Person; | ||
use Faker\Generator; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class PersonTest extends TestCase | ||
{ | ||
|
||
/** | ||
* @var Generator | ||
*/ | ||
private $faker; | ||
|
||
public function setUp() | ||
{ | ||
$faker = new Generator(); | ||
$faker->addProvider(new Person($faker)); | ||
$this->faker = $faker; | ||
} | ||
|
||
public function testTCNo() | ||
{ | ||
for ($i = 0; $i < 100; $i++) { | ||
$number = $this->faker->tcNo; | ||
|
||
$this->assertEquals(11, strlen($number)); | ||
$this->assertTrue(TCNo::isValid($number)); | ||
} | ||
} | ||
} |
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\tr_TR; | ||
|
||
use Faker\Generator; | ||
use Faker\Provider\tr_TR\PhoneNumber; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class PhoneNumberTest extends TestCase | ||
{ | ||
|
||
/** | ||
* @var Generator | ||
*/ | ||
private $faker; | ||
|
||
public function setUp() | ||
{ | ||
$faker = new Generator(); | ||
$faker->addProvider(new PhoneNumber($faker)); | ||
$this->faker = $faker; | ||
} | ||
|
||
public function testPhoneNumber() | ||
{ | ||
for ($i = 0; $i < 100; $i++) { | ||
$number = $this->faker->phoneNumber; | ||
$baseNumber = preg_replace('/ *x.*$/', '', $number); // Remove possible extension | ||
$digits = array_values(array_filter(str_split($baseNumber), 'ctype_digit')); | ||
|
||
$this->assertGreaterThan(10, count($digits)); | ||
} | ||
} | ||
} |