Skip to content

Commit d7bacdc

Browse files
committed
✨ load custom output module via options
1 parent 4fa3b3e commit d7bacdc

File tree

5 files changed

+58
-3
lines changed

5 files changed

+58
-3
lines changed

README.md

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -264,13 +264,25 @@ class MyCustomOptions extends QROptions{
264264
}
265265
```
266266

267-
Invoke a fresh custom `QROutputInterface`, see also `QRCode::initOutputInterface()`.
267+
You can then call `QRCode` with the custom modules...
268268
```php
269-
$qrOutputInterface = new MyCustomOutput($myCustomOptions, (new QRCode($myCustomOptions))->getMatrix($data));
269+
$myCustomOptions = new MyCustomOptions([
270+
'version' => 5,
271+
'eccLevel' => QRCode::ECC_L,
272+
'outputType' => QRCode::OUTPUT_CUSTOM,
273+
'outputInterface' => MyCustomOutput::class,
274+
// custom settings
275+
'myParam' => 'whatever value',
276+
]);
277+
278+
(new QRCode($myCustomOptions))->render($data);
270279
```
271280

272-
...and dump the output, which is equivalent to `QRCode::render()`
281+
...or you can invoke the `QROutputInterface` manually.
273282
```php
283+
$qrOutputInterface = new MyCustomOutput($myCustomOptions, (new QRCode($myCustomOptions))->getMatrix($data));
284+
285+
//dump the output, which is equivalent to QRCode::render()
274286
$qrOutputInterface->dump();
275287
```
276288

@@ -296,6 +308,7 @@ name | description
296308
`OUTPUT_MARKUP_SVG`, `OUTPUT_MARKUP_HTML` | `QROptions::$outputType` markup
297309
`OUTPUT_IMAGE_PNG`, `OUTPUT_IMAGE_JPG`, `OUTPUT_IMAGE_GIF` | `QROptions::$outputType` image
298310
`OUTPUT_STRING_JSON`, `OUTPUT_STRING_TEXT` | `QROptions::$outputType` string
311+
`OUTPUT_CUSTOM` | `QROptions::$outputType`, requires `QROptions::$outputInterface`
299312
`ECC_L`, `ECC_M`, `ECC_Q`, `ECC_H`, | ECC-Level: 7%, 15%, 25%, 30% in `QROptions::$eccLevel`
300313
`DATA_NUMBER`, `DATA_ALPHANUM`, `DATA_BYTE`, `DATA_KANJI` | `QRDataInterface::$datamode`
301314

@@ -310,6 +323,7 @@ property | type | default | allowed | description
310323
`$addQuietzone` | bool | true | - | Add a "quiet zone" (margin) according to the QR code spec
311324
`$quietzoneSize` | int | 4 | clamped to 0 ... `$matrixSize / 2` | Size of the quiet zone
312325
`$outputType` | string | `QRCode::OUTPUT_IMAGE_PNG` | `QRCode::OUTPUT_*` | built-in output type
326+
`$outputInterface` | string | null | * | FQCN of the custom `QROutputInterface` if `QROptions::$outputType` is set to `QRCode::OUTPUT_CUSTOM`
313327
`$cachefile` | string | null | * | optional cache file path
314328
`$eol` | string | `PHP_EOL` | * | newline string (HTML, SVG, TEXT)
315329
`$scale` | int | 5 | * | size of a QR code pixel (SVG, IMAGE_*), HTML -> via CSS

examples/custom_output.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
$data = 'https://www.youtube.com/watch?v=DLzxrzFCyOs&t=43s';
1818

19+
// invoke the QROutputInterface manually
1920
$options = new QROptions([
2021
'version' => 5,
2122
'eccLevel' => QRCode::ECC_L,
@@ -24,3 +25,14 @@
2425
$qrOutputInterface = new MyCustomOutput($options, (new QRCode($options))->getMatrix($data));
2526

2627
var_dump($qrOutputInterface->dump());
28+
29+
30+
// or just
31+
$options = new QROptions([
32+
'version' => 5,
33+
'eccLevel' => QRCode::ECC_L,
34+
'outputType' => QRCode::OUTPUT_CUSTOM,
35+
'outputInterface' => MyCustomOutput::class,
36+
]);
37+
38+
var_dump((new QRCode($options))->render($data));

src/QRCode.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ class QRCode{
4444
const OUTPUT_STRING_JSON = 'json';
4545
const OUTPUT_STRING_TEXT = 'text';
4646

47+
const OUTPUT_CUSTOM = 'custom';
48+
4749
const VERSION_AUTO = -1;
4850
const MASK_PATTERN_AUTO = -1;
4951

@@ -244,6 +246,10 @@ public function initDataInterface(string $data):QRDataInterface{
244246
*/
245247
protected function initOutputInterface(string $data):QROutputInterface{
246248

249+
if($this->options->outputType === $this::OUTPUT_CUSTOM && $this->options->outputInterface !== null){
250+
return $this->loadClass($this->options->outputInterface, QROutputInterface::class, $this->options, $this->getMatrix($data));
251+
}
252+
247253
foreach($this::OUTPUT_MODES as $outputInterface => $modes){
248254

249255
if(in_array($this->options->outputType, $modes, true)){

src/QROptions.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
* @property bool $quietzoneSize
2626
*
2727
* @property string $outputType
28+
* @property string $outputInterface
2829
* @property string $cachefile
2930
*
3031
* @property string $eol
@@ -111,11 +112,19 @@ class QROptions{
111112
* QRCode::OUTPUT_MARKUP_XXXX where XXXX = HTML, SVG
112113
* QRCode::OUTPUT_IMAGE_XXX where XXX = PNG, GIF, JPG
113114
* QRCode::OUTPUT_STRING_XXXX where XXXX = TEXT, JSON
115+
* QRCode::OUTPUT_CUSTOM
114116
*
115117
* @var string
116118
*/
117119
protected $outputType = QRCode::OUTPUT_IMAGE_PNG;
118120

121+
/**
122+
* the FQCN of the custom QROutputInterface if $outputType is set to QRCode::OUTPUT_CUSTOM
123+
*
124+
* @var string
125+
*/
126+
protected $outputInterface;
127+
119128
/**
120129
* /path/to/cache.file
121130
*

tests/QRCodeTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
use chillerlan\QRCode\QROptions;
1616
use chillerlan\QRCode\QRCode;
17+
use chillerlan\QRCodeExamples\MyCustomOutput;
1718

1819
class QRCodeTest extends QRTestAbstract{
1920

@@ -99,4 +100,17 @@ public function testImageTransparencyBGDefault(){
99100
$this->assertSame([255,255,255], $this->getProperty('options')->getValue($this->qrcode)->imageTransparencyBG);
100101
}
101102

103+
public function testCustomOutput(){
104+
105+
$options = new QROptions([
106+
'version' => 5,
107+
'eccLevel' => QRCode::ECC_L,
108+
'outputType' => QRCode::OUTPUT_CUSTOM,
109+
'outputInterface' => MyCustomOutput::class,
110+
]);
111+
112+
$expected = '000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000011111110111010000101111010000011111110000000010000010111000001101011000001010000010000000010111010101101011000001101011010111010000000010111010110100111110010100111010111010000000010111010000001101011000001101010111010000000010000010100111110010100111110010000010000000011111110101010101010101010101011111110000000000000000010010100111110010100000000000000000011001110000101111010000101111001011110000000000000000111010000101111010000111100010000000001011010100111110010100111110011001010000000010000101111101011000001101011110011110000000000011010100011000001101011000101110100000000011001100001001101011000001101010011010000000010110111110000001101011000001100110100000000010000100100010100111110010100001100100000000011111110111101111010000101111010100110000000011010000111010000101111010000111100100000000010101111111111110010100111110011001000000000010110001110101011000001101011110011010000000001001111100011000001101011000101110010000000011000100110001101011000001101010011100000000001000011001000001101011000001100110000000000011101001011010100111110010100001100000000000010111010001101111010000101111010100110000000011100000001010000101111010000111100000000000000001110110111110010100111110011001000000000000011001011101011000001101011110011100000000011111110101011000001101011001111110110000000000000000110001101011000001101000111100000000011111110001000001101011000011010110000000000010000010101010100111110010101000100100000000010111010111101111010000101111111100110000000010111010011010000101111010001101100010000000010111010000111110010100111100101101100000000010000010101101011000001101001100111100000000011111110101011000001101011000110010110000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000';
113+
114+
$this->assertSame($expected, $this->reflection->newInstanceArgs([$options])->render('test'));
115+
}
102116
}

0 commit comments

Comments
 (0)