fixed indentation and php5.3 array syntax

This commit is contained in:
Andrew Willis 2014-01-10 06:54:10 +00:00
parent efba2d8cf1
commit f26a1f55b4
2 changed files with 33 additions and 33 deletions

View File

@ -509,29 +509,29 @@ class Validator
* @return bool
*/
$numberIsValid = function () use ($value) {
$number = preg_replace('/[^0-9]+/', '', $value);
$sum = 0;
$number = preg_replace('/[^0-9]+/', '', $value);
$sum = 0;
$strlen = strlen($number);
if ($strlen < 13) {
return false;
}
for ($i = 0; $i < $strlen; $i++) {
$digit = (int) substr($number, $strlen - $i - 1, 1);
if ($i % 2 == 1) {
$sub_total = $digit * 2;
if ($sub_total > 9) {
$sub_total = ($sub_total - 10) + 1;
}
} else {
$sub_total = $digit;
$strlen = strlen($number);
if ($strlen < 13) {
return false;
}
$sum += $sub_total;
}
if ($sum > 0 && $sum % 10 == 0) {
return true;
}
return false;
for ($i = 0; $i < $strlen; $i++) {
$digit = (int) substr($number, $strlen - $i - 1, 1);
if ($i % 2 == 1) {
$sub_total = $digit * 2;
if ($sub_total > 9) {
$sub_total = ($sub_total - 10) + 1;
}
} else {
$sub_total = $digit;
}
$sum += $sub_total;
}
if ($sum > 0 && $sum % 10 == 0) {
return true;
}
return false;
};
if ($numberIsValid()) {

View File

@ -657,11 +657,11 @@ class ValidateTest extends BaseTestCase
public function testCreditCardValid()
{
$visa = [4539511619543489, 4532949059629052, 4024007171194938, 4929646403373269, 4539135861690622];
$mastercard = [5162057048081965, 5382687859049349, 5484388880142230, 5464941521226434, 5473481232685965];
$amex = [371442067262027, 340743030537918, 345509167493596, 343665795576848, 346087552944316];
$dinersclub = [30363194756249, 30160097740704, 38186521192206, 38977384214552, 38563220301454];
$discover = [6011712400392605, 6011536340491809, 6011785775263015, 6011984124619056, 6011320958064251];
$visa = array(4539511619543489, 4532949059629052, 4024007171194938, 4929646403373269, 4539135861690622);
$mastercard = array(5162057048081965, 5382687859049349, 5484388880142230, 5464941521226434, 5473481232685965);
$amex = array(371442067262027, 340743030537918, 345509167493596, 343665795576848, 346087552944316);
$dinersclub = array(30363194756249, 30160097740704, 38186521192206, 38977384214552, 38563220301454);
$discover = array(6011712400392605, 6011536340491809, 6011785775263015, 6011984124619056, 6011320958064251);
foreach (compact('visa', 'mastercard', 'amex', 'dinersclub', 'discover') as $type => $numbers) {
foreach($numbers as $number) {
@ -674,18 +674,18 @@ class ValidateTest extends BaseTestCase
$this->assertTrue($v->validate());
$v->rule('creditCard', 'test', $type, [$type, 'mastercard', 'visa']);
$this->assertTrue($v->validate());
unset($d);
unset($v);
}
}
}
public function testcreditCardInvalid()
{
$visa = [3539511619543489, 3532949059629052, 3024007171194938, 3929646403373269, 3539135861690622];
$mastercard = [4162057048081965, 4382687859049349, 4484388880142230, 4464941521226434, 4473481232685965];
$amex = [271442067262027, 240743030537918, 245509167493596, 243665795576848, 246087552944316];
$dinersclub = [20363194756249, 20160097740704, 28186521192206, 28977384214552, 28563220301454];
$discover = [5011712400392605, 5011536340491809, 5011785775263015, 5011984124619056, 5011320958064251];
$visa = array(3539511619543489, 3532949059629052, 3024007171194938, 3929646403373269, 3539135861690622);
$mastercard = array(4162057048081965, 4382687859049349, 4484388880142230, 4464941521226434, 4473481232685965);
$amex = array(271442067262027, 240743030537918, 245509167493596, 243665795576848, 246087552944316);
$dinersclub = array(20363194756249, 20160097740704, 28186521192206, 28977384214552, 28563220301454);
$discover = array(5011712400392605, 5011536340491809, 5011785775263015, 5011984124619056, 5011320958064251);
foreach (compact('visa', 'mastercard', 'amex', 'dinersclub', 'discover') as $type => $numbers) {
foreach($numbers as $number) {
@ -702,7 +702,7 @@ class ValidateTest extends BaseTestCase
$this->assertFalse($v->validate());
$v->rule('creditCard', 'test', 'invalidCardName', ['invalidCardName', 'mastercard', 'visa']);
$this->assertFalse($v->validate());
unset($d);
unset($v);
}
}
}