Itf14.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. /** @see Zend_Barcode_Object_Int25 */
  23. require_once 'Zend/Barcode/Object/Int25.php';
  24. /** @see Zend_Validate_Barcode */
  25. require_once 'Zend/Validate/Barcode.php';
  26. /**
  27. * Class for generate Itf14 barcode
  28. *
  29. * @category Zend
  30. * @package Zend_Barcode
  31. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  32. * @license http://framework.zend.com/license/new-bsd New BSD License
  33. */
  34. class Zend_Barcode_Object_Itf14 extends Zend_Barcode_Object_Int25
  35. {
  36. /**
  37. * Constructor
  38. * @param array|Zend_Config $options
  39. * @return void
  40. */
  41. public function __construct($options = null)
  42. {
  43. parent::__construct($options);
  44. // Checksum is mandatory with Itf14
  45. $this->_withChecksum = true;
  46. // Default to true but not mandatory
  47. $this->_withChecksumInText = true;
  48. }
  49. /**
  50. * Activate/deactivate the automatic generation
  51. * of the checksum character
  52. * added to the barcode text
  53. * @param boolean $value
  54. * @return Zend_Barcode_Object
  55. */
  56. public function setWithChecksum($value)
  57. {
  58. // Checksum is mandatory with Itf14
  59. return $this;
  60. }
  61. /**
  62. * Activate/deactivate the automatic generation
  63. * of the checksum character
  64. * added to the barcode text
  65. * @param boolean $value
  66. * @return Zend_Barcode_Object
  67. * @throw Zend_Barcode_Object_Exception
  68. */
  69. public function setWithChecksumInText($value)
  70. {
  71. return $this;
  72. }
  73. /**
  74. * Retrieve text to encode
  75. * @return string
  76. */
  77. public function getText()
  78. {
  79. $text = $this->_getTextWithChecksum();
  80. if (strlen($text) < 14) {
  81. $text = str_repeat('0', 14 - strlen($text)) . $text;
  82. }
  83. return $text;
  84. }
  85. /**
  86. * Retrieve text to display
  87. * @return string
  88. */
  89. public function getTextToDisplay()
  90. {
  91. return $this->getText();
  92. }
  93. /**
  94. * Check allowed characters
  95. * @param string $value
  96. * @return string
  97. * @throw Zend_Barcode_Object_Exception
  98. */
  99. public function validateText($value)
  100. {
  101. $validator = new Zend_Validate_Barcode(array(
  102. 'adapter' => 'itf14',
  103. 'checksum' => false,
  104. ));
  105. // prepend '0'
  106. if (strlen($value) < 13) {
  107. $value = str_repeat('0', 13 - strlen($value)) . $value;
  108. }
  109. // add a '0' because checksum is mandatory
  110. if (!$validator->isValid($value . '0')) {
  111. $message = implode("\n", $validator->getMessages());
  112. /**
  113. * @see Zend_Barcode_Object_Exception
  114. */
  115. require_once 'Zend/Barcode/Object/Exception.php';
  116. throw new Zend_Barcode_Object_Exception($message);
  117. }
  118. }
  119. }