diff --git a/src/Writer/ConsoleWriter.php b/src/Writer/ConsoleWriter.php new file mode 100644 index 0000000..92b8126 --- /dev/null +++ b/src/Writer/ConsoleWriter.php @@ -0,0 +1,29 @@ +create($qrCode); + + return new ConsoleResult($matrix, $qrCode->getForegroundColor(), $qrCode->getBackgroundColor()); + } +} diff --git a/src/Writer/Result/ConsoleResult.php b/src/Writer/Result/ConsoleResult.php new file mode 100644 index 0000000..98f5f04 --- /dev/null +++ b/src/Writer/Result/ConsoleResult.php @@ -0,0 +1,73 @@ + ' ', + 1 => "\xe2\x96\x80", + 2 => "\xe2\x96\x84", + 3 => "\xe2\x96\x88", + ]; + + /** + * Ctor. + */ + public function __construct(MatrixInterface $matrix, ColorInterface $foreground, ColorInterface $background) + { + $this->matrix = $matrix; + $this->colorEscapeCode = sprintf( + "\e[38;2;%d;%d;%dm\e[48;2;%d;%d;%dm", + $foreground->getRed(), + $foreground->getGreen(), + $foreground->getBlue(), + $background->getRed(), + $background->getGreen(), + $background->getBlue() + ); + } + + public function getMimeType(): string + { + return 'text/plain'; + } + + public function getString(): string + { + $side = $this->matrix->getBlockCount(); + $marginLeft = $this->colorEscapeCode.self::twoblocks[0].self::twoblocks[0]; + $marginRight = self::twoblocks[0].self::twoblocks[0]."\e[0m".PHP_EOL; + $marginVertical = $marginLeft.str_repeat(self::twoblocks[0], $side).$marginRight; + + ob_start(); + echo $marginVertical; // margin-top + + for ($rowIndex = 0; $rowIndex < $side; $rowIndex += 2) { + echo $marginLeft; // margin-left + for ($columnIndex = 0; $columnIndex < $side; ++$columnIndex) { + $combined = $this->matrix->getBlockValue($rowIndex, $columnIndex); + if (($rowIndex + 1) < $side) { + $combined |= $this->matrix->getBlockValue($rowIndex + 1, $columnIndex) << 1; + } + echo self::twoblocks[$combined]; + } + echo $marginRight; // margin-right + } + + echo $marginVertical; // margin-bottom + + return (string) ob_get_clean(); + } +} diff --git a/tests/QrCodeTest.php b/tests/QrCodeTest.php index dc0a361..a85c155 100644 --- a/tests/QrCodeTest.php +++ b/tests/QrCodeTest.php @@ -17,11 +17,13 @@ use Endroid\QrCode\RoundBlockSizeMode\RoundBlockSizeModeMargin; use Endroid\QrCode\RoundBlockSizeMode\RoundBlockSizeModeShrink; use Endroid\QrCode\Writer\BinaryWriter; +use Endroid\QrCode\Writer\ConsoleWriter; use Endroid\QrCode\Writer\DebugWriter; use Endroid\QrCode\Writer\EpsWriter; use Endroid\QrCode\Writer\PdfWriter; use Endroid\QrCode\Writer\PngWriter; use Endroid\QrCode\Writer\Result\BinaryResult; +use Endroid\QrCode\Writer\Result\ConsoleResult; use Endroid\QrCode\Writer\Result\DebugResult; use Endroid\QrCode\Writer\Result\EpsResult; use Endroid\QrCode\Writer\Result\PdfResult; @@ -79,6 +81,7 @@ public function writerProvider(): iterable yield [new PdfWriter(), PdfResult::class, 'application/pdf']; yield [new PngWriter(), PngResult::class, 'image/png']; yield [new SvgWriter(), SvgResult::class, 'image/svg+xml']; + yield [new ConsoleWriter(), ConsoleResult::class, 'text/plain']; } /**