-
-
Notifications
You must be signed in to change notification settings - Fork 729
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #378 from Trismegiste/feature-writer-for-command-l…
…ine-interface Writer and Result classes for printing a QR-Code on a console
- Loading branch information
Showing
3 changed files
with
105 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters