Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Validator: Fixed a condition where 'format' wasn't being properly app…

…lied to validators with

multiple format options. It was effectively always 'any' regardless of what it was set to.
    1: Fixing a condition where a null format passed to __callStatic would not be overridden with 'any'
    2: Removed the extract statement for readability.
    3: Fixed $options['all'] being dependent on $format being 'all' as opposed to it's actual
    value of 'any'
    4: Removed superfluous key extraction then rematching for the loop - foreach now just
    iterates over $rules instead of extracting the keys then piecing it back together.
    5: Added a continue statement for elements not in the formats array when $options['all'] is
    false.
    6: Removed format assignment, not necessary.

ValidatorTest: Added some assertFalse tests for format when applied to validator arrays with
formats. Previously only assertTrue was run, this led to a problem not being caught where fiormat
 wasn't being applied, any format could be readily swapped and it would still have validated as
 true.
  • Loading branch information...
commit 88a2ecc1c6f9c6b9a107e8d82db01572e1c3cce0 1 parent edaae76
@Howard3 Howard3 authored
Showing with 21 additions and 9 deletions.
  1. +13 −0 tests/cases/util/ValidatorTest.php
  2. +8 −9 util/Validator.php
View
13 tests/cases/util/ValidatorTest.php
@@ -434,6 +434,7 @@ public function testCreditCardValidation() {
$this->assertTrue(Validator::isCreditCard('348498616319346', 'amex', array(
'deep' => true
)));
+ $this->assertFalse(Validator::isCreditCard('5610376649499352', 'amex'));
/**
* BankCard
@@ -448,6 +449,7 @@ public function testCreditCardValidation() {
$this->assertTrue(Validator::isCreditCard('5610139705753702', 'bankcard'));
$this->assertTrue(Validator::isCreditCard('5602226032150551', 'bankcard'));
$this->assertTrue(Validator::isCreditCard('5602223993735777', 'bankcard'));
+ $this->assertFalse(Validator::isCreditCard('30155483651028', 'bankcard'));
/**
* Diners Club 14
@@ -500,6 +502,7 @@ public function testCreditCardValidation() {
$this->assertTrue(Validator::isCreditCard('5577265786122391', 'diners'));
$this->assertTrue(Validator::isCreditCard('5534061404676989', 'diners'));
$this->assertTrue(Validator::isCreditCard('5545313588374502', 'diners'));
+ $this->assertFalse(Validator::isCreditCard('6011802876467237', 'diners'));
/**
* Discover
@@ -514,6 +517,7 @@ public function testCreditCardValidation() {
$this->assertTrue(Validator::isCreditCard('6509735979634270', 'disc'));
$this->assertTrue(Validator::isCreditCard('6011422366775856', 'disc'));
$this->assertTrue(Validator::isCreditCard('6500976374623323', 'disc'));
+ $this->assertFalse(Validator::isCreditCard('201496944158937', 'disc'));
/**
* enRoute
@@ -528,6 +532,7 @@ public function testCreditCardValidation() {
$this->assertTrue(Validator::isCreditCard('201402662758866', 'enroute'));
$this->assertTrue(Validator::isCreditCard('214981579370225', 'enroute'));
$this->assertTrue(Validator::isCreditCard('201447595859877', 'enroute'));
+ $this->assertFalse(Validator::isCreditCard('210034762247893', 'enroute'));
/**
* JCB 15 digit
@@ -576,6 +581,7 @@ public function testCreditCardValidation() {
$this->assertTrue(Validator::isCreditCard('3528274546125962', 'jcb'));
$this->assertTrue(Validator::isCreditCard('3528890967705733', 'jcb'));
$this->assertTrue(Validator::isCreditCard('3337198811307545', 'jcb'));
+ $this->assertFalse(Validator::isCreditCard('5020147409985219', 'jcb'));
/**
* Maestro (debit card)
@@ -590,6 +596,7 @@ public function testCreditCardValidation() {
$this->assertTrue(Validator::isCreditCard('5020565359718977', 'maestro'));
$this->assertTrue(Validator::isCreditCard('6339931536544062', 'maestro'));
$this->assertTrue(Validator::isCreditCard('6465028615704406', 'maestro'));
+ $this->assertFalse(Validator::isCreditCard('5580424361774366', 'maestro'));
/**
* MasterCard
@@ -619,6 +626,7 @@ public function testCreditCardValidation() {
$this->assertTrue(Validator::isCreditCard('5467639122779531', 'mc'));
$this->assertTrue(Validator::isCreditCard('5297350261550024', 'mc'));
$this->assertTrue(Validator::isCreditCard('5162739131368058', 'mc'));
+ $this->assertFalse(Validator::isCreditCard('6767432107064987', 'mc'));
/**
* Solo 16
@@ -661,6 +669,7 @@ public function testCreditCardValidation() {
$this->assertTrue(Validator::isCreditCard('6334933119080706440', 'solo'));
$this->assertTrue(Validator::isCreditCard('6334647959628261714', 'solo'));
$this->assertTrue(Validator::isCreditCard('6334527312384101382', 'solo'));
+ $this->assertFalse(Validator::isCreditCard('5641829171515733', 'solo'));
/**
* Switch 16
@@ -793,6 +802,7 @@ public function testCreditCardValidation() {
$this->assertTrue(Validator::isCreditCard('4936196077254804290', 'switch'));
$this->assertTrue(Validator::isCreditCard('6759558831206830183', 'switch'));
$this->assertTrue(Validator::isCreditCard('5641827998830403137', 'switch'));
+ $this->assertFalse(Validator::isCreditCard('4024007174754', 'switch'));
/**
* Visa 13 digit
@@ -891,6 +901,7 @@ public function testCreditCardValidation() {
$this->assertTrue(Validator::isCreditCard('4916845885268360', 'visa'));
$this->assertTrue(Validator::isCreditCard('4394514669078434', 'visa'));
$this->assertTrue(Validator::isCreditCard('4485611378115042', 'visa'));
+ $this->assertFalse(Validator::isCreditCard('869940697287073', 'visa'));
/**
* Visa Electron
@@ -910,6 +921,7 @@ public function testCreditCardValidation() {
$this->assertTrue(Validator::isCreditCard('4175009797419290', 'electron'));
$this->assertTrue(Validator::isCreditCard('4175005028142917', 'electron'));
$this->assertTrue(Validator::isCreditCard('4913940802385364', 'electron'));
+ $this->assertFalse(Validator::isCreditCard('869940697287073', 'electron'));
/**
* Voyager
@@ -919,6 +931,7 @@ public function testCreditCardValidation() {
$this->assertTrue(Validator::isCreditCard('869958670174621', 'voyager'));
$this->assertTrue(Validator::isCreditCard('869921250068209', 'voyager'));
$this->assertTrue(Validator::isCreditCard('869972521242198', 'voyager'));
+ $this->assertFalse(Validator::isCreditCard('370482756063980', 'voyager'));
$this->assertTrue(Validator::isLuhn('869972521242198'));
$this->assertFalse(Validator::isLuhn(false));
View
17 util/Validator.php
@@ -367,7 +367,7 @@ public static function __callStatic($method, $args = array()) {
if (!isset($args[0])) {
return false;
}
- $args += array(1 => 'any', 2 => array());
+ $args = array_filter($args) + array(0 => $args[0], 1 => 'any', 2 => array());
$rule = preg_replace("/^is([A-Z][A-Za-z0-9]+)$/", '$1', $method);
$rule[0] = strtolower($rule[0]);
return static::rule($rule, $args[0], $args[1], $args[2]);
@@ -606,21 +606,20 @@ public static function rules($name = null) {
*/
protected static function _checkFormats($rules) {
return function($self, $params, $chain) use ($rules) {
- extract($params);
+ $value = $params['value'];
+ $format = $params['format'];
+ $options = $params['options'];
+
$defaults = array('all' => true);
$options += $defaults;
$formats = (array) $format;
+ $options['all'] = ($format == 'any');
- $ruleIndexes = array_keys($rules);
- $options['all'] = ($format == 'all');
-
- foreach ($ruleIndexes as $index) {
- if (!isset($rules[$index])) {
+ foreach ($rules as $index => $check) {
+ if (!$options['all'] && !(in_array($index, $formats) || isset($formats[$index]))) {
continue;
}
- $check = $rules[$index];
- $format = isset($formats[$index]) ? $formats[$index] : null;
$regexPassed = (is_string($check) && preg_match($check, $value));
$closurePassed = (is_object($check) && $check($value, $format, $options));
Please sign in to comment.
Something went wrong with that request. Please try again.