-
-
Notifications
You must be signed in to change notification settings - Fork 771
Expand file tree
/
Copy pathConsoleResult.php
More file actions
71 lines (59 loc) · 2.03 KB
/
Copy pathConsoleResult.php
File metadata and controls
71 lines (59 loc) · 2.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<?php
declare(strict_types=1);
namespace Endroid\QrCode\Writer\Result;
use Endroid\QrCode\Color\ColorInterface;
use Endroid\QrCode\Matrix\MatrixInterface;
final class ConsoleResult extends AbstractResult
{
private const array TWO_BLOCKS = [
0 => ' ',
1 => "\xe2\x96\x80",
2 => "\xe2\x96\x84",
3 => "\xe2\x96\x88",
];
private string $colorEscapeCode;
public function __construct(
MatrixInterface $matrix,
ColorInterface $foreground,
ColorInterface $background,
) {
parent::__construct($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()
);
}
#[\Override]
public function getMimeType(): string
{
return 'text/plain';
}
#[\Override]
public function getString(): string
{
$matrix = $this->getMatrix();
$side = $matrix->getBlockCount();
$marginLeft = $this->colorEscapeCode.self::TWO_BLOCKS[0].self::TWO_BLOCKS[0];
$marginRight = self::TWO_BLOCKS[0].self::TWO_BLOCKS[0]."\e[0m".PHP_EOL;
$marginVertical = $marginLeft.str_repeat(self::TWO_BLOCKS[0], $side).$marginRight;
$qrCodeString = $marginVertical;
for ($rowIndex = 0; $rowIndex < $side; $rowIndex += 2) {
$qrCodeString .= $marginLeft;
for ($columnIndex = 0; $columnIndex < $side; ++$columnIndex) {
$combined = $matrix->getBlockValue($rowIndex, $columnIndex);
if ($rowIndex + 1 < $side) {
$combined |= $matrix->getBlockValue($rowIndex + 1, $columnIndex) << 1;
}
$qrCodeString .= self::TWO_BLOCKS[$combined];
}
$qrCodeString .= $marginRight;
}
$qrCodeString .= $marginVertical;
return $qrCodeString;
}
}