| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Class QRStringText |
| 5 |
* |
| 6 |
* @created 25.10.2023 |
| 7 |
* @author smiley <smiley@chillerlan.net> |
| 8 |
* @copyright 2023 smiley |
| 9 |
* @license MIT |
| 10 |
*/ |
| 11 |
namespace WCPOS\Vendor\chillerlan\QRCode\Output; |
| 12 |
|
| 13 |
use function array_map, implode, is_string, max, min, sprintf; |
| 14 |
/** |
| 15 |
* |
| 16 |
*/ |
| 17 |
class QRStringText extends QROutputAbstract |
| 18 |
{ |
| 19 |
public const MIME_TYPE = 'text/plain'; |
| 20 |
/** |
| 21 |
* @inheritDoc |
| 22 |
*/ |
| 23 |
public static function moduleValueIsValid($value) : bool |
| 24 |
{ |
| 25 |
return is_string($value); |
| 26 |
} |
| 27 |
/** |
| 28 |
* @inheritDoc |
| 29 |
*/ |
| 30 |
protected function prepareModuleValue($value) : string |
| 31 |
{ |
| 32 |
return $value; |
| 33 |
} |
| 34 |
/** |
| 35 |
* @inheritDoc |
| 36 |
*/ |
| 37 |
protected function getDefaultModuleValue(bool $isDark) : string |
| 38 |
{ |
| 39 |
return $isDark ? '██' : '░░'; |
| 40 |
} |
| 41 |
/** |
| 42 |
* @inheritDoc |
| 43 |
*/ |
| 44 |
public function dump(?string $file = null) : string |
| 45 |
{ |
| 46 |
$lines = []; |
| 47 |
$linestart = $this->options->textLineStart; |
| 48 |
foreach ($this->matrix->getMatrix() as $row) { |
| 49 |
$lines[] = $linestart . implode('', array_map([$this, 'getModuleValue'], $row)); |
| 50 |
} |
| 51 |
$data = implode($this->eol, $lines); |
| 52 |
$this->saveToFile($data, $file); |
| 53 |
return $data; |
| 54 |
} |
| 55 |
/** |
| 56 |
* a little helper to create a proper ANSI 8-bit color escape sequence |
| 57 |
* |
| 58 |
* @see https://en.wikipedia.org/wiki/ANSI_escape_code#8-bit |
| 59 |
* @see https://en.wikipedia.org/wiki/Block_Elements |
| 60 |
* |
| 61 |
* @codeCoverageIgnore |
| 62 |
*/ |
| 63 |
public static function ansi8(string $str, int $color, ?bool $background = null) : string |
| 64 |
{ |
| 65 |
$color = max(0, min($color, 255)); |
| 66 |
$background = $background === \true ? 48 : 38; |
| 67 |
return sprintf("\x1b[%s;5;%sm%s\x1b[0m", $background, $color, $str); |
| 68 |
} |
| 69 |
} |
| 70 |
|