QrCode.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. /** @var string */
  15. private $data;
  16. /** @var EncodingInterface */
  17. private $encoding;
  18. /** @var ErrorCorrectionLevelInterface */
  19. private $errorCorrectionLevel;
  20. /** @var int */
  21. private $size;
  22. /** @var int */
  23. private $margin;
  24. /** @var RoundBlockSizeModeInterface */
  25. private $roundBlockSizeMode;
  26. /** @var ColorInterface */
  27. private $foregroundColor;
  28. /** @var ColorInterface */
  29. private $backgroundColor;
  30. public function __construct(
  31. string $data,
  32. EncodingInterface $encoding = null,
  33. ErrorCorrectionLevelInterface $errorCorrectionLevel = null,
  34. int $size = 300,
  35. int $margin = 10,
  36. RoundBlockSizeModeInterface $roundBlockSizeMode = null,
  37. ColorInterface $foregroundColor = null,
  38. ColorInterface $backgroundColor = null
  39. ) {
  40. $this->data = $data;
  41. $this->encoding = isset($encoding) ? $encoding : new Encoding('UTF-8');
  42. $this->errorCorrectionLevel = isset($errorCorrectionLevel) ? $errorCorrectionLevel : new ErrorCorrectionLevelLow();
  43. $this->size = $size;
  44. $this->margin = $margin;
  45. $this->roundBlockSizeMode = isset($roundBlockSizeMode) ? $roundBlockSizeMode : new RoundBlockSizeModeMargin();
  46. $this->foregroundColor = isset($foregroundColor) ? $foregroundColor : new Color(0, 0, 0);
  47. $this->backgroundColor = isset($backgroundColor) ? $backgroundColor : new Color(255, 255, 255);
  48. }
  49. public static function create(string $data): self
  50. {
  51. return new self($data);
  52. }
  53. public function getData(): string
  54. {
  55. return $this->data;
  56. }
  57. public function setData(string $data): self
  58. {
  59. $this->data = $data;
  60. return $this;
  61. }
  62. public function getEncoding(): EncodingInterface
  63. {
  64. return $this->encoding;
  65. }
  66. public function setEncoding(Encoding $encoding): self
  67. {
  68. $this->encoding = $encoding;
  69. return $this;
  70. }
  71. public function getErrorCorrectionLevel(): ErrorCorrectionLevelInterface
  72. {
  73. return $this->errorCorrectionLevel;
  74. }
  75. public function setErrorCorrectionLevel(ErrorCorrectionLevelInterface $errorCorrectionLevel): self
  76. {
  77. $this->errorCorrectionLevel = $errorCorrectionLevel;
  78. return $this;
  79. }
  80. public function getSize(): int
  81. {
  82. return $this->size;
  83. }
  84. public function setSize(int $size): self
  85. {
  86. $this->size = $size;
  87. return $this;
  88. }
  89. public function getMargin(): int
  90. {
  91. return $this->margin;
  92. }
  93. public function setMargin(int $margin): self
  94. {
  95. $this->margin = $margin;
  96. return $this;
  97. }
  98. public function getRoundBlockSizeMode(): RoundBlockSizeModeInterface
  99. {
  100. return $this->roundBlockSizeMode;
  101. }
  102. public function setRoundBlockSizeMode(RoundBlockSizeModeInterface $roundBlockSizeMode): self
  103. {
  104. $this->roundBlockSizeMode = $roundBlockSizeMode;
  105. return $this;
  106. }
  107. public function getForegroundColor(): ColorInterface
  108. {
  109. return $this->foregroundColor;
  110. }
  111. public function setForegroundColor(ColorInterface $foregroundColor): self
  112. {
  113. $this->foregroundColor = $foregroundColor;
  114. return $this;
  115. }
  116. public function getBackgroundColor(): ColorInterface
  117. {
  118. return $this->backgroundColor;
  119. }
  120. public function setBackgroundColor(ColorInterface $backgroundColor): self
  121. {
  122. $this->backgroundColor = $backgroundColor;
  123. return $this;
  124. }
  125. }