- Add CPF Validation

- Fix coding standars on JSON Validation
This commit is contained in:
Jair Henrique 2011-09-03 19:20:02 -03:00
parent 4f17ae1073
commit 9aafcaceb0
4 changed files with 165 additions and 8 deletions

View file

@ -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.
*
*/

View file

@ -0,0 +1,86 @@
<?php
namespace Respect\Validation\Rules;
use Respect\Validation\Rules\Length;
class CPF extends AbstractRule {
public $cpf;
public function __construct($cpf=null) {
$this->cpf = $cpf;
}
public function validate($input) {
$input = $this->clean($input);
if ($this->isSequenceOfNumber($input))
return false;
if ($this->hasInvalidLength($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);
}
}

View file

@ -2,23 +2,21 @@
namespace Respect\Validation\Rules;
class JSON extends AbstractRule
{
class JSON extends AbstractRule {
public $json;
public function __construct($json=null)
public function __construct($json=null)
{
$this->json = $json;
}
public function validate($input)
public function validate($input)
{
if(json_decode($input)) {
if (json_decode($input))
return true;
} else {
return false;
}
return false;
}
}

View file

@ -0,0 +1,23 @@
<?php
namespace Respect\Validation\Rules;
class CPFTest extends \PHPUnit_Framework_TestCase
{
public function testValidCPF()
{
$cpf = new CPF('342.444.198-88');
$this->assertTrue($cpf->assert($cpf->cpf));
}
/**
* @expectedException Respect\Validation\Exceptions\CPFException
*/
public function testInvalidCPF()
{
$cpf = new CPF('111.111.111-51');
$this->assertFalse($cpf->assert($cpf->cpf));
}
}