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 #1204 from pwsdotru/issue-inn-and-kpp
Add INN and KPP support for ru_RU locale
- Loading branch information
Showing
5 changed files
with
147 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,34 @@ | ||
<?php | ||
namespace Faker\Calculator; | ||
|
||
class Inn | ||
{ | ||
/** | ||
* Generates INN Checksum | ||
* | ||
* 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 checksum($inn) | ||
{ | ||
$multipliers = array(1 => 2, 2 => 4, 3 => 10, 4 => 3, 5 => 5, 6 => 9, 7 => 4, 8 => 6, 9 => 8); | ||
$sum = 0; | ||
for ($i = 1; $i <= 9; $i++) { | ||
$sum += intval(substr($inn, $i-1, 1)) * $multipliers[$i]; | ||
} | ||
return strval(($sum % 11) % 10); | ||
} | ||
|
||
/** | ||
* Checks whether an INN has a valid checksum | ||
* | ||
* @param string $inn | ||
* @return boolean | ||
*/ | ||
public static function isValid($inn) | ||
{ | ||
return self::checksum(substr($inn, 0, -1)) === substr($inn, -1, 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
|
||
namespace Faker\Test\Calculator; | ||
|
||
use Faker\Calculator\Inn; | ||
|
||
class InnTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
|
||
public function checksumProvider() | ||
{ | ||
return array( | ||
array('143525744', '4'), | ||
array('500109285', '3'), | ||
array('500109285', '3'), | ||
array('500109285', '3'), | ||
array('027615723', '1') | ||
); | ||
} | ||
|
||
/** | ||
* @dataProvider checksumProvider | ||
*/ | ||
public function testChecksum($inn, $checksum) | ||
{ | ||
$this->assertEquals($checksum, Inn::checksum($inn), $inn); | ||
} | ||
|
||
public function validatorProvider() | ||
{ | ||
return array( | ||
array('5902179757', true), | ||
array('5408294405', true), | ||
array('2724164617', true), | ||
array('0726000515', true), | ||
array('6312123552', true), | ||
|
||
array('1111111111', false), | ||
array('0123456789', false), | ||
); | ||
} | ||
|
||
/** | ||
* @dataProvider validatorProvider | ||
*/ | ||
public function testIsValid($inn, $isValid) | ||
{ | ||
$this->assertEquals($isValid, Inn::isValid($inn), $inn); | ||
} | ||
} |
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,36 @@ | ||
<?php | ||
|
||
namespace Faker\Test\Provider\ru_RU; | ||
|
||
use Faker\Generator; | ||
use Faker\Provider\ru_RU\Company; | ||
|
||
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 testINN() | ||
{ | ||
$this->assertRegExp('/^[0-9]{10}$/', $this->faker->inn); | ||
$this->assertEquals("77", substr($this->faker->inn("77"), 0, 2)); | ||
$this->assertEquals("02", substr($this->faker->inn(2), 0, 2)); | ||
} | ||
|
||
public function testKPP() | ||
{ | ||
$this->assertRegExp('/^[0-9]{9}$/', $this->faker->kpp); | ||
$this->assertEquals("01001", substr($this->faker->kpp, -5, 5)); | ||
$inn = $this->faker->inn; | ||
$this->assertEquals(substr($inn, 0, 4), substr($this->faker->kpp($inn), 0, 4)); | ||
} | ||
} |