菜谱项目

PersonTest.php 1.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. <?php
  2. namespace Faker\Test\Provider\pt_PT;
  3. use Faker\Generator;
  4. use Faker\Provider\pt_PT\Person;
  5. class PersonTest extends \PHPUnit_Framework_TestCase
  6. {
  7. public function setUp()
  8. {
  9. $faker = new Generator();
  10. $faker->addProvider(new Person($faker));
  11. $this->faker = $faker;
  12. }
  13. public function testTaxpayerIdentificationNumberIsValid()
  14. {
  15. $tin = $this->faker->taxpayerIdentificationNumber();
  16. $this->assertTrue($this->isValidTin($tin), $tin);
  17. }
  18. /**
  19. *
  20. * @link http://pt.wikipedia.org/wiki/N%C3%BAmero_de_identifica%C3%A7%C3%A3o_fiscal
  21. *
  22. * @param type $tin
  23. *
  24. * @return boolean
  25. */
  26. public static function isValidTin($tin)
  27. {
  28. $regex = '(([1,2,3,5,6,8]{1}[0-9]{8})|((45)|(70)|(71)|(72)|(77)|(79)|(90|(98|(99))))[0-9]{7})';
  29. if (is_null($tin) || !is_numeric($tin) || !strlen($tin) == 9 || preg_match("/$regex/", $tin) !== 1) {
  30. return false;
  31. }
  32. $n = str_split($tin);
  33. // cd - Control Digit
  34. $cd = ($n[0] * 9 + $n[1] * 8 + $n[2] * 7 + $n[3] * 6 + $n[4] * 5 + $n[5] * 4 + $n[6] * 3 + $n[7] * 2) % 11;
  35. if ($cd === 0 || $cd === 1) {
  36. $cd = 0;
  37. } else {
  38. $cd = 11 - $cd;
  39. }
  40. if ($cd === intval($n[8])) {
  41. return true;
  42. }
  43. return false;
  44. }
  45. }