Issn.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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_Barcode_AdapterAbstract
  23. */
  24. #require_once 'Zend/Validate/Barcode/AdapterAbstract.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_Barcode_Issn extends Zend_Validate_Barcode_AdapterAbstract
  32. {
  33. /**
  34. * Allowed barcode lengths
  35. * @var integer
  36. */
  37. protected $_length = array(8, 13);
  38. /**
  39. * Allowed barcode characters
  40. * @var string
  41. */
  42. protected $_characters = '0123456789X';
  43. /**
  44. * Checksum function
  45. * @var string
  46. */
  47. protected $_checksum = '_gtin';
  48. /**
  49. * Allows X on length of 8 chars
  50. *
  51. * @param string $value The barcode to check for allowed characters
  52. * @return boolean
  53. */
  54. public function checkChars($value)
  55. {
  56. if (strlen($value) != 8) {
  57. if (strpos($value, 'X') !== false) {
  58. return false;
  59. }
  60. }
  61. return parent::checkChars($value);
  62. }
  63. /**
  64. * Validates the checksum
  65. *
  66. * @param string $value The barcode to check the checksum for
  67. * @return boolean
  68. */
  69. public function checksum($value)
  70. {
  71. if (strlen($value) == 8) {
  72. $this->_checksum = '_issn';
  73. } else {
  74. $this->_checksum = '_gtin';
  75. }
  76. return parent::checksum($value);
  77. }
  78. /**
  79. * Validates the checksum ()
  80. * ISSN implementation (reversed mod11)
  81. *
  82. * @param string $value The barcode to validate
  83. * @return boolean
  84. */
  85. protected function _issn($value)
  86. {
  87. $checksum = substr($value, -1, 1);
  88. $values = str_split(substr($value, 0, -1));
  89. $check = 0;
  90. $multi = 8;
  91. foreach($values as $token) {
  92. if ($token == 'X') {
  93. $token = 10;
  94. }
  95. $check += ($token * $multi);
  96. --$multi;
  97. }
  98. $check %= 11;
  99. $check = 11 - $check;
  100. if ($check == $checksum) {
  101. return true;
  102. } else if (($check == 10) && ($checksum == 'X')) {
  103. return true;
  104. }
  105. return false;
  106. }
  107. }