QrCode.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. declare(strict_types = 1);
  3. namespace BaconQrCode\Encoder;
  4. use BaconQrCode\Common\ErrorCorrectionLevel;
  5. use BaconQrCode\Common\Mode;
  6. use BaconQrCode\Common\Version;
  7. /**
  8. * QR code.
  9. */
  10. final class QrCode
  11. {
  12. /**
  13. * Number of possible mask patterns.
  14. */
  15. public const NUM_MASK_PATTERNS = 8;
  16. /**
  17. * Mode of the QR code.
  18. *
  19. * @var Mode
  20. */
  21. private $mode;
  22. /**
  23. * EC level of the QR code.
  24. *
  25. * @var ErrorCorrectionLevel
  26. */
  27. private $errorCorrectionLevel;
  28. /**
  29. * Version of the QR code.
  30. *
  31. * @var Version
  32. */
  33. private $version;
  34. /**
  35. * Mask pattern of the QR code.
  36. *
  37. * @var int
  38. */
  39. private $maskPattern = -1;
  40. /**
  41. * Matrix of the QR code.
  42. *
  43. * @var ByteMatrix
  44. */
  45. private $matrix;
  46. public function __construct(
  47. Mode $mode,
  48. ErrorCorrectionLevel $errorCorrectionLevel,
  49. Version $version,
  50. int $maskPattern,
  51. ByteMatrix $matrix
  52. ) {
  53. $this->mode = $mode;
  54. $this->errorCorrectionLevel = $errorCorrectionLevel;
  55. $this->version = $version;
  56. $this->maskPattern = $maskPattern;
  57. $this->matrix = $matrix;
  58. }
  59. /**
  60. * Gets the mode.
  61. */
  62. public function getMode() : Mode
  63. {
  64. return $this->mode;
  65. }
  66. /**
  67. * Gets the EC level.
  68. */
  69. public function getErrorCorrectionLevel() : ErrorCorrectionLevel
  70. {
  71. return $this->errorCorrectionLevel;
  72. }
  73. /**
  74. * Gets the version.
  75. */
  76. public function getVersion() : Version
  77. {
  78. return $this->version;
  79. }
  80. /**
  81. * Gets the mask pattern.
  82. */
  83. public function getMaskPattern() : int
  84. {
  85. return $this->maskPattern;
  86. }
  87. /**
  88. * Gets the matrix.
  89. *
  90. * @return ByteMatrix
  91. */
  92. public function getMatrix()
  93. {
  94. return $this->matrix;
  95. }
  96. /**
  97. * Validates whether a mask pattern is valid.
  98. */
  99. public static function isValidMaskPattern(int $maskPattern) : bool
  100. {
  101. return $maskPattern > 0 && $maskPattern < self::NUM_MASK_PATTERNS;
  102. }
  103. /**
  104. * Returns a string representation of the QR code.
  105. */
  106. public function __toString() : string
  107. {
  108. $result = "<<\n"
  109. . ' mode: ' . $this->mode . "\n"
  110. . ' ecLevel: ' . $this->errorCorrectionLevel . "\n"
  111. . ' version: ' . $this->version . "\n"
  112. . ' maskPattern: ' . $this->maskPattern . "\n";
  113. if ($this->matrix === null) {
  114. $result .= " matrix: null\n";
  115. } else {
  116. $result .= " matrix:\n";
  117. $result .= $this->matrix;
  118. }
  119. $result .= ">>\n";
  120. return $result;
  121. }
  122. }