| 1 |
<?php |
| 2 |
/** |
| 3 |
* Class QRStringJSON |
| 4 |
* |
| 5 |
* @created 25.10.2023 |
| 6 |
* @author smiley <[email protected]> |
| 7 |
* @copyright 2023 smiley |
| 8 |
* @license MIT |
| 9 |
* |
| 10 |
* @noinspection PhpComposerExtensionStubsInspection |
| 11 |
*/ |
| 12 |
declare(strict_types=1); |
| 13 |
|
| 14 |
namespace chillerlan\QRCode\Output; |
| 15 |
|
| 16 |
use JsonException; |
| 17 |
use function json_encode; |
| 18 |
|
| 19 |
/** |
| 20 |
* JSON Output |
| 21 |
* |
| 22 |
* @method string getModuleValue(int $M_TYPE) |
| 23 |
* |
| 24 |
* @phpstan-type Module array{x: int, dark: bool, layer: string, value: string} |
| 25 |
*/ |
| 26 |
class QRStringJSON extends QROutputAbstract{ |
| 27 |
use CssColorModuleValueTrait; |
| 28 |
|
| 29 |
final public const MIME_TYPE = 'application/json'; |
| 30 |
final public const SCHEMA = 'https://raw.githubusercontent.com/chillerlan/php-qrcode/main/src/Output/qrcode.schema.json'; |
| 31 |
|
| 32 |
/** |
| 33 |
* @inheritDoc |
| 34 |
* |
| 35 |
* @return int[] |
| 36 |
*/ |
| 37 |
protected function getOutputDimensions():array{ |
| 38 |
return [$this->moduleCount, $this->moduleCount]; |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* @inheritDoc |
| 43 |
* @throws \JsonException |
| 44 |
*/ |
| 45 |
public function dump(string|null $file = null):string{ |
| 46 |
[$width, $height] = $this->getOutputDimensions(); |
| 47 |
$version = $this->matrix->getVersion(); |
| 48 |
$dimension = $version->getDimension(); |
| 49 |
|
| 50 |
$json = [ |
| 51 |
'$schema' => $this::SCHEMA, |
| 52 |
'qrcode' => [ |
| 53 |
'version' => $version->getVersionNumber(), |
| 54 |
'eccLevel' => (string)$this->matrix->getEccLevel(), |
| 55 |
'matrix' => [ |
| 56 |
'size' => $dimension, |
| 57 |
'quietzoneSize' => (int)(($this->moduleCount - $dimension) / 2), |
| 58 |
'maskPattern' => $this->matrix->getMaskPattern()->getPattern(), |
| 59 |
'width' => $width, |
| 60 |
'height' => $height, |
| 61 |
'rows' => [], |
| 62 |
], |
| 63 |
], |
| 64 |
]; |
| 65 |
|
| 66 |
foreach($this->matrix->getMatrix() as $y => $row){ |
| 67 |
$matrixRow = $this->row($y, $row); |
| 68 |
|
| 69 |
if($matrixRow !== null){ |
| 70 |
$json['qrcode']['matrix']['rows'][] = $matrixRow; |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
$data = json_encode($json, $this->options->jsonFlags); |
| 75 |
|
| 76 |
if($data === false){ |
| 77 |
throw new JsonException('error while encoding JSON'); |
| 78 |
} |
| 79 |
|
| 80 |
$this->saveToFile($data, $file); |
| 81 |
|
| 82 |
return $data; |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* Creates an array element for a matrix row |
| 87 |
* |
| 88 |
* @param int[] $row |
| 89 |
* @phpstan-return array{y: int, modules: array<int, Module>} |
| 90 |
*/ |
| 91 |
protected function row(int $y, array $row):array|null{ |
| 92 |
$matrixRow = ['y' => $y, 'modules' => []]; |
| 93 |
|
| 94 |
foreach($row as $x => $M_TYPE){ |
| 95 |
$module = $this->module($x, $y, $M_TYPE); |
| 96 |
|
| 97 |
if($module !== null){ |
| 98 |
$matrixRow['modules'][] = $module; |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
if($matrixRow['modules'] !== []){ |
| 103 |
return $matrixRow; |
| 104 |
} |
| 105 |
|
| 106 |
// skip empty rows |
| 107 |
return null; |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* Creates an array element for a single module |
| 112 |
* |
| 113 |
* @phpstan-return Module |
| 114 |
*/ |
| 115 |
protected function module(int $x, int $y, int $M_TYPE):array|null{ |
| 116 |
$isDark = $this->matrix->isDark($M_TYPE); |
| 117 |
|
| 118 |
if(!$this->drawLightModules && !$isDark){ |
| 119 |
return null; |
| 120 |
} |
| 121 |
|
| 122 |
return [ |
| 123 |
'x' => $x, |
| 124 |
'dark' => $isDark, |
| 125 |
'layer' => ($this::LAYERNAMES[$M_TYPE] ?? ''), |
| 126 |
'value' => $this->getModuleValue($M_TYPE), |
| 127 |
]; |
| 128 |
} |
| 129 |
|
| 130 |
} |