Test Failed
Push — master ( 1c3827...8aff3a )
by smiley
03:00
created

QRMarkup::setModuleValues()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 17

Duplication

Lines 17
Ratio 100 %

Importance

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