Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AdapterAbstract.php
Go to the documentation of this file.
1 <?php
25 #require_once 'Zend/Validate/Barcode/AdapterInterface.php';
26 
35 {
40  protected $_length;
41 
46  protected $_characters;
47 
52  protected $_checksum;
53 
58  protected $_hasChecksum = true;
59 
66  public function checkLength($value)
67  {
68  if (!is_string($value)) {
69  return false;
70  }
71 
72  $fixum = strlen($value);
73  $found = false;
74  $length = $this->getLength();
75  if (is_array($length)) {
76  foreach ($length as $value) {
77  if ($fixum == $value) {
78  $found = true;
79  }
80 
81  if ($value == -1) {
82  $found = true;
83  }
84  }
85  } elseif ($fixum == $length) {
86  $found = true;
87  } elseif ($length == -1) {
88  $found = true;
89  } elseif ($length == 'even') {
90  $count = $fixum % 2;
91  $found = ($count == 0) ? true : false;
92  } elseif ($length == 'odd') {
93  $count = $fixum % 2;
94  $found = ($count == 1) ? true : false;
95  }
96 
97  return $found;
98  }
99 
106  public function checkChars($value)
107  {
108  if (!is_string($value)) {
109  return false;
110  }
111 
112  $characters = $this->getCharacters();
113  if ($characters == 128) {
114  for ($x = 0; $x < 128; ++$x) {
115  $value = str_replace(chr($x), '', $value);
116  }
117  } else {
118  $chars = str_split($characters);
119  foreach ($chars as $char) {
120  $value = str_replace($char, '', $value);
121  }
122  }
123 
124  if (strlen($value) > 0) {
125  return false;
126  }
127 
128  return true;
129  }
130 
137  public function checksum($value)
138  {
139  $checksum = $this->getChecksum();
140  if (!empty($checksum)) {
141  if (method_exists($this, $checksum)) {
142  return call_user_func(array($this, $checksum), $value);
143  }
144  }
145 
146  return false;
147  }
148 
154  public function getLength()
155  {
156  return $this->_length;
157  }
158 
164  public function getCharacters()
165  {
166  return $this->_characters;
167  }
168 
173  public function getChecksum()
174  {
175  return $this->_checksum;
176  }
177 
183  public function getCheck()
184  {
185  return $this->_hasChecksum;
186  }
187 
194  public function setCheck($check)
195  {
196  $this->_hasChecksum = (boolean) $check;
197  return $this;
198  }
199 
207  protected function _gtin($value)
208  {
209  $barcode = substr($value, 0, -1);
210  $sum = 0;
211  $length = strlen($barcode) - 1;
212 
213  for ($i = 0; $i <= $length; $i++) {
214  if (($i % 2) === 0) {
215  $sum += $barcode[$length - $i] * 3;
216  } else {
217  $sum += $barcode[$length - $i];
218  }
219  }
220 
221  $calc = $sum % 10;
222  $checksum = ($calc === 0) ? 0 : (10 - $calc);
223  if ($value[$length + 1] != $checksum) {
224  return false;
225  }
226 
227  return true;
228  }
229 
237  protected function _identcode($value)
238  {
239  $barcode = substr($value, 0, -1);
240  $sum = 0;
241  $length = strlen($value) - 2;
242 
243  for ($i = 0; $i <= $length; $i++) {
244  if (($i % 2) === 0) {
245  $sum += $barcode[$length - $i] * 4;
246  } else {
247  $sum += $barcode[$length - $i] * 9;
248  }
249  }
250 
251  $calc = $sum % 10;
252  $checksum = ($calc === 0) ? 0 : (10 - $calc);
253  if ($value[$length + 1] != $checksum) {
254  return false;
255  }
256 
257  return true;
258  }
259 
267  protected function _code25($value)
268  {
269  $barcode = substr($value, 0, -1);
270  $sum = 0;
271  $length = strlen($barcode) - 1;
272 
273  for ($i = 0; $i <= $length; $i++) {
274  if (($i % 2) === 0) {
275  $sum += $barcode[$i] * 3;
276  } else {
277  $sum += $barcode[$i];
278  }
279  }
280 
281  $calc = $sum % 10;
282  $checksum = ($calc === 0) ? 0 : (10 - $calc);
283  if ($value[$length + 1] != $checksum) {
284  return false;
285  }
286 
287  return true;
288  }
289 
297  protected function _postnet($value)
298  {
299  $checksum = substr($value, -1, 1);
300  $values = str_split(substr($value, 0, -1));
301 
302  $check = 0;
303  foreach($values as $row) {
304  $check += $row;
305  }
306 
307  $check %= 10;
308  $check = 10 - $check;
309  if ($check == $checksum) {
310  return true;
311  }
312 
313  return false;
314  }
315 }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$count
Definition: recent.phtml:13
$values
Definition: options.phtml:88
$value
Definition: gender.phtml:16
$i
Definition: gallery.phtml:31