Debian Codegraph @

vendor/chillerlan/php-qrcode/src/Output/QRStringText.php

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