Debian Codegraph @

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

raw
1 <?php
2 /**
3 * Class QRMarkupHTML
4 *
5 * @created 06.06.2022
6 * @author smiley <[email protected]>
7 * @copyright 2022 smiley
8 * @license MIT
9 */
10 declare(strict_types=1);
11
12 namespace chillerlan\QRCode\Output;
13
14 use function implode, sprintf;
15
16 /**
17 * HTML output (a cheap markup substitute when SVG is not available or not an option)
18 */
19 class QRMarkupHTML extends QRMarkup{
20
21 final public const MIME_TYPE = 'text/html';
22
23 protected function createMarkup(bool $saveToFile):string{
24 $rows = [];
25 $cssClass = $this->getCssClass();
26
27 foreach($this->matrix->getMatrix() as $row){
28 $element = '<span style="background: %s;"></span>';
29 $modules = array_map(fn(int $M_TYPE):string => sprintf($element, $this->getModuleValue($M_TYPE)), $row);
30
31 $rows[] = sprintf('<div>%s</div>%s', implode('', $modules), $this->eol);
32 }
33
34 $html = sprintf('<div class="%1$s">%3$s%2$s</div>%3$s', $cssClass, implode('', $rows), $this->eol);
35
36 // wrap the snippet into a body when saving to file
37 if($saveToFile){
38 $html = sprintf(
39 '<!DOCTYPE html><html lang="none">%2$s<head>%2$s<meta charset="UTF-8">%2$s'.
40 '<title>QR Code</title></head>%2$s<body>%1$s</body>%2$s</html>',
41 $html,
42 $this->eol,
43 );
44 }
45
46 return $html;
47 }
48
49 }