Debian Codegraph @

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

raw
1 <?php
2 /**
3 * Class QRMarkupXML
4 *
5 * @created 01.05.2024
6 * @author smiley <[email protected]>
7 * @copyright 2024 smiley
8 * @license MIT
9 *
10 * @noinspection PhpComposerExtensionStubsInspection
11 */
12 declare(strict_types=1);
13
14 namespace chillerlan\QRCode\Output;
15
16 use DOMDocument;
17 use DOMElement;
18 use function sprintf;
19
20 /**
21 * XML/XSLT output
22 */
23 class QRMarkupXML extends QRMarkup{
24
25 final public const MIME_TYPE = 'application/xml';
26 final public const SCHEMA = 'https://raw.githubusercontent.com/chillerlan/php-qrcode/main/src/Output/qrcode.schema.xsd';
27
28 protected DOMDocument $dom;
29
30 /**
31 * @inheritDoc
32 * @return int[]
33 */
34 protected function getOutputDimensions():array{
35 return [$this->moduleCount, $this->moduleCount];
36 }
37
38 /**
39 * @inheritDoc
40 * @throws \chillerlan\QRCode\Output\QRCodeOutputException
41 */
42 protected function createMarkup(bool $saveToFile):string{
43 /** @noinspection PhpComposerExtensionStubsInspection */
44 $this->dom = new DOMDocument(encoding: 'UTF-8');
45 $this->dom->formatOutput = true;
46
47 if($this->options->xmlStylesheet !== null){
48 $stylesheet = sprintf('type="text/xsl" href="%s"', $this->options->xmlStylesheet);
49 $xslt = $this->dom->createProcessingInstruction('xml-stylesheet', $stylesheet);
50
51 $this->dom->appendChild($xslt);
52 }
53
54 $root = $this->dom->createElement('qrcode');
55
56 $root->setAttribute('xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance');
57 $root->setAttribute('xsi:noNamespaceSchemaLocation', $this::SCHEMA);
58 $root->setAttribute('version', (string)$this->matrix->getVersion());
59 $root->setAttribute('eccLevel', (string)$this->matrix->getEccLevel());
60 $root->appendChild($this->createMatrix());
61
62 $this->dom->appendChild($root);
63
64 $xml = $this->dom->saveXML();
65
66 if($xml === false){
67 throw new QRCodeOutputException('XML error');
68 }
69
70 return $xml;
71 }
72
73 /**
74 * Creates the matrix element
75 */
76 protected function createMatrix():DOMElement{
77 [$width, $height] = $this->getOutputDimensions();
78 $matrix = $this->dom->createElement('matrix');
79 $dimension = $this->matrix->getVersion()->getDimension();
80
81 $matrix->setAttribute('size', (string)$dimension);
82 $matrix->setAttribute('quietzoneSize', (string)(int)(($this->moduleCount - $dimension) / 2));
83 $matrix->setAttribute('maskPattern', (string)$this->matrix->getMaskPattern()->getPattern());
84 $matrix->setAttribute('width', (string)$width);
85 $matrix->setAttribute('height', (string)$height);
86
87 foreach($this->matrix->getMatrix() as $y => $row){
88 $matrixRow = $this->row($y, $row);
89
90 if($matrixRow !== null){
91 $matrix->appendChild($matrixRow);
92 }
93 }
94
95 return $matrix;
96 }
97
98 /**
99 * Creates a DOM element for a matrix row
100 *
101 * @param int[] $row
102 */
103 protected function row(int $y, array $row):DOMElement|null{
104 $matrixRow = $this->dom->createElement('row');
105
106 $matrixRow->setAttribute('y', (string)$y);
107
108 foreach($row as $x => $M_TYPE){
109 $module = $this->module($x, $y, $M_TYPE);
110
111 if($module !== null){
112 $matrixRow->appendChild($module);
113 }
114
115 }
116
117 if($matrixRow->childElementCount > 0){
118 return $matrixRow;
119 }
120
121 // skip empty rows
122 return null;
123 }
124
125 /**
126 * Creates a DOM element for a single module
127 */
128 protected function module(int $x, int $y, int $M_TYPE):DOMElement|null{
129 $isDark = $this->matrix->isDark($M_TYPE);
130
131 if(!$this->drawLightModules && !$isDark){
132 return null;
133 }
134
135 $module = $this->dom->createElement('module');
136
137 $module->setAttribute('x', (string)$x);
138 $module->setAttribute('dark', (($isDark) ? 'true' : 'false'));
139 $module->setAttribute('layer', ($this::LAYERNAMES[$M_TYPE] ?? ''));
140 $module->setAttribute('value', (string)$this->getModuleValue($M_TYPE));
141
142 return $module;
143 }
144
145 }