Permalink
Browse files

Merge pull request Respect#14 from jairhenrique/CPFValidate

Validação de CPF
  • Loading branch information...
alganet committed Sep 6, 2011
2 parents 4f17ae1 + 65cc12d commit ca984a6485f526cbabd9a84e161bf26132ebc322
@@ -0,0 +1,50 @@
<?php
namespace Respect\Validation\Exceptions;
class CPFException extends ValidationException
{
public static $defaultTemplates = array(
self::MODE_DEFAULT => array(
self::STANDARD => '{{name}} must be a valid CPF number',
),
self::MODE_NEGATIVE => array(
self::STANDARD => '{{name}} must not be a valid CPF number',
)
);
}
/**
* LICENSE
*
* Copyright (c) 2009-2011, Jair Henrique.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* * Neither the name of Alexandre Gomes Gaigalas nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
@@ -0,0 +1,82 @@
<?php
namespace Respect\Validation\Rules;
use Respect\Validation\Rules\Length;
class CPF extends AbstractRule
{
public function validate($input)
{
$input = $this->clean($input);
if ($this->hasInvalidLength($input))
return false;
if ($this->isSequenceOfNumber($input))
return false;
if ($this->processNumber($input))
return true;
return false;
}
private function processNumber($input)
{
$verify = array('firstDigit' => 0,
'secondDigit' => 0,
);
$multiple = 10;
for ($i = 0; $i < 9; $i++)
$verify['firstDigit'] += ($multiple-- * (int) $input[$i]);
$verify['firstDigit'] = 11 - ($verify['firstDigit'] % 11);
if ($verify['firstDigit'] >= 10)
$verify['firstDigit'] = 0;
$multiple = 11;
for ($i = 0; $i < 9; $i++)
$verify['secondDigit'] += ($multiple-- * (int) $input[$i]);
$verify['secondDigit'] += (2 * $verify['firstDigit']);
$verify['secondDigit'] = 11 - ($verify['secondDigit'] % 11);
if ($verify['secondDigit'] >= 10)
$verify['secondDigit'] = 0;
$digits = substr($input, (strlen($input) - 2), 2);
if (strcmp("{$verify['firstDigit']}{$verify['secondDigit']}", $digits) === 0)
return true;
return false;
}
private function hasInvalidLength($input)
{
$vl = new Length(11,11);
return !$vl->assert($input);
}
private function isSequenceOfNumber($input=null)
{
for ($i = 0; $i <= 9; $i++)
if (strcmp($input, str_pad('', strlen($input), $i)) === 0)
return true;
return false;
}
private function clean($input=null)
{
return preg_replace("/\.|-/", "", $input);
}
}
@@ -2,23 +2,15 @@
namespace Respect\Validation\Rules;
class JSON extends AbstractRule
class JSON extends AbstractRule
{
public $json;
public function __construct($json=null)
public function validate($input)
{
$this->json = $json;
}
public function validate($input)
{
if(json_decode($input)) {
if (json_decode($input))
return true;
} else {
return false;
}
return false;
}
}
@@ -0,0 +1,115 @@
<?php
namespace Respect\Validation\Rules;
class CPFTest extends \PHPUnit_Framework_TestCase {
protected $cpf;
protected function setUp()
{
$this->cpf = new CPF;
}
/**
* @dataProvider providerValidFormattedCPF
*/
public function testValidFormattedCPF($input)
{
$this->assertTrue($this->cpf->assert($input));
}
/**
* @dataProvider providerValidUnformattedCPF
*/
public function testValidUnformattedCPF($input)
{
$this->assertTrue($this->cpf->assert($input));
}
/**
* @dataProvider providerInvalidFormattedCPF
* @expectedException Respect\Validation\Exceptions\CPFException
*/
public function testInvalidFormattedCPF($input)
{
$this->assertFalse($this->cpf->assert($input));
}
/**
* @dataProvider providerInvalidUnformattedCPF
* @expectedException Respect\Validation\Exceptions\CPFException
*/
public function testInvalidUnformattedCPF($input)
{
$this->assertFalse($this->cpf->assert($input));
}
/**
* @dataProvider providerInvalidFormattedAndUnformattedCPFLength
* @expectedException Respect\Validation\Exceptions\LengthException
*/
public function testInvalidFormattedAndUnformattedCPFLength($input)
{
$this->assertFalse($this->cpf->assert($input));
}
public function providerValidFormattedCPF()
{
return array(
array('342.444.198-88'),
array('342.444.198.88'),
array('350.45261819'),
array('693-319-118-40'),
array('3.6.8.8.9.2.5.5.4.8.8')
);
}
public function providerValidUnformattedCPF()
{
return array(
array('11598647644'),
array('86734718697'),
array('86223423284'),
array('24845408333'),
array('95574461102'),
);
}
public function providerInvalidFormattedCPF()
{
return array(
array('000.000.000-00'),
array('111.222.444-05'),
array('999999999.99'),
array('8.8.8.8.8.8.8.8.8.8.8'),
array('693-319-110-40'),
array('698.111-111.00')
);
}
public function providerInvalidUnformattedCPF()
{
return array(
array('11111111111'),
array('22222222222'),
array('12345678900'),
array('99299929384'),
array('84434895894'),
array('44242340000')
);
}
public function providerInvalidFormattedAndUnformattedCPFLength()
{
return array(
array('1'),
array('22'),
array('123'),
array('992999999999929384'),
array('')
);
}
}
@@ -5,18 +5,23 @@
class JSONTest extends \PHPUnit_Framework_TestCase
{
protected $json;
protected function setUp()
{
$this->json = new JSON;
}
public function testValidJSON()
{
$object = new JSON('{"foo": "bar", "number":1}');
$this->assertTrue($object->assert($object->json));
$this->assertTrue($this->json->assert('{"foo": "bar", "number":1}'));
}
/**
* @expectedException Respect\Validation\Exceptions\JSONException
*/
public function testInvalidJSON()
{
$object = new JSON("{foo:bar}");
$this->assertFalse($object->assert($object->json));
$this->assertFalse($this->json->assert("{foo:bar}"));
}
}

0 comments on commit ca984a6

Please sign in to comment.