Passed
Push — main ( 3afa88...ae0f31 )
by smiley
02:04
created

QRMarkupSVG::getCssClass()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 1
nop 1
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Class QRMarkupSVG
4
 *
5
 * @created      06.06.2022
6
 * @author       smiley <[email protected]>
7
 * @copyright    2022 smiley
8
 * @license      MIT
9
 */
10
11
namespace chillerlan\QRCode\Output;
12
13
use chillerlan\QRCode\Data\QRMatrix;
14
use function array_chunk, implode, is_string, preg_match, sprintf, trim;
15
16
/**
17
 * SVG output
18
 *
19
 * @see https://github.com/codemasher/php-qrcode/pull/5
20
 * @see https://developer.mozilla.org/en-US/docs/Web/SVG/Element/svg
21
 * @see https://www.sarasoueidan.com/demos/interactive-svg-coordinate-system/
22
 * @see http://apex.infogridpacific.com/SVG/svg-tutorial-contents.html
23
 */
24
class QRMarkupSVG extends QRMarkup{
25
26
	/**
27
	 * @see https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/fill
28
	 * @inheritDoc
29
	 */
30
	public static function moduleValueIsValid($value):bool{
31
32
		if(!is_string($value)){
33
			return false;
34
		}
35
36
		$value = trim($value);
37
38
		// url(...)
39
		if(preg_match('~^url\([-/#a-z\d]+\)$~i', $value)){
40
			return true;
41
		}
42
43
		// otherwise check for standard css notation
44
		return parent::moduleValueIsValid($value);
45
	}
46
47
	/**
48
	 * @inheritDoc
49
	 */
50
	protected function createMarkup(bool $saveToFile):string{
51
		$svg = $this->header();
52
53
		if(!empty($this->options->svgDefs)){
54
			$svg .= sprintf('<defs>%1$s%2$s</defs>%2$s', $this->options->svgDefs, $this->options->eol);
55
		}
56
57
		$svg .= $this->paths();
58
59
		// close svg
60
		$svg .= sprintf('%1$s</svg>%1$s', $this->options->eol);
61
62
		// transform to data URI only when not saving to file
63
		if(!$saveToFile && $this->options->imageBase64){
64
			$svg = $this->toBase64DataURI($svg, 'image/svg+xml');
65
		}
66
67
		return $svg;
68
	}
69
70
	/**
71
	 * returns the <svg> header with the given options parsed
72
	 */
73
	protected function header():string{
74
		$width  = ($this->options->svgWidth !== null) ? sprintf(' width="%s"', $this->options->svgWidth) : '';
75
		$height = ($this->options->svgHeight !== null) ? sprintf(' height="%s"', $this->options->svgHeight) : '';
76
77
		/** @noinspection HtmlUnknownAttribute */
78
		return sprintf(
79
			'<?xml version="1.0" encoding="UTF-8"?>%6$s'.
80
			'<svg xmlns="http://www.w3.org/2000/svg" class="qr-svg %1$s" viewBox="0 0 %2$s %2$s" preserveAspectRatio="%3$s"%4$s%5$s>%6$s',
81
			$this->options->cssClass,
82
			($this->options->svgViewBoxSize ?? $this->moduleCount),
83
			$this->options->svgPreserveAspectRatio,
84
			$width,
85
			$height,
86
			$this->options->eol
87
		);
88
	}
89
90
	/**
91
	 * returns one or more SVG <path> elements
92
	 */
93
	protected function paths():string{
94
		$paths = $this->collectModules(fn(int $x, int $y, int $M_TYPE):string => $this->module($x, $y, $M_TYPE));
95
		$svg   = [];
96
97
		// create the path elements
98
		foreach($paths as $M_TYPE => $modules){
99
			// limit the total line length
100
			$chunks = array_chunk($modules, 100);
101
			$chonks = [];
102
103
			foreach($chunks as $chunk){
104
				$chonks[] = implode(' ', $chunk);
105
			}
106
107
			$path = implode($this->options->eol, $chonks);
0 ignored issues
show
Bug introduced by
It seems like $this->options->eol can also be of type null; however, parameter $glue of implode() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

107
			$path = implode(/** @scrutinizer ignore-type */ $this->options->eol, $chonks);
Loading history...
108
109
			if(empty($path)){
110
				continue;
111
			}
112
113
			$svg[] = $this->path($path, $M_TYPE);
114
		}
115
116
		return implode($this->options->eol, $svg);
117
	}
118
119
	/**
120
	 * renders and returns a single <path> element
121
	 *
122
	 * @see https://developer.mozilla.org/en-US/docs/Web/SVG/Element/path
123
	 */
124
	protected function path(string $path, int $M_TYPE):string{
125
		// ignore non-existent module values
126
		$format = !isset($this->moduleValues[$M_TYPE]) || empty($this->moduleValues[$M_TYPE])
127
			? '<path class="%1$s" d="%2$s"/>'
128
			: '<path class="%1$s" fill="%3$s" fill-opacity="%4$s" d="%2$s"/>';
129
130
		return sprintf(
131
			$format,
132
			$this->getCssClass($M_TYPE),
133
			$path,
134
			($this->moduleValues[$M_TYPE] ?? ''), // value may or may not exist
135
			$this->options->svgOpacity
136
		);
137
	}
138
139
	/**
140
	 * @inheritDoc
141
	 */
142
	protected function getCssClass(int $M_TYPE):string{
143
		return implode(' ', [
144
			'qr-'.$M_TYPE,
145
			(($M_TYPE & QRMatrix::IS_DARK) === QRMatrix::IS_DARK) ? 'dark' : 'light',
146
			$this->options->cssClass,
147
		]);
148
	}
149
150
	/**
151
	 * returns a path segment for a single module
152
	 *
153
	 * @see https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/d
154
	 */
155
	protected function module(int $x, int $y, int $M_TYPE):string{
0 ignored issues
show
Unused Code introduced by
The parameter $M_TYPE is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

155
	protected function module(int $x, int $y, /** @scrutinizer ignore-unused */ int $M_TYPE):string{

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
156
157
		if(!$this->options->drawLightModules && !$this->matrix->check($x, $y)){
158
			return '';
159
		}
160
161
		if($this->options->drawCircularModules && !$this->matrix->checkTypeIn($x, $y, $this->options->keepAsSquare)){
0 ignored issues
show
Bug introduced by
It seems like $this->options->keepAsSquare can also be of type null; however, parameter $M_TYPES of chillerlan\QRCode\Data\QRMatrix::checkTypeIn() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

161
		if($this->options->drawCircularModules && !$this->matrix->checkTypeIn($x, $y, /** @scrutinizer ignore-type */ $this->options->keepAsSquare)){
Loading history...
162
			$r = $this->options->circleRadius;
163
164
			return sprintf(
165
				'M%1$s %2$s a%3$s %3$s 0 1 0 %4$s 0 a%3$s %3$s 0 1 0 -%4$s 0Z',
166
				($x + 0.5 - $r),
167
				($y + 0.5),
168
				$r,
169
				($r * 2)
170
			);
171
172
		}
173
174
		return sprintf('M%1$s %2$s h1 v1 h-1Z', $x, $y);
175
	}
176
177
}
178