Skip to content

Commit

Permalink
Move TCNo and INN from calculator to localized providers (FakerPHP#108)
Browse files Browse the repository at this point in the history
* move tcno and inn from calculator to localized providers

* change inn method name

* cleanup

* Update src/Faker/Calculator/Inn.php

Co-authored-by: Bram Ceulemans <[email protected]>

* cleanup

* fix bc change

* Update src/Faker/Calculator/Inn.php

Co-authored-by: Bram Ceulemans <[email protected]>

* Update src/Faker/Calculator/TCNo.php

Co-authored-by: Bram Ceulemans <[email protected]>

* Update src/Faker/Calculator/Inn.php

Co-authored-by: Bram Ceulemans <[email protected]>

* Update src/Faker/Calculator/TCNo.php

Co-authored-by: Bram Ceulemans <[email protected]>

* Update src/Faker/Calculator/TCNo.php

Co-authored-by: Bram Ceulemans <[email protected]>

* Update src/Faker/Provider/ru_RU/Company.php

* Update src/Faker/Provider/ru_RU/Company.php

* Update src/Faker/Provider/ru_RU/Company.php

Co-authored-by: Bram Ceulemans <[email protected]>

* Update src/Faker/Provider/ru_RU/Company.php

Co-authored-by: Bram Ceulemans <[email protected]>

* Update src/Faker/Provider/tr_TR/Person.php

Co-authored-by: Bram Ceulemans <[email protected]>

* Update src/Faker/Provider/ru_RU/Company.php

Co-authored-by: Bram Ceulemans <[email protected]>

* Update src/Faker/Provider/ru_RU/Company.php

Co-authored-by: Bram Ceulemans <[email protected]>

* Improve consistency of comments and phpdoc (FakerPHP#110)

* Improve consistency of comments and phpdoc

* Add StyleCI changes

* Enable whitespace_after_comma_in_array fixer (FakerPHP#112)

* Enable whitespace_after_comma_in_array fixer

* Apply fixes from StyleCI

* Minor cleanup (FakerPHP#113)

* enable StyleCI single_quote (FakerPHP#116)

* Apply fixes from StyleCI

* Enable modernize_types_casting fixer (FakerPHP#117)

* Enable modernize_types_casting fixer

* Apply fixes from StyleCI

* move tcno and inn from calculator to localized providers

* fix bc change

* fix merge issue

* fix newline

* re-add docblock empty line

* add type to data provider params

Signed-off-by: Daniel Schmelz <[email protected]>

* run make cs

* run make cs

Co-authored-by: Bram Ceulemans <[email protected]>
Co-authored-by: Graham Campbell <[email protected]>
  • Loading branch information
3 people committed Dec 24, 2020
1 parent 16f4584 commit d6062dd
Show file tree
Hide file tree
Showing 8 changed files with 225 additions and 151 deletions.
21 changes: 12 additions & 9 deletions src/Faker/Calculator/Inn.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

namespace Faker\Calculator;

/**
* @deprecated moved to ru_RU\Company, use {@link \Faker\Provider\ru_RU\Company}.
* @see \Faker\Provider\ru_RU\Company
*/
class Inn
{
/**
Expand All @@ -12,17 +16,13 @@ class Inn
* @param string $inn
*
* @return string Checksum (one digit)
*
* @deprecated use {@link \Faker\Provider\ru_RU\Company::inn10Checksum()} instead
* @see \Faker\Provider\ru_RU\Company::inn10Checksum()
*/
public static function checksum($inn)
{
$multipliers = [2, 4, 10, 3, 5, 9, 4, 6, 8];
$sum = 0;

for ($i = 0; $i < 9; ++$i) {
$sum += (int) $inn[$i] * $multipliers[$i];
}

return (string) (($sum % 11) % 10);
return \Faker\Provider\ru_RU\Company::inn10Checksum($inn);
}

/**
Expand All @@ -31,9 +31,12 @@ public static function checksum($inn)
* @param string $inn
*
* @return bool
*
* @deprecated use {@link \Faker\Provider\ru_RU\Company::inn10IsValid()} instead
* @see \Faker\Provider\ru_RU\Company::inn10IsValid()
*/
public static function isValid($inn)
{
return strlen($inn) === 10 && self::checksum($inn) === $inn[9];
return \Faker\Provider\ru_RU\Company::inn10IsValid($inn);
}
}
36 changes: 12 additions & 24 deletions src/Faker/Calculator/TCNo.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

namespace Faker\Calculator;

use InvalidArgumentException;

/**
* @deprecated moved to tr_TR\Person, use {@link \Faker\Provider\tr_TR\Person}.
* @see \Faker\Provider\tr_TR\Person
*/
class TCNo
{
/**
Expand All @@ -15,30 +17,13 @@ class TCNo
* @param string $identityPrefix
*
* @return string Checksum (two digit)
*
* @deprecated use {@link \Faker\Provider\tr_TR\Person::tcNoChecksum()} instead
* @see \Faker\Provider\tr_TR\Person::tcNoChecksum()
*/
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;
return \Faker\Provider\tr_TR\Person::tcNoChecksum($identityPrefix);
}

/**
Expand All @@ -47,9 +32,12 @@ public static function checksum($identityPrefix)
* @param string $tcNo
*
* @return bool
*
* @deprecated use {@link \Faker\Provider\tr_TR\Person::tcNoIsValid()} instead
* @see \Faker\Provider\tr_TR\Person::tcNoIsValid()
*/
public static function isValid($tcNo)
{
return self::checksum(substr($tcNo, 0, -2)) === substr($tcNo, -2, 2);
return \Faker\Provider\tr_TR\Person::tcNoIsValid($tcNo);
}
}
63 changes: 59 additions & 4 deletions src/Faker/Provider/ru_RU/Company.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,31 @@ public static function companyNameSuffix()
return static::randomElement(static::$companyNameSuffixes);
}

/**
* Generates a Russian Taxpayer Personal Identification Number
*
* @param string $area_code
*
* @return string
*
* @deprecated use {@link \Faker\Provider\ru_RU\Company::inn10()} instead
* @see \Faker\Provider\ru_RU\Company::inn10()
*/
public static function inn($area_code = '')
{
if ($area_code === '' || (int) $area_code == 0) {
return self::inn10($area_code);
}

/**
* Generates a Russian Taxpayer Personal Identification Number
*
* @param string $area_code
*
* @return string
*/
public static function inn10($area_code = '')
{
if ($area_code === '' || (int) $area_code === 0) {
//Simple generation code for areas in Russian without check for valid
$area_code = self::numberBetween(1, 91);
} else {
Expand All @@ -108,15 +130,48 @@ public static function inn($area_code = '')
$area_code = str_pad($area_code, 2, '0', STR_PAD_LEFT);
$inn_base = $area_code . static::numerify('#######');

return $inn_base . \Faker\Calculator\Inn::checksum($inn_base);
return $inn_base . self::inn10Checksum($inn_base);
}

public static function kpp($inn = '')
{
if ($inn == '' || strlen($inn) < 4) {
$inn = static::inn();
if ($inn === '' || strlen($inn) < 4) {
$inn = self::inn10();
}

return substr($inn, 0, 4) . '01001';
}

/**
* Generates INN Checksum
*
* @see https://ru.wikipedia.org/wiki/%D0%98%D0%B4%D0%B5%D0%BD%D1%82%D0%B8%D1%84%D0%B8%D0%BA%D0%B0%D1%86%D0%B8%D0%BE%D0%BD%D0%BD%D1%8B%D0%B9_%D0%BD%D0%BE%D0%BC%D0%B5%D1%80_%D0%BD%D0%B0%D0%BB%D0%BE%D0%B3%D0%BE%D0%BF%D0%BB%D0%B0%D1%82%D0%B5%D0%BB%D1%8C%D1%89%D0%B8%D0%BA%D0%B0
*
* @param string $inn
*
* @return string Checksum (one digit)
*/
public static function inn10Checksum($inn)
{
$multipliers = [2, 4, 10, 3, 5, 9, 4, 6, 8];
$sum = 0;

for ($i = 0; $i < 9; ++$i) {
$sum += (int) $inn[$i] * $multipliers[$i];
}

return (string) (($sum % 11) % 10);
}

/**
* Checks whether an INN has a valid checksum
*
* @param string $inn
*
* @return bool
*/
public static function inn10IsValid($inn)
{
return strlen($inn) === 10 && self::inn10Checksum($inn) === $inn[9];
}
}
51 changes: 49 additions & 2 deletions src/Faker/Provider/tr_TR/Person.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Faker\Provider\tr_TR;

use Faker\Calculator\TCNo;
use InvalidArgumentException;

class Person extends \Faker\Provider\Person
{
Expand Down Expand Up @@ -107,8 +107,55 @@ public static function titleFemale()
public function tcNo()
{
$randomDigits = static::numerify('#########');
$checksum = TCNo::checksum($randomDigits);
$checksum = self::tcNoChecksum($randomDigits);

return $randomDigits . $checksum;
}

/**
* Generates Turkish Identity Number Checksum
* Gets first 9 digit as prefix and calculates checksum
*
* @see https://en.wikipedia.org/wiki/Turkish_Identification_Number
*
* @param string $identityPrefix
*
* @return string Checksum (two digit)
*/
public static function tcNoChecksum($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 a TCNo has a valid checksum
*
* @param string $tcNo
*
* @return bool
*/
public static function tcNoIsValid($tcNo)
{
return self::tcNoChecksum(substr($tcNo, 0, -2)) === substr($tcNo, -2, 2);
}
}
49 changes: 0 additions & 49 deletions test/Faker/Calculator/InnTest.php

This file was deleted.

56 changes: 0 additions & 56 deletions test/Faker/Calculator/TCNoTest.php

This file was deleted.

Loading

0 comments on commit d6062dd

Please sign in to comment.