1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Class QROutputAbstract |
4
|
|
|
* |
5
|
|
|
* @created 09.12.2015 |
6
|
|
|
* @author Smiley <[email protected]> |
7
|
|
|
* @copyright 2015 Smiley |
8
|
|
|
* @license MIT |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace chillerlan\QRCode\Output; |
12
|
|
|
|
13
|
|
|
use chillerlan\QRCode\Data\QRMatrix; |
14
|
|
|
use chillerlan\Settings\SettingsContainerInterface; |
15
|
|
|
use function base64_encode, dirname, file_put_contents, is_writable, sprintf; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* common output abstract |
19
|
|
|
*/ |
20
|
|
|
abstract class QROutputAbstract implements QROutputInterface{ |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* the current size of the QR matrix |
24
|
|
|
* |
25
|
|
|
* @see \chillerlan\QRCode\Data\QRMatrix::size() |
26
|
|
|
*/ |
27
|
|
|
protected int $moduleCount; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* the current scaling for a QR pixel |
31
|
|
|
* |
32
|
|
|
* @see \chillerlan\QRCode\QROptions::$scale |
33
|
|
|
*/ |
34
|
|
|
protected int $scale; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* the side length of the QR image (modules * scale) |
38
|
|
|
*/ |
39
|
|
|
protected int $length; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* an (optional) array of color values for the several QR matrix parts |
43
|
|
|
*/ |
44
|
|
|
protected array $moduleValues; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* the (filled) data matrix object |
48
|
|
|
*/ |
49
|
|
|
protected QRMatrix $matrix; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @var \chillerlan\Settings\SettingsContainerInterface|\chillerlan\QRCode\QROptions |
53
|
|
|
*/ |
54
|
|
|
protected SettingsContainerInterface $options; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* QROutputAbstract constructor. |
58
|
|
|
*/ |
59
|
|
|
public function __construct(SettingsContainerInterface $options, QRMatrix $matrix){ |
60
|
|
|
$this->options = $options; |
61
|
|
|
$this->matrix = $matrix; |
62
|
|
|
$this->moduleCount = $this->matrix->size(); |
63
|
|
|
$this->scale = $this->options->scale; |
64
|
|
|
$this->length = $this->moduleCount * $this->scale; |
65
|
|
|
|
66
|
|
|
$this->setModuleValues(); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Sets the initial module values |
71
|
|
|
*/ |
72
|
|
|
protected function setModuleValues():void{ |
73
|
|
|
|
74
|
|
|
foreach($this::DEFAULT_MODULE_VALUES as $M_TYPE => $defaultValue){ |
75
|
|
|
$value = $this->options->moduleValues[$M_TYPE] ?? null; |
76
|
|
|
|
77
|
|
|
$this->moduleValues[$M_TYPE] = $this->moduleValueIsValid($value) |
78
|
|
|
? $this->getModuleValue($value) |
79
|
|
|
: $this->getDefaultModuleValue($defaultValue); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Determines whether the given value is valid |
86
|
|
|
* |
87
|
|
|
* @param mixed|null $value |
88
|
|
|
*/ |
89
|
|
|
abstract protected function moduleValueIsValid($value):bool; |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Returns the final value for the given input (return value depends on the output module) |
93
|
|
|
* |
94
|
|
|
* @param mixed $value |
95
|
|
|
* |
96
|
|
|
* @return mixed |
97
|
|
|
*/ |
98
|
|
|
abstract protected function getModuleValue($value); |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Returns a defualt value for either dark or light modules (return value depends on the output module) |
102
|
|
|
* |
103
|
|
|
* @return mixed |
104
|
|
|
*/ |
105
|
|
|
abstract protected function getDefaultModuleValue(bool $isDark); |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Returns a base64 data URI for the given string and mime type |
109
|
|
|
*/ |
110
|
|
|
protected function base64encode(string $data, string $mime):string{ |
111
|
|
|
return sprintf('data:%s;base64,%s', $mime, base64_encode($data)); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* saves the qr data to a file |
116
|
|
|
* |
117
|
|
|
* @see file_put_contents() |
118
|
|
|
* @see \chillerlan\QRCode\QROptions::cachefile |
119
|
|
|
* |
120
|
|
|
* @throws \chillerlan\QRCode\Output\QRCodeOutputException |
121
|
|
|
*/ |
122
|
|
|
protected function saveToFile(string $data, string $file):void{ |
123
|
|
|
|
124
|
|
|
if(!is_writable(dirname($file))){ |
125
|
|
|
throw new QRCodeOutputException(sprintf('Cannot write data to cache file: %s', $file)); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
if(file_put_contents($file, $data) === false){ |
129
|
|
|
throw new QRCodeOutputException(sprintf('Cannot write data to cache file: %s (file_put_contents error)', $file)); |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
} |
134
|
|
|
|