Identcode.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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_Barcode
  17. * @subpackage Object
  18. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /**
  23. * @see Zend_Barcode_Object_Code25interleaved
  24. */
  25. require_once 'Zend/Barcode/Object/Code25interleaved.php';
  26. /**
  27. * @see Zend_Validate_Barcode
  28. */
  29. require_once 'Zend/Validate/Barcode.php';
  30. /**
  31. * Class for generate Identcode barcode
  32. *
  33. * @category Zend
  34. * @package Zend_Barcode
  35. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  36. * @license http://framework.zend.com/license/new-bsd New BSD License
  37. */
  38. class Zend_Barcode_Object_Identcode extends Zend_Barcode_Object_Code25interleaved
  39. {
  40. /**
  41. * Default options for Identcode barcode
  42. * @return void
  43. */
  44. protected function _getDefaultOptions()
  45. {
  46. $this->_barcodeLength = 12;
  47. $this->_mandatoryChecksum = true;
  48. }
  49. /**
  50. * Retrieve text to display
  51. * @return string
  52. */
  53. public function getTextToDisplay()
  54. {
  55. return preg_replace('/([0-9]{2})([0-9]{3})([0-9]{3})([0-9]{3})([0-9])/',
  56. '$1.$2 $3.$4 $5',
  57. $this->getText());
  58. }
  59. /**
  60. * Check allowed characters
  61. * @param string $value
  62. * @return string
  63. * @throws Zend_Barcode_Object_Exception
  64. */
  65. public function validateText($value)
  66. {
  67. $this->_validateText($value, array('validator' => $this->getType()));
  68. }
  69. /**
  70. * Get barcode checksum
  71. *
  72. * @param string $text
  73. * @return int
  74. */
  75. public function getChecksum($text)
  76. {
  77. $this->_checkText($text);
  78. $checksum = 0;
  79. for ($i = strlen($text); $i > 0; $i --) {
  80. $checksum += intval($text{$i - 1}) * (($i % 2) ? 4 : 9);
  81. }
  82. $checksum = (10 - ($checksum % 10)) % 10;
  83. return $checksum;
  84. }
  85. }