mirror of
https://github.com/Respect/Validation.git
synced 2024-06-07 16:22:16 +02:00
Updated CNH validator with Exceptions
Created new Exception for CNH Validator
This commit is contained in:
parent
fa298ffda6
commit
eb519fdc55
17
library/Respect/Validation/Exceptions/CnhException.php
Normal file
17
library/Respect/Validation/Exceptions/CnhException.php
Normal file
|
@ -0,0 +1,17 @@
|
|||
<?php
|
||||
|
||||
namespace Respect\Validation\Exceptions;
|
||||
|
||||
class CnhException extends ValidationException
|
||||
{
|
||||
|
||||
public static $defaultTemplates = array(
|
||||
self::MODE_DEFAULT => array(
|
||||
self::STANDARD => '{{name}} must be a valid CNH number',
|
||||
),
|
||||
self::MODE_NEGATIVE => array(
|
||||
self::STANDARD => '{{name}} must not be a valid CNH number',
|
||||
)
|
||||
);
|
||||
|
||||
}
|
|
@ -22,10 +22,9 @@ class Cnh extends AbstractRule
|
|||
|
||||
for ( $i = 0 , $j = 1 , $v = 0 ; $i < 9 ; ++$i , ++$j ) {
|
||||
$v += (int) $input[$i] * $j;
|
||||
|
||||
$vl2 = ( $x = ( $v % 11 ) ) >= 10 ? 0 : $x - $dsc;
|
||||
$ret = sprintf( '%d%d' , $vl1 , $vl2 ) == substr( $input , -2 );
|
||||
}
|
||||
$vl2 = ( $x = ( $v % 11 ) ) >= 10 ? 0 : $x - $dsc;
|
||||
$ret = sprintf( '%d%d' , $vl1 , $vl2 ) == substr( $input , -2 );
|
||||
}
|
||||
return $ret;
|
||||
}
|
||||
|
|
|
@ -15,34 +15,37 @@ class CnhTest extends \PHPUnit_Framework_TestCase {
|
|||
*/
|
||||
public function test_valid_cnh($cnh)
|
||||
{
|
||||
$expectedValid = $this->cnhValidator->validate($cnh);
|
||||
$expectedValid = $this->cnhValidator->assert($cnh);
|
||||
$this->assertTrue($expectedValid);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider invalidCnhProvider
|
||||
* @expectedException Respect\Validation\Exceptions\CnhException
|
||||
*/
|
||||
public function test_invalid_cnh($cnh)
|
||||
{
|
||||
$expectedInvalid = $this->cnhValidator->validate($cnh);
|
||||
$expectedInvalid = $this->cnhValidator->assert($cnh);
|
||||
$this->assertFalse($expectedInvalid);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider notIntegerCnhProvider
|
||||
* @expectedException Respect\Validation\Exceptions\CnhException
|
||||
*/
|
||||
public function test_not_integer_cnh($cnh)
|
||||
{
|
||||
$expectedInvalid = $this->cnhValidator->validate($cnh);
|
||||
$expectedInvalid = $this->cnhValidator->assert($cnh);
|
||||
$this->assertFalse($expectedInvalid);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider invalidCnhLenghtProvider
|
||||
* @expectedException Respect\Validation\Exceptions\CnhException
|
||||
*/
|
||||
public function test_invalid_length_cnh($cnh)
|
||||
{
|
||||
$expectedInvalid = $this->cnhValidator->validate($cnh);
|
||||
$expectedInvalid = $this->cnhValidator->assert($cnh);
|
||||
$this->assertFalse($expectedInvalid);
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue