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 #545 from igorsantos07/improved-pt-cherry-picked
Improving pt_BR Providers
- Loading branch information
Showing
7 changed files
with
314 additions
and
24 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
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
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,35 @@ | ||
<?php | ||
|
||
namespace Faker\Provider\pt_BR; | ||
|
||
/** | ||
* Calculates one MOD 11 check digit based on customary Brazilian algorithms. | ||
* @link http://en.wikipedia.org/wiki/Check_digit | ||
* @link http://pt.wikipedia.org/wiki/CNPJ#Algoritmo_de_Valida.C3.A7.C3.A3o | ||
* @link http://en.wikipedia.org/wiki/Cadastro_de_Pessoas_F%C3%ADsicas#Validation | ||
* | ||
* @param string|integer $numbers Numbers on which generate the check digit | ||
* @return integer | ||
*/ | ||
function check_digit($numbers) | ||
{ | ||
$length = strlen($numbers); | ||
$second_algorithm = $length >= 12; | ||
$verifier = 0; | ||
|
||
for ($i = 1; $i <= $length; $i++) { | ||
if (!$second_algorithm) { | ||
$multiplier = $i+1; | ||
} else { | ||
$multiplier = ($i >= 9)? $i-7 : $i+1; | ||
} | ||
$verifier += $numbers[$length-$i] * $multiplier; | ||
} | ||
|
||
$verifier = 11 - ($verifier % 11); | ||
if ($verifier >= 10) { | ||
$verifier = 0; | ||
} | ||
|
||
return $verifier; | ||
} |
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,25 @@ | ||
<?php | ||
|
||
namespace Faker\Test\Provider\pt_BR; | ||
|
||
use Faker\Generator; | ||
use Faker\Provider\pt_BR\Company; | ||
|
||
class CompanyTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
|
||
public function setUp() | ||
{ | ||
$faker = new Generator(); | ||
$faker->addProvider(new Company($faker)); | ||
$this->faker = $faker; | ||
} | ||
|
||
public function testCnpjFormatIsValid() | ||
{ | ||
$cnpj = $this->faker->cnpj(false); | ||
$this->assertRegExp('/\d{8}\d{4}\d{2}/', $cnpj); | ||
$cnpj = $this->faker->cnpj(true); | ||
$this->assertRegExp('/\d{2}\.\d{3}\.\d{3}\/\d{4}-\d{2}/', $cnpj); | ||
} | ||
} |
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,33 @@ | ||
<?php | ||
|
||
namespace Faker\Test\Provider\pt_BR; | ||
|
||
use Faker\Generator; | ||
use Faker\Provider\pt_BR\Person; | ||
|
||
class PersonTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
|
||
public function setUp() | ||
{ | ||
$faker = new Generator(); | ||
$faker->addProvider(new Person($faker)); | ||
$this->faker = $faker; | ||
} | ||
|
||
public function testCpfFormatIsValid() | ||
{ | ||
$cpf = $this->faker->cpf(false); | ||
$this->assertRegExp('/\d{9}\d{2}/', $cpf); | ||
$cpf = $this->faker->cpf(true); | ||
$this->assertRegExp('/\d{3}\.\d{3}\.\d{3}-\d{2}/', $cpf); | ||
} | ||
|
||
public function testRgFormatIsValid() | ||
{ | ||
$rg = $this->faker->rg(false); | ||
$this->assertRegExp('/\d{8}\d/', $rg); | ||
$rg = $this->faker->rg(true); | ||
$this->assertRegExp('/\d{2}\.\d{3}\.\d{3}-[0-9X]/', $rg); | ||
} | ||
} |