| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Class QRString |
| 5 |
* |
| 6 |
* @created 05.12.2015 |
| 7 |
* @author Smiley <smiley@chillerlan.net> |
| 8 |
* @copyright 2015 Smiley |
| 9 |
* @license MIT |
| 10 |
* |
| 11 |
* @noinspection PhpComposerExtensionStubsInspection |
| 12 |
*/ |
| 13 |
namespace WCPOS\Vendor\chillerlan\QRCode\Output; |
| 14 |
|
| 15 |
use function implode, is_string, json_encode, max, min, sprintf; |
| 16 |
use const JSON_THROW_ON_ERROR; |
| 17 |
/** |
| 18 |
* Converts the matrix data into string types |
| 19 |
* |
| 20 |
* @deprecated 5.0.0 this class will be removed in future versions, use one of QRStringText or QRStringJSON instead |
| 21 |
*/ |
| 22 |
class QRString extends QROutputAbstract |
| 23 |
{ |
| 24 |
/** |
| 25 |
* @inheritDoc |
| 26 |
*/ |
| 27 |
public static function moduleValueIsValid($value) : bool |
| 28 |
{ |
| 29 |
return is_string($value); |
| 30 |
} |
| 31 |
/** |
| 32 |
* @inheritDoc |
| 33 |
*/ |
| 34 |
protected function prepareModuleValue($value) : string |
| 35 |
{ |
| 36 |
return $value; |
| 37 |
} |
| 38 |
/** |
| 39 |
* @inheritDoc |
| 40 |
*/ |
| 41 |
protected function getDefaultModuleValue(bool $isDark) : string |
| 42 |
{ |
| 43 |
return $isDark ? '██' : '░░'; |
| 44 |
} |
| 45 |
/** |
| 46 |
* @inheritDoc |
| 47 |
*/ |
| 48 |
public function dump(?string $file = null) : string |
| 49 |
{ |
| 50 |
switch ($this->options->outputType) { |
| 51 |
case QROutputInterface::STRING_TEXT: |
| 52 |
$data = $this->text(); |
| 53 |
break; |
| 54 |
case QROutputInterface::STRING_JSON: |
| 55 |
default: |
| 56 |
$data = $this->json(); |
| 57 |
} |
| 58 |
$this->saveToFile($data, $file); |
| 59 |
return $data; |
| 60 |
} |
| 61 |
/** |
| 62 |
* string output |
| 63 |
*/ |
| 64 |
protected function text() : string |
| 65 |
{ |
| 66 |
$lines = []; |
| 67 |
$linestart = $this->options->textLineStart; |
| 68 |
for ($y = 0; $y < $this->moduleCount; $y++) { |
| 69 |
$r = []; |
| 70 |
for ($x = 0; $x < $this->moduleCount; $x++) { |
| 71 |
$r[] = $this->getModuleValueAt($x, $y); |
| 72 |
} |
| 73 |
$lines[] = $linestart . implode('', $r); |
| 74 |
} |
| 75 |
return implode($this->eol, $lines); |
| 76 |
} |
| 77 |
/** |
| 78 |
* JSON output |
| 79 |
* |
| 80 |
* @throws \JsonException |
| 81 |
*/ |
| 82 |
protected function json() : string |
| 83 |
{ |
| 84 |
return json_encode($this->matrix->getMatrix($this->options->jsonAsBooleans), JSON_THROW_ON_ERROR); |
| 85 |
} |
| 86 |
// |
| 87 |
/** |
| 88 |
* a little helper to create a proper ANSI 8-bit color escape sequence |
| 89 |
* |
| 90 |
* @see https://en.wikipedia.org/wiki/ANSI_escape_code#8-bit |
| 91 |
* @see https://en.wikipedia.org/wiki/Block_Elements |
| 92 |
* |
| 93 |
* @codeCoverageIgnore |
| 94 |
*/ |
| 95 |
public static function ansi8(string $str, int $color, ?bool $background = null) : string |
| 96 |
{ |
| 97 |
$color = max(0, min($color, 255)); |
| 98 |
$background = $background === \true ? 48 : 38; |
| 99 |
return sprintf("\x1b[%s;5;%sm%s\x1b[0m", $background, $color, $str); |
| 100 |
} |
| 101 |
} |
| 102 |
|