Explorar o código

[ZF-9477] Zend_Validate_CreditCard:

- switched length and type test to increase usability

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@21569 44c647ce-9c0f-0410-b52a-842ac1e357ba
thomas %!s(int64=16) %!d(string=hai) anos
pai
achega
d1d5a14dd4
Modificáronse 2 ficheiros con 26 adicións e 14 borrados
  1. 16 14
      library/Zend/Validate/CreditCard.php
  2. 10 0
      tests/Zend/Validate/CreditCardTest.php

+ 16 - 14
library/Zend/Validate/CreditCard.php

@@ -259,26 +259,28 @@ class Zend_Validate_CreditCard extends Zend_Validate_Abstract
 
         $length = strlen($value);
         $types  = $this->getType();
-        $found  = false;
+        $foundp = false;
+        $foundl = false;
         foreach ($types as $type) {
-            if (in_array($length, $this->_cardLength[$type])) {
-                foreach ($this->_cardType[$type] as $prefix) {
-                    if (substr($value, 0, strlen($prefix)) == $prefix) {
-                        $found = true;
-                        break;
+            foreach ($this->_cardType[$type] as $prefix) {
+                if (substr($value, 0, strlen($prefix)) == $prefix) {
+                    $foundp = true;
+                    if (in_array($length, $this->_cardLength[$type])) {
+                        $foundl = true;
+                        break 2;
                     }
                 }
             }
         }
 
-        if ($found == false) {
-            if (!in_array($length, $this->_cardLength[$type])) {
-                $this->_error(self::LENGTH, $value);
-                return false;
-            } else {
-                $this->_error(self::PREFIX, $value);
-                return false;
-            }
+        if ($foundp == false){
+            $this->_error(self::PREFIX, $value);
+            return false;
+        }
+
+        if ($foundl == false) {
+            $this->_error(self::LENGTH, $value);
+            return false;
         }
 
         $sum    = 0;

+ 10 - 0
tests/Zend/Validate/CreditCardTest.php

@@ -249,6 +249,16 @@ class Zend_Validate_CreditCardTest extends PHPUnit_Framework_TestCase
         $this->assertEquals(array('Zend_Validate_CreditCardTest', 'staticCallback'), $validator->getService());
     }
 
+    /**
+     * @group ZF-9477
+     */
+    public function testMultiInstitute() {
+        $validator      = new Zend_Validate_CreditCard(array('type' => Zend_Validate_CreditCard::MASTERCARD));
+        $this->assertFalse($validator->isValid('4111111111111111'));
+        $message = $validator->getMessages();
+        $this->assertContains('not from an allowed institute', current($message));
+    }
+
     public static function staticCallback($value)
     {
         return false;