Completed
Push — master ( 645168...459db1 )
by smiley
03:19
created

QRMarkup::toHTML()   B

Complexity

Conditions 5
Paths 8

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 10
c 0
b 0
f 0
nc 8
nop 0
dl 0
loc 19
rs 8.8571
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
	 * @return string
24
	 */
25 View Code Duplication
	public function dump(){
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...
26
27
		$data = call_user_func([$this, $this->outputMode ?? QRCode::OUTPUT_MARKUP_SVG]);
28
29
		if($this->options->cachefile !== null){
30
			$this->saveToFile($data);
31
		}
32
33
		return $data;
34
	}
35
36
	/**
37
	 * @return string|bool
38
	 */
39
	protected function html(){
40
		$html = '';
41
42
		foreach($this->matrix->matrix() as $row){
43
			$html .= '<div>';
44
45
			foreach($row as $pixel){
46
				$html .= '<span style="background: '.($this->options->moduleValues[$pixel] ?: 'lightgrey').';"></span>';
47
			}
48
49
			$html .= '</div>'.$this->options->eol;
50
		}
51
52
		if($this->options->cachefile){
53
			return '<!DOCTYPE html><head><meta charset="UTF-8"></head><body>'.$this->options->eol.$html.'</body>';
54
		}
55
56
		return $html;
57
	}
58
59
	/**
60
	 * @link https://github.com/codemasher/php-qrcode/pull/5
61
	 *
62
	 * @return string|bool
63
	 */
64
	protected function svg(){
65
		$scale  = $this->options->scale;
66
		$length = $this->moduleCount * $scale;
67
		$matrix = $this->matrix->matrix();
68
69
		$svg = '<svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="'.$length.'px" height="'.$length.'px">'
70
		       .$this->options->eol
71
		       .'<defs>'.$this->options->svgDefs.'</defs>'
72
		       .$this->options->eol;
73
74
		foreach($this->options->moduleValues as $M_TYPE => $value){
75
76
			// fallback
77
			if(is_bool($value)){
78
				$value = $value ? '#000' : '#fff';
79
			}
80
81
			$path = '';
82
83
			foreach($matrix as $y => $row){
84
				//we'll combine active blocks within a single row as a lightweight compression technique
85
				$start = null;
86
				$count = 0;
87
88
				foreach($row as $x => $module){
89
90
					if($module === $M_TYPE){
91
						$count++;
92
93
						if($start === null){
94
							$start = $x * $scale;
95
						}
96
97
						if($row[$x + 1] ?? false){
98
							continue;
99
						}
100
					}
101
102
					if($count > 0){
103
						$len = $count * $scale;
104
						$path .= 'M' .$start. ' ' .($y * $scale). ' h'.$len.' v'.$scale.' h-'.$len.'Z ';
105
106
						// reset count
107
						$count = 0;
108
						$start = null;
109
					}
110
111
				}
112
113
			}
114
115
			if(!empty($path)){
116
				$svg .= '<path class="qr-'.$M_TYPE.' '.$this->options->cssClass.'" stroke="transparent" fill="'.$value.'" fill-opacity="'.$this->options->svgOpacity.'" d="'.$path.'" />';
117
			}
118
119
		}
120
121
		// close svg
122
		$svg .= '</svg>'.$this->options->eol;
123
124
		// if saving to file, append the correct headers
125
		if($this->options->cachefile){
126
			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;
127
		}
128
129
		return $svg;
130
	}
131
132
}
133