MatrixFactory.php 1.2 KB

1234567891011121314151617181920212223242526272829303132
  1. <?php
  2. declare(strict_types=1);
  3. namespace Endroid\QrCode\Bacon;
  4. use BaconQrCode\Encoder\Encoder;
  5. use Endroid\QrCode\Matrix\Matrix;
  6. use Endroid\QrCode\Matrix\MatrixFactoryInterface;
  7. use Endroid\QrCode\Matrix\MatrixInterface;
  8. use Endroid\QrCode\QrCodeInterface;
  9. final class MatrixFactory implements MatrixFactoryInterface
  10. {
  11. public function create(QrCodeInterface $qrCode): MatrixInterface
  12. {
  13. $baconErrorCorrectionLevel = ErrorCorrectionLevelConverter::convertToBaconErrorCorrectionLevel($qrCode->getErrorCorrectionLevel());
  14. $baconMatrix = Encoder::encode($qrCode->getData(), $baconErrorCorrectionLevel, strval($qrCode->getEncoding()))->getMatrix();
  15. $blockValues = [];
  16. $columnCount = $baconMatrix->getWidth();
  17. $rowCount = $baconMatrix->getHeight();
  18. for ($rowIndex = 0; $rowIndex < $rowCount; ++$rowIndex) {
  19. $blockValues[$rowIndex] = [];
  20. for ($columnIndex = 0; $columnIndex < $columnCount; ++$columnIndex) {
  21. $blockValues[$rowIndex][$columnIndex] = $baconMatrix->get($columnIndex, $rowIndex);
  22. }
  23. }
  24. return new Matrix($blockValues, $qrCode->getSize(), $qrCode->getMargin(), $qrCode->getRoundBlockSizeMode());
  25. }
  26. }