BinarySequenceConverter.php 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\librarys\Picqer\Barcode\Helpers;
  8. use app\librarys\Picqer\Barcode\Barcode;
  9. use app\librarys\Picqer\Barcode\BarcodeBar;
  10. /**
  11. * Convert binary barcode sequence string to Barcode representation.
  12. */
  13. class BinarySequenceConverter
  14. {
  15. public static function convert(string $code, string $sequence): Barcode
  16. {
  17. $barcode = new Barcode($code);
  18. $len = strlen($sequence);
  19. $barWidth = 0;
  20. for ($i = 0; $i < $len; ++$i) {
  21. $barWidth += 1;
  22. if (($i == ($len - 1)) || (($i < ($len - 1)) && ($sequence[$i] != $sequence[($i + 1)]))) {
  23. if ($sequence[$i] == '1') {
  24. $drawBar = true;
  25. } else {
  26. $drawBar = false;
  27. }
  28. $barcode->addBar(new BarcodeBar($barWidth, 1, $drawBar));
  29. $barWidth = 0;
  30. }
  31. }
  32. return $barcode;
  33. }
  34. }