Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/lib/zend/Zend/Validate/CreditCard.php
Go to the documentation of this file.
00001 <?php
00025 require_once 'Zend/Validate/Abstract.php';
00026 
00033 class Zend_Validate_CreditCard extends Zend_Validate_Abstract
00034 {
00040     const ALL              = 'All';
00041     const AMERICAN_EXPRESS = 'American_Express';
00042     const UNIONPAY         = 'Unionpay';
00043     const DINERS_CLUB      = 'Diners_Club';
00044     const DINERS_CLUB_US   = 'Diners_Club_US';
00045     const DISCOVER         = 'Discover';
00046     const JCB              = 'JCB';
00047     const LASER            = 'Laser';
00048     const MAESTRO          = 'Maestro';
00049     const MASTERCARD       = 'Mastercard';
00050     const SOLO             = 'Solo';
00051     const VISA             = 'Visa';
00052 
00053     const CHECKSUM       = 'creditcardChecksum';
00054     const CONTENT        = 'creditcardContent';
00055     const INVALID        = 'creditcardInvalid';
00056     const LENGTH         = 'creditcardLength';
00057     const PREFIX         = 'creditcardPrefix';
00058     const SERVICE        = 'creditcardService';
00059     const SERVICEFAILURE = 'creditcardServiceFailure';
00060 
00066     protected $_messageTemplates = array(
00067         self::CHECKSUM       => "Luhn algorithm (mod-10 checksum) failed on '%value%'",
00068         self::CONTENT        => "'%value%' must contain only digits",
00069         self::INVALID        => "Invalid type given, value should be a string",
00070         self::LENGTH         => "'%value%' contains an invalid amount of digits",
00071         self::PREFIX         => "'%value%' is not from an allowed institute",
00072         self::SERVICE        => "Validation of '%value%' has been failed by the service",
00073         self::SERVICEFAILURE => "The service returned a failure while validating '%value%'",
00074     );
00075 
00081     protected $_cardLength = array(
00082         self::AMERICAN_EXPRESS => array(15),
00083         self::DINERS_CLUB      => array(14),
00084         self::DINERS_CLUB_US   => array(16),
00085         self::DISCOVER         => array(16),
00086         self::JCB              => array(16),
00087         self::LASER            => array(16, 17, 18, 19),
00088         self::MAESTRO          => array(12, 13, 14, 15, 16, 17, 18, 19),
00089         self::MASTERCARD       => array(16),
00090         self::SOLO             => array(16, 18, 19),
00091         self::UNIONPAY         => array(16, 17, 18, 19),
00092         self::VISA             => array(16),
00093     );
00094 
00100     protected $_cardType = array(
00101         self::AMERICAN_EXPRESS => array('34', '37'),
00102         self::DINERS_CLUB      => array('300', '301', '302', '303', '304', '305', '36'),
00103         self::DINERS_CLUB_US   => array('54', '55'),
00104         self::DISCOVER         => array('6011', '622126', '622127', '622128', '622129', '62213',
00105                                         '62214', '62215', '62216', '62217', '62218', '62219',
00106                                         '6222', '6223', '6224', '6225', '6226', '6227', '6228',
00107                                         '62290', '62291', '622920', '622921', '622922', '622923',
00108                                         '622924', '622925', '644', '645', '646', '647', '648',
00109                                         '649', '65'),
00110         self::JCB              => array('3528', '3529', '353', '354', '355', '356', '357', '358'),
00111         self::LASER            => array('6304', '6706', '6771', '6709'),
00112         self::MAESTRO          => array('5018', '5020', '5038', '6304', '6759', '6761', '6763'),
00113         self::MASTERCARD       => array('51', '52', '53', '54', '55'),
00114         self::SOLO             => array('6334', '6767'),
00115         self::UNIONPAY         => array('622126', '622127', '622128', '622129', '62213', '62214',
00116                                         '62215', '62216', '62217', '62218', '62219', '6222', '6223',
00117                                         '6224', '6225', '6226', '6227', '6228', '62290', '62291',
00118                                         '622920', '622921', '622922', '622923', '622924', '622925'),
00119         self::VISA             => array('4'),
00120     );
00121 
00127     protected $_type = array();
00128 
00134     protected $_service;
00135 
00141     public function __construct($options = array())
00142     {
00143         if ($options instanceof Zend_Config) {
00144             $options = $options->toArray();
00145         } else if (!is_array($options)) {
00146             $options = func_get_args();
00147             $temp['type'] = array_shift($options);
00148             if (!empty($options)) {
00149                 $temp['service'] = array_shift($options);
00150             }
00151 
00152             $options = $temp;
00153         }
00154 
00155         if (!array_key_exists('type', $options)) {
00156             $options['type'] = self::ALL;
00157         }
00158 
00159         $this->setType($options['type']);
00160         if (array_key_exists('service', $options)) {
00161             $this->setService($options['service']);
00162         }
00163     }
00164 
00170     public function getType()
00171     {
00172         return $this->_type;
00173     }
00174 
00181     public function setType($type)
00182     {
00183         $this->_type = array();
00184         return $this->addType($type);
00185     }
00186 
00193     public function addType($type)
00194     {
00195         if (is_string($type)) {
00196             $type = array($type);
00197         }
00198 
00199         foreach($type as $typ) {
00200             if (defined('self::' . strtoupper($typ)) && !in_array($typ, $this->_type)) {
00201                 $this->_type[] = $typ;
00202             }
00203 
00204             if (($typ == self::ALL)) {
00205                 $this->_type = array_keys($this->_cardLength);
00206             }
00207         }
00208 
00209         return $this;
00210     }
00211 
00217     public function getService()
00218     {
00219         return $this->_service;
00220     }
00221 
00227     public function setService($service)
00228     {
00229         if (!is_callable($service)) {
00230             require_once 'Zend/Validate/Exception.php';
00231             throw new Zend_Validate_Exception('Invalid callback given');
00232         }
00233 
00234         $this->_service = $service;
00235         return $this;
00236     }
00237 
00246     public function isValid($value)
00247     {
00248         $this->_setValue($value);
00249 
00250         if (!is_string($value)) {
00251             $this->_error(self::INVALID, $value);
00252             return false;
00253         }
00254 
00255         if (!ctype_digit($value)) {
00256             $this->_error(self::CONTENT, $value);
00257             return false;
00258         }
00259 
00260         $length = strlen($value);
00261         $types  = $this->getType();
00262         $foundp = false;
00263         $foundl = false;
00264         foreach ($types as $type) {
00265             foreach ($this->_cardType[$type] as $prefix) {
00266                 if (substr($value, 0, strlen($prefix)) == $prefix) {
00267                     $foundp = true;
00268                     if (in_array($length, $this->_cardLength[$type])) {
00269                         $foundl = true;
00270                         break 2;
00271                     }
00272                 }
00273             }
00274         }
00275 
00276         if ($foundp == false){
00277             $this->_error(self::PREFIX, $value);
00278             return false;
00279         }
00280 
00281         if ($foundl == false) {
00282             $this->_error(self::LENGTH, $value);
00283             return false;
00284         }
00285 
00286         $sum    = 0;
00287         $weight = 2;
00288 
00289         for ($i = $length - 2; $i >= 0; $i--) {
00290             $digit = $weight * $value[$i];
00291             $sum += floor($digit / 10) + $digit % 10;
00292             $weight = $weight % 2 + 1;
00293         }
00294 
00295         if ((10 - $sum % 10) % 10 != $value[$length - 1]) {
00296             $this->_error(self::CHECKSUM, $value);
00297             return false;
00298         }
00299 
00300         if (!empty($this->_service)) {
00301             try {
00302                 require_once 'Zend/Validate/Callback.php';
00303                 $callback = new Zend_Validate_Callback($this->_service);
00304                 $callback->setOptions($this->_type);
00305                 if (!$callback->isValid($value)) {
00306                     $this->_error(self::SERVICE, $value);
00307                     return false;
00308                 }
00309             } catch (Zend_Exception $e) {
00310                 $this->_error(self::SERVICEFAILURE, $value);
00311                 return false;
00312             }
00313         }
00314 
00315         return true;
00316     }
00317 }
 All Data Structures Namespaces Files Functions Variables Enumerations