Ccnum.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Validate
  17. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id$
  20. */
  21. /**
  22. * @see Zend_Validate_Abstract
  23. */
  24. #require_once 'Zend/Validate/Abstract.php';
  25. /**
  26. * @category Zend
  27. * @package Zend_Validate
  28. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  29. * @license http://framework.zend.com/license/new-bsd New BSD License
  30. */
  31. class Zend_Validate_Ccnum extends Zend_Validate_Abstract
  32. {
  33. /**
  34. * Validation failure message key for when the value is not of valid length
  35. */
  36. const LENGTH = 'ccnumLength';
  37. /**
  38. * Validation failure message key for when the value fails the mod-10 checksum
  39. */
  40. const CHECKSUM = 'ccnumChecksum';
  41. /**
  42. * Digits filter for input
  43. *
  44. * @var Zend_Filter_Digits
  45. */
  46. protected static $_filter = null;
  47. /**
  48. * Validation failure message template definitions
  49. *
  50. * @var array
  51. */
  52. protected $_messageTemplates = array(
  53. self::LENGTH => "'%value%' must contain between 13 and 19 digits",
  54. self::CHECKSUM => "Luhn algorithm (mod-10 checksum) failed on '%value%'"
  55. );
  56. public function __construct()
  57. {
  58. trigger_error('Using the Ccnum validator is deprecated in favor of the CreditCard validator');
  59. }
  60. /**
  61. * Defined by Zend_Validate_Interface
  62. *
  63. * Returns true if and only if $value follows the Luhn algorithm (mod-10 checksum)
  64. *
  65. * @param string $value
  66. * @return boolean
  67. */
  68. public function isValid($value)
  69. {
  70. $this->_setValue($value);
  71. if (null === self::$_filter) {
  72. /**
  73. * @see Zend_Filter_Digits
  74. */
  75. #require_once 'Zend/Filter/Digits.php';
  76. self::$_filter = new Zend_Filter_Digits();
  77. }
  78. $valueFiltered = self::$_filter->filter($value);
  79. $length = strlen($valueFiltered);
  80. if ($length < 13 || $length > 19) {
  81. $this->_error(self::LENGTH);
  82. return false;
  83. }
  84. $sum = 0;
  85. $weight = 2;
  86. for ($i = $length - 2; $i >= 0; $i--) {
  87. $digit = $weight * $valueFiltered[$i];
  88. $sum += floor($digit / 10) + $digit % 10;
  89. $weight = $weight % 2 + 1;
  90. }
  91. if ((10 - $sum % 10) % 10 != $valueFiltered[$length - 1]) {
  92. $this->_error(self::CHECKSUM, $valueFiltered);
  93. return false;
  94. }
  95. return true;
  96. }
  97. }