Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Ccnum.php
Go to the documentation of this file.
1 <?php
25 #require_once 'Zend/Validate/Abstract.php';
26 
34 {
38  const LENGTH = 'ccnumLength';
39 
43  const CHECKSUM = 'ccnumChecksum';
44 
50  protected static $_filter = null;
51 
57  protected $_messageTemplates = array(
58  self::LENGTH => "'%value%' must contain between 13 and 19 digits",
59  self::CHECKSUM => "Luhn algorithm (mod-10 checksum) failed on '%value%'"
60  );
61 
62  public function __construct()
63  {
64  trigger_error('Using the Ccnum validator is deprecated in favor of the CreditCard validator');
65  }
66 
75  public function isValid($value)
76  {
77  $this->_setValue($value);
78 
79  if (null === self::$_filter) {
83  #require_once 'Zend/Filter/Digits.php';
84  self::$_filter = new Zend_Filter_Digits();
85  }
86 
87  $valueFiltered = self::$_filter->filter($value);
88 
89  $length = strlen($valueFiltered);
90 
91  if ($length < 13 || $length > 19) {
92  $this->_error(self::LENGTH);
93  return false;
94  }
95 
96  $sum = 0;
97  $weight = 2;
98 
99  for ($i = $length - 2; $i >= 0; $i--) {
100  $digit = $weight * $valueFiltered[$i];
101  $sum += floor($digit / 10) + $digit % 10;
102  $weight = $weight % 2 + 1;
103  }
104 
105  if ((10 - $sum % 10) % 10 != $valueFiltered[$length - 1]) {
106  $this->_error(self::CHECKSUM, $valueFiltered);
107  return false;
108  }
109 
110  return true;
111  }
112 }
_error($messageKey, $value=null)
Definition: Abstract.php:284
$value
Definition: gender.phtml:16
isValid($value)
Definition: Ccnum.php:75
static $_filter
Definition: Ccnum.php:50
$i
Definition: gallery.phtml:31