Passed
Push — main ( 709a03...3e1426 )
by smiley
02:31
created

QRFpdf::getDefaultModuleValue()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 1
nc 2
nop 1
dl 0
loc 2
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Class QRFpdf
4
 *
5
 * @created      03.06.2020
6
 * @author       Maximilian Kresse
7
 * @license      MIT
8
 *
9
 * @see https://github.com/chillerlan/php-qrcode/pull/49
10
 */
11
12
namespace chillerlan\QRCode\Output;
13
14
use chillerlan\QRCode\Data\QRMatrix;
15
use chillerlan\Settings\SettingsContainerInterface;
16
use FPDF;
17
18
use function array_values, class_exists, count, is_array;
19
20
/**
21
 * QRFpdf output module (requires fpdf)
22
 *
23
 * @see https://github.com/Setasign/FPDF
24
 * @see http://www.fpdf.org/
25
 */
26
class QRFpdf extends QROutputAbstract{
27
28
	/**
29
	 * QRFpdf constructor.
30
	 *
31
	 * @throws \chillerlan\QRCode\Output\QRCodeOutputException
32
	 */
33
	public function __construct(SettingsContainerInterface $options, QRMatrix $matrix){
34
35
		if(!class_exists(FPDF::class)){
36
			// @codeCoverageIgnoreStart
37
			throw new QRCodeOutputException(
38
				'The QRFpdf output requires FPDF (https://github.com/Setasign/FPDF)'.
39
				' as dependency but the class "\\FPDF" couldn\'t be found.'
40
			);
41
			// @codeCoverageIgnoreEnd
42
		}
43
44
		parent::__construct($options, $matrix);
45
	}
46
47
	/**
48
	 * @inheritDoc
49
	 */
50
	protected function moduleValueIsValid($value):bool{
51
		return is_array($value) && count($value) >= 3;
52
	}
53
54
	/**
55
	 * @inheritDoc
56
	 */
57
	protected function getModuleValue($value):array{
58
		return array_values($value);
59
	}
60
61
	/**
62
	 * @inheritDoc
63
	 */
64
	protected function getDefaultModuleValue(bool $isDark):array{
65
		return $isDark ? [0, 0, 0] : [255, 255, 255];
66
	}
67
68
	/**
69
	 * @inheritDoc
70
	 *
71
	 * @return string|\FPDF
72
	 */
73
	public function dump(string $file = null){
74
		$file ??= $this->options->cachefile;
75
76
		$fpdf = new FPDF('P', $this->options->fpdfMeasureUnit, [$this->length, $this->length]);
77
		$fpdf->AddPage();
78
79
		$prevColor = null;
80
81
		foreach($this->matrix->matrix() as $y => $row){
82
83
			foreach($row as $x => $M_TYPE){
84
				/** @var int $M_TYPE */
85
				$color = $this->moduleValues[$M_TYPE];
86
87
				if($prevColor !== $color){
88
					/** @phan-suppress-next-line PhanParamTooFewUnpack */
89
					$fpdf->SetFillColor(...$color);
0 ignored issues
show
Bug introduced by
$color is expanded, but the parameter $r of FPDF::SetFillColor() does not expect variable arguments. ( Ignorable by Annotation )

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

89
					$fpdf->SetFillColor(/** @scrutinizer ignore-type */ ...$color);
Loading history...
90
					$prevColor = $color;
91
				}
92
93
				$fpdf->Rect($x * $this->scale, $y * $this->scale, 1 * $this->scale, 1 * $this->scale, 'F');
94
			}
95
96
		}
97
98
		if($this->options->returnResource){
99
			return $fpdf;
100
		}
101
102
		$pdfData = $fpdf->Output('S');
103
104
		if($file !== null){
105
			$this->saveToFile($pdfData, $file);
106
		}
107
108
		if($this->options->imageBase64){
109
			$pdfData = $this->base64encode($pdfData, 'application/pdf');
110
		}
111
112
		return $pdfData;
113
	}
114
115
}
116