Completed
Push — master ( ba81da...090bbe )
by smiley
02:25
created

QRMarkup::html()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
nc 6
nop 0
dl 0
loc 19
rs 9.6333
c 0
b 0
f 0
1
<?php
2
/**
3
 * Class QRMarkup
4
 *
5
 * @filesource   QRMarkup.php
6
 * @created      17.12.2016
7
 * @package      chillerlan\QRCode\Output
8
 * @author       Smiley <[email protected]>
9
 * @copyright    2016 Smiley
10
 * @license      MIT
11
 */
12
13
namespace chillerlan\QRCode\Output;
14
15
use chillerlan\QRCode\QRCode;
16
17
/**
18
 * Converts the matrix into markup types: HTML, SVG, ...
19
 */
20
class QRMarkup extends QROutputAbstract{
21
22
	/**
23
	 * @var string
24
	 */
25
	protected $defaultMode = QRCode::OUTPUT_MARKUP_SVG;
26
27
	/**
28
	 * @see \sprintf()
29
	 *
30
	 * @var string
31
	 */
32
	protected $svgHeader = '<svg xmlns="http://www.w3.org/2000/svg" version="1.1" class="qr-svg" style="width: 100%%; height: auto;" viewBox="0 0 %1$d %1$d">';
33
34
	/**
35
	 * @return void
36
	 */
37 View Code Duplication
	protected function setModuleValues():void{
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
38
39
		foreach($this::DEFAULT_MODULE_VALUES as $M_TYPE => $defaultValue){
40
			$v = $this->options->moduleValues[$M_TYPE] ?? null;
41
42
			if(!is_string($v)){
43
				$this->moduleValues[$M_TYPE] = $defaultValue
44
					? $this->options->markupDark
45
					: $this->options->markupLight;
46
			}
47
			else{
48
				$this->moduleValues[$M_TYPE] = trim(strip_tags($v), '\'"');
49
			}
50
51
		}
52
53
	}
54
55
	/**
56
	 * @return string
57
	 */
58
	protected function html():string{
59
		$html = '';
60
61
		foreach($this->matrix->matrix() as $row){
62
			$html .= '<div>';
63
64
			foreach($row as $M_TYPE){
65
				$html .= '<span style="background: '.$this->moduleValues[$M_TYPE].';"></span>';
66
			}
67
68
			$html .= '</div>'.$this->options->eol;
69
		}
70
71
		if($this->options->cachefile){
72
			return '<!DOCTYPE html><head><meta charset="UTF-8"></head><body>'.$this->options->eol.$html.'</body>';
73
		}
74
75
		return $html;
76
	}
77
78
	/**
79
	 * @link https://github.com/codemasher/php-qrcode/pull/5
80
	 *
81
	 * @return string
82
	 */
83
	protected function svg():string{
84
		$matrix = $this->matrix->matrix();
85
86
		$svg = sprintf($this->svgHeader, $this->options->svgViewBoxSize ?? $this->moduleCount)
87
		       .$this->options->eol
88
		       .'<defs>'.$this->options->svgDefs.'</defs>'
89
		       .$this->options->eol;
90
91
		foreach($this->moduleValues as $M_TYPE => $value){
92
			$path = '';
93
94
			foreach($matrix as $y => $row){
95
				//we'll combine active blocks within a single row as a lightweight compression technique
96
				$start = null;
97
				$count = 0;
98
99
				foreach($row as $x => $module){
100
101
					if($module === $M_TYPE){
102
						$count++;
103
104
						if($start === null){
105
							$start = $x;
106
						}
107
108
						if($row[$x + 1] ?? false){
109
							continue;
110
						}
111
					}
112
113
					if($count > 0){
114
						$len = $count;
115
						$path .= 'M' .$start. ' ' .$y. ' h'.$len.' v1 h-'.$len.'Z ';
116
117
						// reset count
118
						$count = 0;
119
						$start = null;
120
					}
121
122
				}
123
124
			}
125
126
			if(!empty($path)){
127
				$svg .= '<path class="qr-'.$M_TYPE.' '.$this->options->cssClass.'" stroke="transparent" fill="'.$value.'" fill-opacity="'.$this->options->svgOpacity.'" d="'.$path.'" />';
128
			}
129
130
		}
131
132
		// close svg
133
		$svg .= '</svg>'.$this->options->eol;
134
135
		// if saving to file, append the correct headers
136
		if($this->options->cachefile){
137
			return '<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">'.$this->options->eol.$svg;
138
		}
139
140
		return $svg;
141
	}
142
143
}
144