QrCode.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. declare(strict_types=1);
  3. namespace Endroid\QrCode;
  4. use Endroid\QrCode\Color\Color;
  5. use Endroid\QrCode\Color\ColorInterface;
  6. use Endroid\QrCode\Encoding\Encoding;
  7. use Endroid\QrCode\Encoding\EncodingInterface;
  8. use Endroid\QrCode\ErrorCorrectionLevel\ErrorCorrectionLevelInterface;
  9. use Endroid\QrCode\ErrorCorrectionLevel\ErrorCorrectionLevelLow;
  10. use Endroid\QrCode\RoundBlockSizeMode\RoundBlockSizeModeInterface;
  11. use Endroid\QrCode\RoundBlockSizeMode\RoundBlockSizeModeMargin;
  12. final class QrCode implements QrCodeInterface
  13. {
  14. private string $data;
  15. private EncodingInterface $encoding;
  16. private ErrorCorrectionLevelInterface $errorCorrectionLevel;
  17. private int $size;
  18. private int $margin;
  19. private RoundBlockSizeModeInterface $roundBlockSizeMode;
  20. private ColorInterface $foregroundColor;
  21. private ColorInterface $backgroundColor;
  22. public function __construct(
  23. string $data,
  24. EncodingInterface $encoding = null,
  25. ErrorCorrectionLevelInterface $errorCorrectionLevel = null,
  26. int $size = 300,
  27. int $margin = 10,
  28. RoundBlockSizeModeInterface $roundBlockSizeMode = null,
  29. ColorInterface $foregroundColor = null,
  30. ColorInterface $backgroundColor = null
  31. ) {
  32. $this->data = $data;
  33. $this->encoding = $encoding ?? new Encoding('UTF-8');
  34. $this->errorCorrectionLevel = $errorCorrectionLevel ?? new ErrorCorrectionLevelLow();
  35. $this->size = $size;
  36. $this->margin = $margin;
  37. $this->roundBlockSizeMode = $roundBlockSizeMode ?? new RoundBlockSizeModeMargin();
  38. $this->foregroundColor = $foregroundColor ?? new Color(0, 0, 0);
  39. $this->backgroundColor = $backgroundColor ?? new Color(255, 255, 255);
  40. }
  41. public static function create(string $data): self
  42. {
  43. return new self($data);
  44. }
  45. public function getData(): string
  46. {
  47. return $this->data;
  48. }
  49. public function setData(string $data): self
  50. {
  51. $this->data = $data;
  52. return $this;
  53. }
  54. public function getEncoding(): EncodingInterface
  55. {
  56. return $this->encoding;
  57. }
  58. public function setEncoding(Encoding $encoding): self
  59. {
  60. $this->encoding = $encoding;
  61. return $this;
  62. }
  63. public function getErrorCorrectionLevel(): ErrorCorrectionLevelInterface
  64. {
  65. return $this->errorCorrectionLevel;
  66. }
  67. public function setErrorCorrectionLevel(ErrorCorrectionLevelInterface $errorCorrectionLevel): self
  68. {
  69. $this->errorCorrectionLevel = $errorCorrectionLevel;
  70. return $this;
  71. }
  72. public function getSize(): int
  73. {
  74. return $this->size;
  75. }
  76. public function setSize(int $size): self
  77. {
  78. $this->size = $size;
  79. return $this;
  80. }
  81. public function getMargin(): int
  82. {
  83. return $this->margin;
  84. }
  85. public function setMargin(int $margin): self
  86. {
  87. $this->margin = $margin;
  88. return $this;
  89. }
  90. public function getRoundBlockSizeMode(): RoundBlockSizeModeInterface
  91. {
  92. return $this->roundBlockSizeMode;
  93. }
  94. public function setRoundBlockSizeMode(RoundBlockSizeModeInterface $roundBlockSizeMode): self
  95. {
  96. $this->roundBlockSizeMode = $roundBlockSizeMode;
  97. return $this;
  98. }
  99. public function getForegroundColor(): ColorInterface
  100. {
  101. return $this->foregroundColor;
  102. }
  103. public function setForegroundColor(ColorInterface $foregroundColor): self
  104. {
  105. $this->foregroundColor = $foregroundColor;
  106. return $this;
  107. }
  108. public function getBackgroundColor(): ColorInterface
  109. {
  110. return $this->backgroundColor;
  111. }
  112. public function setBackgroundColor(ColorInterface $backgroundColor): self
  113. {
  114. $this->backgroundColor = $backgroundColor;
  115. return $this;
  116. }
  117. }