Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NFe Access Key Validator #138

Closed
wants to merge 10 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,7 @@ Reference
* v::phone()
* v::sf()
* v::zend()
* v::nfe()

### Alphabetically

Expand Down Expand Up @@ -538,6 +539,10 @@ See also:
* v::cpf() - Validates the Brazillian CPF number.
* v::cnh() - Validates the Brazillian driver's license.

#### v::nfe()

Validates the access key of the Brazilian electronic invoice.

#### v::consonants() *(deprecated)*

Validates strings that contain only consonants. It's now deprecated, consonant should be used
Expand Down
100 changes: 0 additions & 100 deletions composer.json

This file was deleted.

15 changes: 15 additions & 0 deletions library/Respect/Validation/Exceptions/NFeException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php
namespace Respect\Validation\Exceptions;

class NFeException extends ValidationException
{
public static $defaultTemplates = array(
self::MODE_DEFAULT => array(
self::STANDARD => '{{name}} must be a valid NFe access key',
),
self::MODE_NEGATIVE => array(
self::STANDARD => '{{name}} must not be a valid NFe access key',
)
);
}

35 changes: 35 additions & 0 deletions library/Respect/Validation/Rules/NFe.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Respect\Validation\Rules;

class NFe extends AbstractRule
{

/**
* (MICV 4.01 - NT2009.006 #85)
* @param string $aK access key
* @return boolean
*/
public function validate($aK)
{
if (strlen($aK) !== 44)
return false;

$w = [];

for ($i = 0, $z = 5, $m = 43; $i <= $m; $i++) {
$z = ($i < $m) ? ($z - 1) == 1 ? 9 : ($z - 1) : 0;
$w [] = $z;
}

for ($i = 0, $s = 0, $k = 44; $i < $k; ++$i)
$s += $aK{ $i } * $w[ $i ];

$s -= (11 * floor($s / 11));
$v = ($s == 0 || $s == 1) ? 0 : (11 - $s);

return $v == $aK{ 43 };
}

}

81 changes: 81 additions & 0 deletions tests/library/Respect/Validation/Rules/NFeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php
namespace Respect\Validation\Rules;

class NFeTest extends \PHPUnit_Framework_TestCase
{
protected $nfeValidator;

protected function setUp()
{
$this->nfeValidator = new NFe();
}

/**
* @dataProvider validAccessKeyProvider
*/
public function testValidAccessKey($aK)
{
$this->assertTrue($this->nfeValidator->assert($aK));
$this->assertTrue($this->nfeValidator->__invoke($aK));
$this->assertTrue($this->nfeValidator->check($aK));
}

/**
* @dataProvider invalidAccessKeyProvider
* @expectedException Respect\Validation\Exceptions\NFeException
*/
public function testInvalidAccessKey($aK)
{
$this->assertFalse($this->nfeValidator->assert($aK));
}

/**
* @dataProvider invalidAccessKeyLengthProvider
* @expectedException Respect\Validation\Exceptions\NFeException
*/
public function testInvalidLength($aK)
{
$this->assertFalse($this->nfeValidator->assert($aK));
}

public function validAccessKeyProvider()
{
// confidential
// this one is provided from taxpayer integration manual.
return array(
array('52060433009911002506550120000007800267301615')
);
}

public function invalidAccessKeyProvider()
{
return array(
array('31841136830118868211870485416765268625116906'),
array('21470801245862435081451225624565260861852679'),
array('45644318091447671194616059650873352394885852'),
array('17214281716057582143671174314277906696193888'),
array('56017280182977836779696364362142515138726654'),
array('90157126614010548506235171976891004177042525'),
array('78457064241662300187501877048374851128754067'),
array('39950148079977322431982386613620895568235903'),
array('90820939577654114875253907311677136672761216')
);
}

public function invalidAccessKeyLengthProvider()
{
return array(
array('11145573386990252067204852181837301'),
array('6209433147444876'),
array('00745996227609395385255721262102'),
array('58215798856653'),
array('24149625439084262707824706699374326'),
array('163907274335'),
array('67229454773008929675906894698'),
array('5858836670181917762140106857095788313119136'),
array('6098412281885524361833754087461339281130'),
array('9025299113310221')
);
}
}