Passed
Push — main ( 7963d9...f1858f )
by smiley
02:10
created

QRString::dump()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 4
eloc 11
nc 6
nop 1
dl 0
loc 17
rs 9.9
c 2
b 0
f 0
1
<?php
2
/**
3
 * Class QRString
4
 *
5
 * @created      05.12.2015
6
 * @author       Smiley <[email protected]>
7
 * @copyright    2015 Smiley
8
 * @license      MIT
9
 *
10
 * @noinspection PhpUnusedParameterInspection
11
 * @noinspection PhpComposerExtensionStubsInspection
12
 */
13
14
namespace chillerlan\QRCode\Output;
15
16
use chillerlan\QRCode\QRCode;
17
18
use function implode, is_string, json_encode;
19
20
/**
21
 * Converts the matrix data into string types
22
 */
23
class QRString extends QROutputAbstract{
24
25
	protected string $defaultMode = QRCode::OUTPUT_STRING_TEXT;
26
27
	/**
28
	 * @inheritDoc
29
	 */
30
	protected function moduleValueIsValid($value):bool{
31
		return is_string($value);
32
	}
33
34
	/**
35
	 * @inheritDoc
36
	 */
37
	protected function getModuleValue($value):string{
38
		return $value;
39
	}
40
41
	/**
42
	 * @inheritDoc
43
	 */
44
	protected function getDefaultModuleValue(bool $isDark):string{
45
		return $isDark ? $this->options->textDark : $this->options->textLight;
46
	}
47
48
	/**
49
	 * @inheritDoc
50
	 */
51
	public function dump(string $file = null):string{
52
		$file ??= $this->options->cachefile;
53
54
		switch($this->options->outputType){
55
			case QRCode::OUTPUT_STRING_TEXT:
56
				$data = $this->text();
57
				break;
58
			case QRCode::OUTPUT_STRING_JSON:
59
			default:
60
				$data = $this->json();
61
		}
62
63
		if($file !== null){
64
			$this->saveToFile($data, $file);
65
		}
66
67
		return $data;
68
	}
69
70
	/**
71
	 * string output
72
	 */
73
	protected function text():string{
74
		$str = [];
75
76
		foreach($this->matrix->matrix() as $row){
77
			$r = [];
78
79
			foreach($row as $M_TYPE){
80
				$r[] = $this->moduleValues[$M_TYPE];
81
			}
82
83
			$str[] = implode('', $r);
84
		}
85
86
		return implode($this->options->eol, $str);
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

86
		return implode(/** @scrutinizer ignore-type */ $this->options->eol, $str);
Loading history...
87
	}
88
89
	/**
90
	 * JSON output
91
	 */
92
	protected function json():string{
93
		return json_encode($this->matrix->matrix());
94
	}
95
96
}
97