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
Improving pt_BR Providers #545
Merged
fzaninotto
merged 18 commits into
fzaninotto:master
from
igorsantos07:improved-pt-cherry-picked
May 19, 2015
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
6df5498
Improving code and adding documents
igorsantos07 94e2a43
More robust brazilian phone generator
igorsantos07 fa21d04
Adding documentation about pt_BR providers
igorsantos07 8cae5bb
Including future ninth digit area codes
igorsantos07 df641cd
Improving readability of Brazilian document generators and adding bas…
igorsantos07 cf9daf3
Several fixes to brazilian phone generator
igorsantos07 c874c02
Updating brazilian generators doc
igorsantos07 5322f13
Small code style fixes
igorsantos07 1085695
Small code style fixes
igorsantos07 615483d
Brazilian PhoneNumber::areaCode() should be static
igorsantos07 9518fe8
Adding retrocompat with PHP 5.3 on dynamic static calls
igorsantos07 4aaf031
Fixing file permissions
igorsantos07 bf30f7b
Adding explain links about brazilian document algorithms
igorsantos07 13daec1
Improving pt_BR examples
igorsantos07 05bae3c
Merge branch 'improved-pt-cherry-picked' of github.com:igorsantos07/F…
igorsantos07 1505f89
Moving pt_BR CNPJ generator to Company provider
igorsantos07 5454bee
Adding pt_BR RG and improving docs
igorsantos07 6aeea1f
removing debug statements
igorsantos07 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,8 @@ | |
|
||
namespace Faker\Provider\pt_BR; | ||
|
||
require_once "check_digit.php"; | ||
|
||
class Person extends \Faker\Provider\Person | ||
{ | ||
protected static $maleNameFormats = array( | ||
|
@@ -90,7 +92,7 @@ class Person extends \Faker\Provider\Person | |
|
||
protected static $titleFemale = array('Sra.', 'Srta.', 'Dr.',); | ||
|
||
private static $suffix = array('Filho', 'Neto', 'Sobrinho', 'Jr.'); | ||
protected static $suffix = array('Filho', 'Neto', 'Sobrinho', 'Jr.'); | ||
|
||
/** | ||
* @example 'Jr.' | ||
|
@@ -99,4 +101,33 @@ public static function suffix() | |
{ | ||
return static::randomElement(static::$suffix); | ||
} | ||
|
||
/** | ||
* A random CPF number. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you link to a definition of the format, e.g. in Wikipedia? Same for cnpj. |
||
* @link http://en.wikipedia.org/wiki/Cadastro_de_Pessoas_F%C3%ADsicas | ||
* @param bool $formatted If the number should have dots/dashes or not. | ||
* @return string | ||
*/ | ||
public function cpf($formatted = true) | ||
{ | ||
$n = $this->generator->numerify('#########'); | ||
$n .= check_digit($n); | ||
$n .= check_digit($n); | ||
|
||
return $formatted? vsprintf('%d%d%d.%d%d%d.%d%d%d-%d%d', str_split($n)) : $n; | ||
} | ||
|
||
/** | ||
* A random RG number, following Sao Paulo state's rules. | ||
* @link http://pt.wikipedia.org/wiki/C%C3%A9dula_de_identidade | ||
* @param bool $formatted If the number should have dots/dashes or not. | ||
* @return string | ||
*/ | ||
public function rg($formatted = true) | ||
{ | ||
$n = $this->generator->numerify('########'); | ||
$n .= check_digit($n); | ||
|
||
return $formatted? vsprintf('%d%d.%d%d%d.%d%d%d-%s', str_split($n)) : $n; | ||
} | ||
} |
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); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The file permissions have changed, please revert to 644.