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

Turkish identity number for tr_TR #1462

Merged
merged 2 commits into from
Jun 18, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -1610,6 +1610,15 @@ echo $faker->personalIdentityNumber() // '950910-0799'
//Since the numbers are different for male and female persons, optionally you can specify gender.
echo $faker->personalIdentityNumber('female') // '950910-0781'
```
### `Faker\Provider\tr_TR\Person`

```php
<?php

//Generates a valid Turkish identity number (in Turkish - T.C. Kimlik No)
echo $faker->tcNo // '55300634882'

```


### `Faker\Provider\zh_CN\Payment`
Expand Down
52 changes: 52 additions & 0 deletions src/Faker/Calculator/TCNo.php
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);
}
}
15 changes: 15 additions & 0 deletions src/Faker/Provider/tr_TR/Person.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Faker\Provider\tr_TR;

use Faker\Calculator\TCNo;

class Person extends \Faker\Provider\Person
{
/**
Expand Down Expand Up @@ -94,4 +96,17 @@ public static function titleFemale()
{
return static::titleMale();
}

/**
* National Personal Identity number (tc kimlik no)
* @link https://en.wikipedia.org/wiki/Turkish_Identification_Number
* @return string on format XXXXXXXXXXX
*/
public function tcNo()
{
$randomDigits = static::numerify('#########');
$checksum = TCNo::checksum($randomDigits);

return $randomDigits . $checksum;
}
}
54 changes: 54 additions & 0 deletions test/Faker/Calculator/TCNoTest.php
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);
}
}
28 changes: 28 additions & 0 deletions test/Faker/Provider/tr_TR/CompanyTest.php
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);
}
}
29 changes: 29 additions & 0 deletions test/Faker/Provider/tr_TR/PaymentTest.php
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));
}
}
34 changes: 34 additions & 0 deletions test/Faker/Provider/tr_TR/PersonTest.php
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));
}
}
}
34 changes: 34 additions & 0 deletions test/Faker/Provider/tr_TR/PhoneNumberTest.php
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));
}
}
}