BCGothercode.barcode.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. /**
  3. *--------------------------------------------------------------------
  4. *
  5. * Sub-Class - othercode
  6. *
  7. * Other Codes
  8. * Starting with a bar and altern to space, bar, ...
  9. * 0 is the smallest
  10. *
  11. *--------------------------------------------------------------------
  12. * Copyright (C) Jean-Sebastien Goupil
  13. * http://www.barcodephp.com
  14. */
  15. include_once('BCGParseException.php');
  16. include_once('BCGBarcode1D.php');
  17. class BCGothercode extends BCGBarcode1D {
  18. /**
  19. * Constructor.
  20. */
  21. public function __construct() {
  22. parent::__construct();
  23. $this->keys = array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9');
  24. }
  25. /**
  26. * Draws the barcode.
  27. *
  28. * @param resource $im
  29. */
  30. public function draw($im) {
  31. $this->drawChar($im, $this->text, true);
  32. $this->drawText($im, 0, 0, $this->positionX, $this->thickness);
  33. }
  34. /**
  35. * Gets the label.
  36. * If the label was set to BCGBarcode1D::AUTO_LABEL, the label will display the value from the text parsed.
  37. *
  38. * @return string
  39. */
  40. public function getLabel() {
  41. $label = $this->label;
  42. if ($this->label === BCGBarcode1D::AUTO_LABEL) {
  43. $label = '';
  44. }
  45. return $label;
  46. }
  47. /**
  48. * Returns the maximal size of a barcode.
  49. *
  50. * @param int $w
  51. * @param int $h
  52. * @return int[]
  53. */
  54. public function getDimension($w, $h) {
  55. $array = str_split($this->text, 1);
  56. $textlength = array_sum($array) + count($array);
  57. $w += $textlength;
  58. $h += $this->thickness;
  59. return parent::getDimension($w, $h);
  60. }
  61. /**
  62. * Validates the input.
  63. */
  64. protected function validate() {
  65. $c = strlen($this->text);
  66. if ($c === 0) {
  67. throw new BCGParseException('othercode', 'No data has been entered.');
  68. }
  69. // Checking if all chars are allowed
  70. for ($i = 0; $i < $c; $i++) {
  71. if (array_search($this->text[$i], $this->keys) === false) {
  72. throw new BCGParseException('othercode', 'The character \'' . $this->text[$i] . '\' is not allowed.');
  73. }
  74. }
  75. parent::validate();
  76. }
  77. }
  78. ?>