|
|
@@ -18,16 +18,52 @@ use function json_encode;
|
|
|
*
|
|
|
*/
|
|
|
class QRStringJSON extends QROutputAbstract{
|
|
|
+ use CssColorModuleValueTrait;
|
|
|
|
|
|
final public const MIME_TYPE = 'application/json';
|
|
|
+ final public const SCHEMA = 'https://raw.githubusercontent.com/chillerlan/php-qrcode/main/src/Output/qrcode.schema.json';
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @inheritDoc
|
|
|
+ */
|
|
|
+ protected function getOutputDimensions():array{
|
|
|
+ return [$this->moduleCount, $this->moduleCount];
|
|
|
+ }
|
|
|
|
|
|
/**
|
|
|
* @inheritDoc
|
|
|
* @throws \JsonException
|
|
|
*/
|
|
|
public function dump(string $file = null):string{
|
|
|
- $matrix = $this->matrix->getMatrix($this->options->jsonAsBooleans);
|
|
|
- $data = json_encode($matrix, $this->options->jsonFlags);;
|
|
|
+ [$width, $height] = $this->getOutputDimensions();
|
|
|
+ $version = $this->matrix->getVersion();
|
|
|
+ $dimension = $version->getDimension();
|
|
|
+
|
|
|
+ $json = [
|
|
|
+ '$schema' => $this::SCHEMA,
|
|
|
+ 'qrcode' => [
|
|
|
+ 'version' => $version->getVersionNumber(),
|
|
|
+ 'eccLevel' => (string)$this->matrix->getEccLevel(),
|
|
|
+ 'matrix' => [
|
|
|
+ 'size' => $dimension,
|
|
|
+ 'quietzoneSize' => (int)(($this->moduleCount - $dimension) / 2),
|
|
|
+ 'maskPattern' => $this->matrix->getMaskPattern()->getPattern(),
|
|
|
+ 'width' => $width,
|
|
|
+ 'height' => $height,
|
|
|
+ 'rows' => [],
|
|
|
+ ],
|
|
|
+ ],
|
|
|
+ ];
|
|
|
+
|
|
|
+ foreach($this->matrix->getMatrix() as $y => $row){
|
|
|
+ $matrixRow = $this->row($y, $row);
|
|
|
+
|
|
|
+ if($matrixRow !== null){
|
|
|
+ $json['qrcode']['matrix']['rows'][] = $matrixRow;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ $data = json_encode($json, $this->options->jsonFlags);;
|
|
|
|
|
|
$this->saveToFile($data, $file);
|
|
|
|
|
|
@@ -35,33 +71,43 @@ class QRStringJSON extends QROutputAbstract{
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * unused - required by interface
|
|
|
- *
|
|
|
- * @inheritDoc
|
|
|
- * @codeCoverageIgnore
|
|
|
+ * Creates an array element for a matrix row
|
|
|
*/
|
|
|
- protected function prepareModuleValue(mixed $value):string{
|
|
|
- return '';
|
|
|
- }
|
|
|
+ protected function row(int $y, array $row):array|null{
|
|
|
+ $matrixRow = ['y' => $y, 'modules' => []];
|
|
|
|
|
|
- /**
|
|
|
- * unused - required by interface
|
|
|
- *
|
|
|
- * @inheritDoc
|
|
|
- * @codeCoverageIgnore
|
|
|
- */
|
|
|
- protected function getDefaultModuleValue(bool $isDark):string{
|
|
|
- return '';
|
|
|
+ foreach($row as $x => $M_TYPE){
|
|
|
+ $module = $this->module($x, $y, $M_TYPE);
|
|
|
+
|
|
|
+ if($module !== null){
|
|
|
+ $matrixRow['modules'][] = $module;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if(!empty($matrixRow['modules'])){
|
|
|
+ return $matrixRow;
|
|
|
+ }
|
|
|
+
|
|
|
+ // skip empty rows
|
|
|
+ return null;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * unused - required by interface
|
|
|
- *
|
|
|
- * @inheritDoc
|
|
|
- * @codeCoverageIgnore
|
|
|
+ * Creates an array element for a single module
|
|
|
*/
|
|
|
- public static function moduleValueIsValid(mixed $value):bool{
|
|
|
- return true;
|
|
|
+ protected function module(int $x, int $y, int $M_TYPE):array|null{
|
|
|
+ $isDark = $this->matrix->isDark($M_TYPE);
|
|
|
+
|
|
|
+ if(!$this->drawLightModules && !$isDark){
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ return [
|
|
|
+ 'x' => $x,
|
|
|
+ 'dark' => $isDark,
|
|
|
+ 'layer' => ($this::LAYERNAMES[$M_TYPE] ?? ''),
|
|
|
+ 'value' => $this->getModuleValue($M_TYPE),
|
|
|
+ ];
|
|
|
}
|
|
|
|
|
|
}
|