|
18 | 18 | * |
19 | 19 | */ |
20 | 20 | class QRStringJSON extends QROutputAbstract{ |
| 21 | + use CssColorModuleValueTrait; |
21 | 22 |
|
22 | 23 | final public const MIME_TYPE = 'application/json'; |
| 24 | + final public const SCHEMA = 'https://raw.githubusercontent.com/chillerlan/php-qrcode/main/src/Output/qrcode.schema.json'; |
| 25 | + |
| 26 | + /** |
| 27 | + * @inheritDoc |
| 28 | + */ |
| 29 | + protected function getOutputDimensions():array{ |
| 30 | + return [$this->moduleCount, $this->moduleCount]; |
| 31 | + } |
23 | 32 |
|
24 | 33 | /** |
25 | 34 | * @inheritDoc |
26 | 35 | * @throws \JsonException |
27 | 36 | */ |
28 | 37 | public function dump(string $file = null):string{ |
29 | | - $matrix = $this->matrix->getMatrix($this->options->jsonAsBooleans); |
30 | | - $data = json_encode($matrix, $this->options->jsonFlags);; |
| 38 | + [$width, $height] = $this->getOutputDimensions(); |
| 39 | + $version = $this->matrix->getVersion(); |
| 40 | + $dimension = $version->getDimension(); |
| 41 | + |
| 42 | + $json = [ |
| 43 | + '$schema' => $this::SCHEMA, |
| 44 | + 'qrcode' => [ |
| 45 | + 'version' => $version->getVersionNumber(), |
| 46 | + 'eccLevel' => (string)$this->matrix->getEccLevel(), |
| 47 | + 'matrix' => [ |
| 48 | + 'size' => $dimension, |
| 49 | + 'quietzoneSize' => (int)(($this->moduleCount - $dimension) / 2), |
| 50 | + 'maskPattern' => $this->matrix->getMaskPattern()->getPattern(), |
| 51 | + 'width' => $width, |
| 52 | + 'height' => $height, |
| 53 | + 'rows' => [], |
| 54 | + ], |
| 55 | + ], |
| 56 | + ]; |
| 57 | + |
| 58 | + foreach($this->matrix->getMatrix() as $y => $row){ |
| 59 | + $matrixRow = $this->row($y, $row); |
| 60 | + |
| 61 | + if($matrixRow !== null){ |
| 62 | + $json['qrcode']['matrix']['rows'][] = $matrixRow; |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + $data = json_encode($json, $this->options->jsonFlags);; |
31 | 67 |
|
32 | 68 | $this->saveToFile($data, $file); |
33 | 69 |
|
34 | 70 | return $data; |
35 | 71 | } |
36 | 72 |
|
37 | 73 | /** |
38 | | - * unused - required by interface |
39 | | - * |
40 | | - * @inheritDoc |
41 | | - * @codeCoverageIgnore |
| 74 | + * Creates an array element for a matrix row |
42 | 75 | */ |
43 | | - protected function prepareModuleValue(mixed $value):string{ |
44 | | - return ''; |
45 | | - } |
| 76 | + protected function row(int $y, array $row):array|null{ |
| 77 | + $matrixRow = ['y' => $y, 'modules' => []]; |
46 | 78 |
|
47 | | - /** |
48 | | - * unused - required by interface |
49 | | - * |
50 | | - * @inheritDoc |
51 | | - * @codeCoverageIgnore |
52 | | - */ |
53 | | - protected function getDefaultModuleValue(bool $isDark):string{ |
54 | | - return ''; |
| 79 | + foreach($row as $x => $M_TYPE){ |
| 80 | + $module = $this->module($x, $y, $M_TYPE); |
| 81 | + |
| 82 | + if($module !== null){ |
| 83 | + $matrixRow['modules'][] = $module; |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + if(!empty($matrixRow['modules'])){ |
| 88 | + return $matrixRow; |
| 89 | + } |
| 90 | + |
| 91 | + // skip empty rows |
| 92 | + return null; |
55 | 93 | } |
56 | 94 |
|
57 | 95 | /** |
58 | | - * unused - required by interface |
59 | | - * |
60 | | - * @inheritDoc |
61 | | - * @codeCoverageIgnore |
| 96 | + * Creates an array element for a single module |
62 | 97 | */ |
63 | | - public static function moduleValueIsValid(mixed $value):bool{ |
64 | | - return true; |
| 98 | + protected function module(int $x, int $y, int $M_TYPE):array|null{ |
| 99 | + $isDark = $this->matrix->isDark($M_TYPE); |
| 100 | + |
| 101 | + if(!$this->drawLightModules && !$isDark){ |
| 102 | + return null; |
| 103 | + } |
| 104 | + |
| 105 | + return [ |
| 106 | + 'x' => $x, |
| 107 | + 'dark' => $isDark, |
| 108 | + 'layer' => ($this::LAYERNAMES[$M_TYPE] ?? ''), |
| 109 | + 'value' => $this->getModuleValue($M_TYPE), |
| 110 | + ]; |
65 | 111 | } |
66 | 112 |
|
67 | 113 | } |
0 commit comments