File indexing completed on 2024-05-12 06:03:13

0001 <?php
0002 /**
0003  * Zend Framework
0004  *
0005  * LICENSE
0006  *
0007  * This source file is subject to the new BSD license that is bundled
0008  * with this package in the file LICENSE.txt.
0009  * It is also available through the world-wide-web at this URL:
0010  * http://framework.zend.com/license/new-bsd
0011  * If you did not receive a copy of the license and are unable to
0012  * obtain it through the world-wide-web, please send an email
0013  * to license@zend.com so we can send you a copy immediately.
0014  *
0015  * @category   Zend
0016  * @package    Zend_Validate
0017  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0018  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0019  * @version    $Id$
0020  */
0021 
0022 /**
0023  * @see Zend_Validate_Abstract
0024  */
0025 // require_once 'Zend/Validate/Abstract.php';
0026 
0027 /**
0028  * @category   Zend
0029  * @package    Zend_Validate
0030  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0031  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0032  */
0033 class Zend_Validate_Ccnum extends Zend_Validate_Abstract
0034 {
0035     /**
0036      * Validation failure message key for when the value is not of valid length
0037      */
0038     const LENGTH   = 'ccnumLength';
0039 
0040     /**
0041      * Validation failure message key for when the value fails the mod-10 checksum
0042      */
0043     const CHECKSUM = 'ccnumChecksum';
0044 
0045     /**
0046      * Digits filter for input
0047      *
0048      * @var Zend_Filter_Digits
0049      */
0050     protected static $_filter = null;
0051 
0052     /**
0053      * Validation failure message template definitions
0054      *
0055      * @var array
0056      */
0057     protected $_messageTemplates = array(
0058         self::LENGTH   => "'%value%' must contain between 13 and 19 digits",
0059         self::CHECKSUM => "Luhn algorithm (mod-10 checksum) failed on '%value%'"
0060     );
0061 
0062     public function __construct()
0063     {
0064         trigger_error('Using the Ccnum validator is deprecated in favor of the CreditCard validator');
0065     }
0066 
0067     /**
0068      * Defined by Zend_Validate_Interface
0069      *
0070      * Returns true if and only if $value follows the Luhn algorithm (mod-10 checksum)
0071      *
0072      * @param  string $value
0073      * @return boolean
0074      */
0075     public function isValid($value)
0076     {
0077         $this->_setValue($value);
0078 
0079         if (null === self::$_filter) {
0080             /**
0081              * @see Zend_Filter_Digits
0082              */
0083             // require_once 'Zend/Filter/Digits.php';
0084             self::$_filter = new Zend_Filter_Digits();
0085         }
0086 
0087         $valueFiltered = self::$_filter->filter($value);
0088 
0089         $length = strlen($valueFiltered);
0090 
0091         if ($length < 13 || $length > 19) {
0092             $this->_error(self::LENGTH);
0093             return false;
0094         }
0095 
0096         $sum    = 0;
0097         $weight = 2;
0098 
0099         for ($i = $length - 2; $i >= 0; $i--) {
0100             $digit = $weight * $valueFiltered[$i];
0101             $sum += floor($digit / 10) + $digit % 10;
0102             $weight = $weight % 2 + 1;
0103         }
0104 
0105         if ((10 - $sum % 10) % 10 != $valueFiltered[$length - 1]) {
0106             $this->_error(self::CHECKSUM, $valueFiltered);
0107             return false;
0108         }
0109 
0110         return true;
0111     }
0112 }