Adds NFeAccessKey validation rule.

Relevant just to Brazil, this rule validates NFe access key.
This commit is contained in:
Andrey Knupp Vital 2013-11-12 00:14:38 -02:00 committed by Augusto Pascutti
parent d1a2f18a80
commit 917176222c
4 changed files with 142 additions and 0 deletions

View file

@ -304,6 +304,7 @@ Reference
* v::phone()
* v::sf()
* v::zend()
* v::nfeAccessKey()
### Alphabetically
@ -540,6 +541,10 @@ See also:
* v::cpf() - Validates the Brazillian CPF number.
* v::cnh() - Validates the Brazillian driver's license.
#### v::nfeAccessKey()
Validates the access key of the Brazilian electronic invoice (NFe).
#### v::consonants() *(deprecated)*
Validates strings that contain only consonants. It's now deprecated, consonant should be used

View file

@ -0,0 +1,15 @@
<?php
namespace Respect\Validation\Exceptions;
class NfeAccessKeyException 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',
)
);
}

View file

@ -0,0 +1,42 @@
<?php
namespace Respect\Validation\Rules;
/**
* Rule restrict to Brasil.
*
* Valida chave de acesso de NFe.
* Mais especificamente, relacionada ao DANFE.
*/
class NfeAccessKey extends AbstractRule
{
/**
* @see Manual de Integração do Contribuinte v4.0.1 (http://www.nfe.fazenda.gov.br)
* @param string $aK access key
* @return boolean
*/
public function validate($aK)
{
if (strlen($aK) !== 44) {
return false;
}
$w = array();
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 };
}
}

View file

@ -0,0 +1,80 @@
<?php
namespace Respect\Validation\Rules;
class NfeAccessKeyTest extends \PHPUnit_Framework_TestCase
{
protected $nfeValidator;
protected function setUp()
{
$this->nfeValidator = new NfeAccessKey();
}
/**
* @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\NfeAccessKeyException
*/
public function testInvalidAccessKey($aK)
{
$this->assertFalse($this->nfeValidator->assert($aK));
}
/**
* @dataProvider invalidAccessKeyLengthProvider
* @expectedException Respect\Validation\Exceptions\NfeAccessKeyException
*/
public function testInvalidLengthCnh($aK)
{
$this->assertFalse($this->nfeValidator->assert($aK));
}
public function validAccessKeyProvider()
{
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')
);
}
}