2
0

Ccnum.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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-2009 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-2009 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. /**
  57. * Defined by Zend_Validate_Interface
  58. *
  59. * Returns true if and only if $value follows the Luhn algorithm (mod-10 checksum)
  60. *
  61. * @param string $value
  62. * @return boolean
  63. */
  64. public function isValid($value)
  65. {
  66. $this->_setValue($value);
  67. if (null === self::$_filter) {
  68. /**
  69. * @see Zend_Filter_Digits
  70. */
  71. require_once 'Zend/Filter/Digits.php';
  72. self::$_filter = new Zend_Filter_Digits();
  73. }
  74. $valueFiltered = self::$_filter->filter($value);
  75. $length = strlen($valueFiltered);
  76. if ($length < 13 || $length > 19) {
  77. $this->_error(self::LENGTH);
  78. return false;
  79. }
  80. $sum = 0;
  81. $weight = 2;
  82. for ($i = $length - 2; $i >= 0; $i--) {
  83. $digit = $weight * $valueFiltered[$i];
  84. $sum += floor($digit / 10) + $digit % 10;
  85. $weight = $weight % 2 + 1;
  86. }
  87. if ((10 - $sum % 10) % 10 != $valueFiltered[$length - 1]) {
  88. $this->_error(self::CHECKSUM, $valueFiltered);
  89. return false;
  90. }
  91. return true;
  92. }
  93. }