mirror of
https://github.com/Respect/Validation.git
synced 2024-06-04 06:42:22 +02:00
Added CreditCard Validator
This commit is contained in:
parent
5bf3f19653
commit
c4119fab82
|
@ -0,0 +1,50 @@
|
|||
<?php
|
||||
|
||||
namespace Respect\Validation\Exceptions;
|
||||
|
||||
class CreditCardException extends ValidationException
|
||||
{
|
||||
|
||||
public static $defaultTemplates = array(
|
||||
self::MODE_DEFAULT => array(
|
||||
self::STANDARD => '{{name}} must be a valid Credit Card number',
|
||||
),
|
||||
self::MODE_NEGATIVE => array(
|
||||
self::STANDARD => '{{name}} must not be a valid Credit Card number',
|
||||
)
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* LICENSE
|
||||
*
|
||||
* Copyright (c) 2011, Jean Pimentel.
|
||||
* 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.
|
||||
*
|
||||
*/
|
71
library/Respect/Validation/Rules/CreditCard.php
Normal file
71
library/Respect/Validation/Rules/CreditCard.php
Normal file
|
@ -0,0 +1,71 @@
|
|||
<?php
|
||||
|
||||
namespace Respect\Validation\Rules;
|
||||
|
||||
class CreditCard extends AbstractRule
|
||||
{
|
||||
|
||||
public function validate($input)
|
||||
{
|
||||
$input = preg_replace('([^0-9])', '', $input);
|
||||
if(!empty($input))
|
||||
return $this->verifyMod10($input);
|
||||
return false;
|
||||
}
|
||||
|
||||
private function verifyMod10($input)
|
||||
{
|
||||
$sum = 0;
|
||||
$input = strrev($input);
|
||||
for ($i = 0; $i < strlen($input); $i++)
|
||||
{
|
||||
$current = substr($input, $i, 1);
|
||||
if ($i % 2 == 1)
|
||||
{
|
||||
$current *= 2;
|
||||
if ($current > 9)
|
||||
{
|
||||
$firstDigit = $current % 10;
|
||||
$secondDigit = ($current - $firstDigit) / 10;
|
||||
$current = $firstDigit + $secondDigit;
|
||||
}
|
||||
}
|
||||
$sum += $current;
|
||||
}
|
||||
return ($sum % 10 == 0);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* LICENSE
|
||||
*
|
||||
* Copyright (c) 2011, Jean Pimentel.
|
||||
* 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.
|
||||
*
|
||||
*/
|
59
tests/library/Respect/Validation/Rules/CreditCardTest.php
Normal file
59
tests/library/Respect/Validation/Rules/CreditCardTest.php
Normal file
|
@ -0,0 +1,59 @@
|
|||
<?php
|
||||
|
||||
namespace Respect\Validation\Rules;
|
||||
|
||||
class CreditCardTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
|
||||
protected $creditCardValidator;
|
||||
|
||||
protected function setUp()
|
||||
{
|
||||
$this->creditCardValidator = new CreditCard;
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider providerForCreditCard
|
||||
*
|
||||
*/
|
||||
public function test_valid_creditCards_should_return_True($input)
|
||||
{
|
||||
$this->assertTrue($this->creditCardValidator->validate($input));
|
||||
$this->assertTrue($this->creditCardValidator->assert($input));
|
||||
$this->assertTrue($this->creditCardValidator->check($input));
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider providerForNotCreditCard
|
||||
* @expectedException Respect\Validation\Exceptions\CreditCardException
|
||||
*/
|
||||
public function test_invalid_creditCards_should_throw_CreditCardException($input)
|
||||
{
|
||||
$this->assertFalse($this->creditCardValidator->validate($input));
|
||||
$this->assertFalse($this->creditCardValidator->assert($input));
|
||||
}
|
||||
|
||||
public function providerForCreditCard()
|
||||
{
|
||||
return array(
|
||||
array('5376 7473 9720 8720'), // MasterCard
|
||||
array('4024.0071.5336.1885'), // Visa 16
|
||||
array('4024 007 193 879'), // Visa 13
|
||||
array('340-3161-9380-9364'), // AmericanExpress
|
||||
array('30351042633884'), // Dinners
|
||||
);
|
||||
}
|
||||
|
||||
public function providerForNotCreditCard()
|
||||
{
|
||||
return array(
|
||||
array(null),
|
||||
array('it isnt my credit card number'),
|
||||
array('&stR@ng3|) (|-|@r$'),
|
||||
array(''),
|
||||
array('1234 1234 1234 1234'),
|
||||
array('1234.1234.1234.1234'),
|
||||
);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in a new issue