2
0

Identcode.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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-2009 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-2009 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. * Number of characters in the barcode
  42. * @var $_barcodeLength integer | string
  43. */
  44. protected $_barcodeLength = 12;
  45. /**
  46. * Activation of mandatory checksum
  47. * to deactivate unauthorized modification
  48. * @var $_mandatoryChecksum boolean
  49. */
  50. protected $_mandatoryChecksum = true;
  51. /**
  52. * Retrieve text to display
  53. * @return string
  54. */
  55. public function getTextToDisplay()
  56. {
  57. return preg_replace('/([0-9]{2})([0-9]{3})([0-9]{3})([0-9]{3})([0-9])/',
  58. '$1.$2 $3.$4 $5',
  59. $this->getText());
  60. }
  61. /**
  62. * Check allowed characters
  63. * @param string $value
  64. * @return string
  65. * @throw Zend_Barcode_Object_Exception
  66. */
  67. public function validateText($value)
  68. {
  69. $this->_validateText($value, array('validator' => $this->getType()));
  70. }
  71. /**
  72. * Get barcode checksum
  73. *
  74. * @param string $text
  75. * @return int
  76. */
  77. public function getChecksum($text)
  78. {
  79. $this->_checkText($text);
  80. $checksum = 0;
  81. for ($i = strlen($text); $i > 0; $i --) {
  82. $checksum += intval($text{$i - 1}) * (($i % 2) ? 4 : 9);
  83. }
  84. $checksum = 10 - ($checksum % 10);
  85. if ($checksum == 10) {
  86. $checksum = 0;
  87. }
  88. return $checksum;
  89. }
  90. }