Completed
Push — master ( 7df5a4...fcf7fd )
by smiley
02:10
created

QRMarkup::dump()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 16
Code Lines 9

Duplication

Lines 16
Ratio 100 %

Importance

Changes 0
Metric Value
cc 5
eloc 9
c 0
b 0
f 0
nc 5
nop 0
dl 16
loc 16
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
0 ignored issues
show
Documentation introduced by
Should the return type not be string|boolean?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
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
		if($this->options->cachefile !== null && !is_writable(dirname($this->options->cachefile))){
28
			throw new QRCodeOutputException('Could not write data to cache file: '.$this->options->cachefile);
29
		}
30
31
		$data = $this->options->outputType === QRCode::OUTPUT_MARKUP_HTML
32
			? $this->toHTML()
33
			: $this->toSVG();
34
35
		if($this->options->cachefile !== null){
36
			$this->saveToFile($data);
0 ignored issues
show
Bug introduced by
It seems like $data defined by $this->options->outputTy...HTML() : $this->toSVG() on line 31 can also be of type boolean; however, chillerlan\QRCode\Output...tAbstract::saveToFile() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
37
		}
38
39
		return $data;
40
	}
41
42
	/**
43
	 * @return string|bool
44
	 */
45
	protected function toHTML(){
46
		$html = '';
47
48
		foreach($this->matrix->matrix() as $row){
49
			$html .= '<div>';
50
51
			foreach($row as $pixel){
52
				$html .= '<span style="background: '.($this->options->moduleValues[$pixel] ?: 'lightgrey').';"></span>';
53
			}
54
55
			$html .= '</div>'.$this->options->eol;
56
		}
57
58
		if($this->options->cachefile){
59
			return '<!DOCTYPE html><head><meta charset="UTF-8"></head><body>'.$this->options->eol.$html.'</body>';
60
		}
61
62
		return $html;
63
	}
64
65
	/**
66
	 * @link https://github.com/codemasher/php-qrcode/pull/5
67
	 *
68
	 * @return string|bool
69
	 */
70
	protected function toSVG(){
71
		$length = ($this->moduleCount + ($this->options->addQuietzone ? 8 : 0)) * $this->options->scale;
72
73
		// svg header
74
		$svg = '<svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="'.$length.'" height="'.$length.'" viewBox="0 0 '.$length.' '.$length.'">'.$this->options->eol.
75
		       '<defs><style>rect{shape-rendering:crispEdges}</style></defs>'.$this->options->eol;
76
77
		// @todo: optimize -> see https://github.com/alexeyten/qr-image/blob/master/lib/vector.js
78
		foreach($this->options->moduleValues as $key => $value){
79
80
			// fallback
81
			if(is_bool($value)){
82
				$value = $value ? '#000' : '#fff';
83
			}
84
85
			// svg body
86
			foreach($this->matrix->matrix() as $y => $row){
87
				//we'll combine active blocks within a single row as a lightweight compression technique
88
				$from  = -1;
89
				$count = 0;
90
91
				foreach($row as $x => $pixel){
92
					if($pixel === $key){
93
						$count++;
94
95
						if($from < 0){
96
							$from = $x;
97
						}
98
					}
99
					elseif($from >= 0){
100
						$svg .= '<rect x="'.($from * $this->options->scale).'" y="'.($y * $this->options->scale)
101
						        .'" width="'.($this->options->scale * $count).'" height="'.$this->options->scale.'" fill="'.$value.'"'
102
						        .(trim($this->options->cssClass) !== '' ? ' class="'.$this->options->cssClass.'"' :'').' />'
103
						        .$this->options->eol;
104
105
						// reset count
106
						$from  = -1;
107
						$count = 0;
108
					}
109
				}
110
111
				// close off the row, if applicable
112
				if($from >= 0){
113
					$svg .= '<rect x="'.($from * $this->options->scale).'" y="'.($y * $this->options->scale)
114
					        .'" width="'.($this->options->scale * $count).'" height="'.$this->options->scale.'" class="'.$this->options->cssClass.'" fill="'.$value.'" />'.$this->options->eol;
115
				}
116
			}
117
		}
118
119
		// close svg
120
		$svg .= '</svg>'.$this->options->eol;
121
122
		// if saving to file, append the correct headers
123
		if($this->options->cachefile){
124
			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;
125
		}
126
127
		return $svg;
128
	}
129
130
}
131