Royalmail.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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_ObjectAbstract
  24. */
  25. require_once 'Zend/Barcode/Object/ObjectAbstract.php';
  26. /**
  27. * @see Zend_Validate_Barcode
  28. */
  29. require_once 'Zend/Validate/Barcode.php';
  30. /**
  31. * Class for generate Royal maim 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_Royalmail extends Zend_Barcode_Object_ObjectAbstract
  39. {
  40. /**
  41. * Coding map
  42. * - 0 = Tracker, Ascender and Descender
  43. * - 1 = Tracker and Ascender
  44. * - 2 = Tracker and Descender
  45. * - 3 = Tracker
  46. * @var array
  47. */
  48. protected $_codingMap = array(
  49. '0' => '3300', '1' => '3210', '2' => '3201', '3' => '2310', '4' => '2301', '5' => '2211',
  50. '6' => '3120', '7' => '3030', '8' => '3021', '9' => '2130', 'A' => '2121', 'B' => '2031',
  51. 'C' => '3102', 'D' => '3012', 'E' => '3003', 'F' => '2112', 'G' => '2103', 'H' => '2013',
  52. 'I' => '1320', 'J' => '1230', 'K' => '1221', 'L' => '0330', 'M' => '0321', 'N' => '0231',
  53. 'O' => '1302', 'P' => '1212', 'Q' => '1203', 'R' => '0312', 'S' => '0303', 'T' => '0213',
  54. 'U' => '1122', 'V' => '1032', 'W' => '1023', 'X' => '0132', 'Y' => '0123', 'Z' => '0033'
  55. );
  56. protected $_rows = array(
  57. '0' => 1, '1' => 1, '2' => 1, '3' => 1, '4' => 1, '5' => 1,
  58. '6' => 2, '7' => 2, '8' => 2, '9' => 2, 'A' => 2, 'B' => 2,
  59. 'C' => 3, 'D' => 3, 'E' => 3, 'F' => 3, 'G' => 3, 'H' => 3,
  60. 'I' => 4, 'J' => 4, 'K' => 4, 'L' => 4, 'M' => 4, 'N' => 4,
  61. 'O' => 5, 'P' => 5, 'Q' => 5, 'R' => 5, 'S' => 5, 'T' => 5,
  62. 'U' => 0, 'V' => 0, 'W' => 0, 'X' => 0, 'Y' => 0, 'Z' => 0,
  63. );
  64. protected $_columns = array(
  65. '0' => 1, '1' => 2, '2' => 3, '3' => 4, '4' => 5, '5' => 0,
  66. '6' => 1, '7' => 2, '8' => 3, '9' => 4, 'A' => 5, 'B' => 0,
  67. 'C' => 1, 'D' => 2, 'E' => 3, 'F' => 4, 'G' => 5, 'H' => 0,
  68. 'I' => 1, 'J' => 2, 'K' => 3, 'L' => 4, 'M' => 5, 'N' => 0,
  69. 'O' => 1, 'P' => 2, 'Q' => 3, 'R' => 4, 'S' => 5, 'T' => 0,
  70. 'U' => 1, 'V' => 2, 'W' => 3, 'X' => 4, 'Y' => 5, 'Z' => 0,
  71. );
  72. /**
  73. * Default options for Postnet barcode
  74. * @return void
  75. */
  76. protected function _getDefaultOptions()
  77. {
  78. $this->_barThinWidth = 2;
  79. $this->_barHeight = 20;
  80. $this->_drawText = false;
  81. $this->_stretchText = true;
  82. $this->_mandatoryChecksum = true;
  83. }
  84. /**
  85. * Width of the barcode (in pixels)
  86. * @return integer
  87. */
  88. protected function _calculateBarcodeWidth()
  89. {
  90. $quietZone = $this->getQuietZone();
  91. $startCharacter = (2 * $this->_barThinWidth) * $this->_factor;
  92. $stopCharacter = (1 * $this->_barThinWidth) * $this->_factor;
  93. $encodedData = (8 * $this->_barThinWidth) * $this->_factor * strlen($this->getText());
  94. return $quietZone + $startCharacter + $encodedData + $stopCharacter + $quietZone;
  95. }
  96. /**
  97. * Partial check of interleaved Postnet barcode
  98. * @return void
  99. */
  100. protected function _checkParams()
  101. {}
  102. /**
  103. * Prepare array to draw barcode
  104. * @return array
  105. */
  106. protected function _prepareBarcode()
  107. {
  108. $barcodeTable = array();
  109. // Start character (1)
  110. $barcodeTable[] = array(1 , $this->_barThinWidth , 0 , 5/8);
  111. $barcodeTable[] = array(0 , $this->_barThinWidth , 0 , 1);
  112. // Text to encode
  113. $textTable = str_split($this->getText());
  114. foreach ($textTable as $char) {
  115. $bars = str_split($this->_codingMap[$char]);
  116. foreach ($bars as $b) {
  117. $barcodeTable[] = array(1 , $this->_barThinWidth , ($b > 1 ? 3/8 : 0) , ($b % 2 ? 5/8 : 1));
  118. $barcodeTable[] = array(0 , $this->_barThinWidth , 0 , 1);
  119. }
  120. }
  121. // Stop character (1)
  122. $barcodeTable[] = array(1 , $this->_barThinWidth , 0 , 1);
  123. return $barcodeTable;
  124. }
  125. /**
  126. * Get barcode checksum
  127. *
  128. * @param string $text
  129. * @return int
  130. */
  131. public function getChecksum($text)
  132. {
  133. $this->_checkText($text);
  134. $values = str_split($text);
  135. $rowvalue = 0;
  136. $colvalue = 0;
  137. foreach($values as $row) {
  138. $rowvalue += $this->_rows[$row];
  139. $colvalue += $this->_columns[$row];
  140. }
  141. $rowvalue %= 6;
  142. $colvalue %= 6;
  143. $rowchkvalue = array_keys($this->_rows, $rowvalue);
  144. $colchkvalue = array_keys($this->_columns, $colvalue);
  145. return current(array_intersect($rowchkvalue, $colchkvalue));
  146. }
  147. }