Passed
Push — main ( 8c75d8...000860 )
by smiley
10:08
created

QREps::getModuleValue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
nc 1
nop 1
dl 0
loc 5
rs 10
c 1
b 0
f 0
1
<?php
2
/**
3
 * Class QREps
4
 *
5
 * @created      09.05.2022
6
 * @author       smiley <[email protected]>
7
 * @copyright    2022 smiley
8
 * @license      MIT
9
 */
10
11
namespace chillerlan\QRCode\Output;
12
13
use function date, implode, is_int, sprintf;
14
15
/**
16
 * Encapsulated Postscript (EPS) output
17
 *
18
 * @see https://github.com/t0k4rt/phpqrcode/blob/bb29e6eb77e0a2a85bb0eb62725e0adc11ff5a90/qrvect.php#L52-L137
19
 * @see https://web.archive.org/web/20170818010030/http://wwwimages.adobe.com/content/dam/Adobe/en/devnet/postscript/pdfs/5002.EPSF_Spec.pdf
20
 * @see https://web.archive.org/web/20210419003859/https://www.adobe.com/content/dam/acom/en/devnet/actionscript/articles/PLRM.pdf
21
 */
22
class QREps extends QROutputAbstract{
23
24
	/**
25
	 * @inheritDoc
26
	 */
27
	protected function moduleValueIsValid($value):bool{
28
		return is_int($value) && $value >= 0 && $value <= 0xffffff;
29
	}
30
31
	/**
32
	 * @inheritDoc
33
	 */
34
	protected function getModuleValue($value):array{
35
		return [
36
			round((($value & 0xff0000) >> 16) / 255, 5),
37
			round((($value & 0x00ff00) >> 8) / 255, 5),
38
			round(($value & 0x0000ff) / 255, 5)
39
		];
40
	}
41
42
	/**
43
	 * @inheritDoc
44
	 */
45
	protected function getDefaultModuleValue(bool $isDark):array{
46
		return $isDark ? [0.0, 0.0, 0.0] : [1.0, 1.0, 1.0];
47
	}
48
49
	/**
50
	 * @inheritDoc
51
	 */
52
	public function dump(string $file = null):string{
53
		$file       ??= $this->options->cachefile;
54
		$saveToFile   = $file !== null;
55
56
		$eps = [
57
			// main header
58
			'%!PS-Adobe-3.0 EPSF-3.0',
59
			'%%Creator: php-qrcode (https://github.com/chillerlan/php-qrcode)',
60
			'%%Title: QR Code',
61
			sprintf('%%%%CreationDate: %1$s', date('c')),
62
			'%%DocumentData: Clean7Bit',
63
			'%%LanguageLevel: 3',
64
			sprintf('%%%%BoundingBox: 0 -%1$s %1$s 0', $this->length),
65
			'%%EndComments',
66
			// function definitions
67
			'%%BeginProlog',
68
			'/F { rectfill } def',
69
			'/S { setrgbcolor } def',
70
			'%%EndProlog',
71
			// rotate into the proper orientation and scale to fit
72
			'-90 rotate',
73
			sprintf('%1$s %1$s scale', $this->scale),
74
		];
75
76
		// create the path elements
77
		$paths = $this->collectModules(fn(int $x, int $y):string => sprintf('%s %s 1 1 F', $x, $y));
78
79
		foreach($paths as $M_TYPE => $path){
80
81
			if(empty($path)){
82
				continue;
83
			}
84
85
			$eps[] = sprintf('%f %f %f S', ...$this->moduleValues[$M_TYPE]);
86
			$eps[] = implode("\n", $path);
87
		}
88
89
		// end file
90
		$eps[] = '%%EOF';
91
92
		$data = implode("\n", $eps);
93
94
		if($saveToFile){
95
			$this->saveToFile($data, $file);
0 ignored issues
show
Bug introduced by
It seems like $file can also be of type null; however, parameter $file of chillerlan\QRCode\Output...tAbstract::saveToFile() 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

95
			$this->saveToFile($data, /** @scrutinizer ignore-type */ $file);
Loading history...
96
		}
97
98
		return $data;
99
	}
100
101
}
102