1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Class QROutputAbstract |
4
|
|
|
* |
5
|
|
|
* @filesource QROutputAbstract.php |
6
|
|
|
* @created 09.12.2015 |
7
|
|
|
* @package chillerlan\QRCode\Output |
8
|
|
|
* @author Smiley <[email protected]> |
9
|
|
|
* @copyright 2015 Smiley |
10
|
|
|
* @license MIT |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace chillerlan\QRCode\Output; |
14
|
|
|
|
15
|
|
|
use chillerlan\QRCode\{Data\QRMatrix, QRCode}; |
16
|
|
|
use chillerlan\Settings\SettingsContainerInterface; |
17
|
|
|
|
18
|
|
|
use function call_user_func, dirname, file_put_contents, get_called_class, in_array, is_writable, sprintf; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* common output abstract |
22
|
|
|
*/ |
23
|
|
|
abstract class QROutputAbstract implements QROutputInterface{ |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* the current size of the QR matrix |
27
|
|
|
* |
28
|
|
|
* @see \chillerlan\QRCode\Data\QRMatrix::size() |
29
|
|
|
*/ |
30
|
|
|
protected int $moduleCount; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* the current output mode |
34
|
|
|
* |
35
|
|
|
* @see \chillerlan\QRCode\QROptions::$outputType |
36
|
|
|
*/ |
37
|
|
|
protected string $outputMode; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* the default output mode of the current output module |
41
|
|
|
*/ |
42
|
|
|
protected string $defaultMode; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* the current scaling for a QR pixel |
46
|
|
|
* |
47
|
|
|
* @see \chillerlan\QRCode\QROptions::$scale |
48
|
|
|
*/ |
49
|
|
|
protected int $scale; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* the side length of the QR image (modules * scale) |
53
|
|
|
*/ |
54
|
|
|
protected int $length; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* an (optional) array of color values for the several QR matrix parts |
58
|
|
|
*/ |
59
|
|
|
protected array $moduleValues; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* the (filled) data matrix object |
63
|
|
|
*/ |
64
|
|
|
protected QRMatrix $matrix; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @var \chillerlan\Settings\SettingsContainerInterface|\chillerlan\QRCode\QROptions |
68
|
|
|
*/ |
69
|
|
|
protected SettingsContainerInterface $options; |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* QROutputAbstract constructor. |
73
|
|
|
*/ |
74
|
|
|
public function __construct(SettingsContainerInterface $options, QRMatrix $matrix){ |
75
|
|
|
$this->options = $options; |
76
|
|
|
$this->matrix = $matrix; |
77
|
|
|
$this->moduleCount = $this->matrix->size(); |
78
|
|
|
$this->scale = $this->options->scale; |
79
|
|
|
$this->length = $this->moduleCount * $this->scale; |
80
|
|
|
|
81
|
|
|
$class = get_called_class(); |
82
|
|
|
|
83
|
|
|
if(isset(QRCode::OUTPUT_MODES[$class]) && in_array($this->options->outputType, QRCode::OUTPUT_MODES[$class])){ |
84
|
|
|
$this->outputMode = $this->options->outputType; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
$this->setModuleValues(); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Sets the initial module values (clean-up & defaults) |
92
|
|
|
*/ |
93
|
|
|
abstract protected function setModuleValues():void; |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* saves the qr data to a file |
97
|
|
|
* |
98
|
|
|
* @see file_put_contents() |
99
|
|
|
* @see \chillerlan\QRCode\QROptions::cachefile |
100
|
|
|
* |
101
|
|
|
* @throws \chillerlan\QRCode\Output\QRCodeOutputException |
102
|
|
|
*/ |
103
|
|
|
protected function saveToFile(string $data, string $file):bool{ |
104
|
|
|
|
105
|
|
|
if(!is_writable(dirname($file))){ |
106
|
|
|
throw new QRCodeOutputException(sprintf('Could not write data to cache file: %s', $file)); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
return (bool)file_put_contents($file, $data); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @inheritDoc |
114
|
|
|
*/ |
115
|
|
|
public function dump(string $file = null){ |
116
|
|
|
// call the built-in output method |
117
|
|
|
$data = call_user_func([$this, $this->outputMode ?? $this->defaultMode]); |
118
|
|
|
$file ??= $this->options->cachefile; |
119
|
|
|
|
120
|
|
|
if($file !== null){ |
121
|
|
|
$this->saveToFile($data, $file); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
return $data; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
} |
128
|
|
|
|