Skip to content

Commit

Permalink
Merge pull request #378 from Trismegiste/feature-writer-for-command-l…
Browse files Browse the repository at this point in the history
…ine-interface

Writer and Result classes for printing a QR-Code on a console
  • Loading branch information
endroid authored Aug 21, 2022
2 parents fbb9906 + 70a02f5 commit 1aaaa51
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 0 deletions.
29 changes: 29 additions & 0 deletions src/Writer/ConsoleWriter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

namespace Endroid\QrCode\Writer;

use Endroid\QrCode\Bacon\MatrixFactory;
use Endroid\QrCode\Label\LabelInterface;
use Endroid\QrCode\Logo\LogoInterface;
use Endroid\QrCode\QrCodeInterface;
use Endroid\QrCode\Writer\Result\ConsoleResult;
use Endroid\QrCode\Writer\Result\ResultInterface;

/**
* Writer of QR Code for CLI.
*/
class ConsoleWriter implements WriterInterface
{
/**
* {@inheritDoc}
*/
public function write(QrCodeInterface $qrCode, LogoInterface $logo = null, LabelInterface $label = null, $options = []): ResultInterface
{
$matrixFactory = new MatrixFactory();
$matrix = $matrixFactory->create($qrCode);

return new ConsoleResult($matrix, $qrCode->getForegroundColor(), $qrCode->getBackgroundColor());
}
}
73 changes: 73 additions & 0 deletions src/Writer/Result/ConsoleResult.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

declare(strict_types=1);

namespace Endroid\QrCode\Writer\Result;

use Endroid\QrCode\Color\ColorInterface;
use Endroid\QrCode\Matrix\MatrixInterface;

/**
* Implementation of ResultInterface for printing a QR-Code on command line interface.
*/
class ConsoleResult extends AbstractResult
{
protected MatrixInterface $matrix;
protected string $colorEscapeCode;

public const twoblocks = [
0 => ' ',
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();
}
}
3 changes: 3 additions & 0 deletions tests/QrCodeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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'];
}

/**
Expand Down

0 comments on commit 1aaaa51

Please sign in to comment.