Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/lib/zend/Zend/Validate/Ccnum.php
Go to the documentation of this file.
00001 <?php
00025 require_once 'Zend/Validate/Abstract.php';
00026 
00033 class Zend_Validate_Ccnum extends Zend_Validate_Abstract
00034 {
00038     const LENGTH   = 'ccnumLength';
00039 
00043     const CHECKSUM = 'ccnumChecksum';
00044 
00050     protected static $_filter = null;
00051 
00057     protected $_messageTemplates = array(
00058         self::LENGTH   => "'%value%' must contain between 13 and 19 digits",
00059         self::CHECKSUM => "Luhn algorithm (mod-10 checksum) failed on '%value%'"
00060     );
00061 
00062     public function __construct()
00063     {
00064         trigger_error('Using the Ccnum validator is deprecated in favor of the CreditCard validator');
00065     }
00066 
00075     public function isValid($value)
00076     {
00077         $this->_setValue($value);
00078 
00079         if (null === self::$_filter) {
00083             require_once 'Zend/Filter/Digits.php';
00084             self::$_filter = new Zend_Filter_Digits();
00085         }
00086 
00087         $valueFiltered = self::$_filter->filter($value);
00088 
00089         $length = strlen($valueFiltered);
00090 
00091         if ($length < 13 || $length > 19) {
00092             $this->_error(self::LENGTH);
00093             return false;
00094         }
00095 
00096         $sum    = 0;
00097         $weight = 2;
00098 
00099         for ($i = $length - 2; $i >= 0; $i--) {
00100             $digit = $weight * $valueFiltered[$i];
00101             $sum += floor($digit / 10) + $digit % 10;
00102             $weight = $weight % 2 + 1;
00103         }
00104 
00105         if ((10 - $sum % 10) % 10 != $valueFiltered[$length - 1]) {
00106             $this->_error(self::CHECKSUM, $valueFiltered);
00107             return false;
00108         }
00109 
00110         return true;
00111     }
00112 }
 All Data Structures Namespaces Files Functions Variables Enumerations