Parcourir la source

:sparkles: load custom output module via options

smiley il y a 8 ans
Parent
commit
d7bacdc942
5 fichiers modifiés avec 54 ajouts et 3 suppressions
  1. 17 3
      README.md
  2. 12 0
      examples/custom_output.php
  3. 6 0
      src/QRCode.php
  4. 9 0
      src/QROptions.php
  5. 10 0
      tests/QRCodeTest.php

+ 17 - 3
README.md

@@ -264,13 +264,25 @@ class MyCustomOptions extends QROptions{
 }
 ```
 
-Invoke a fresh custom `QROutputInterface`, see also `QRCode::initOutputInterface()`.
+You can then call `QRCode` with the custom modules...
 ```php
-$qrOutputInterface = new MyCustomOutput($myCustomOptions, (new QRCode($myCustomOptions))->getMatrix($data));
+$myCustomOptions = new MyCustomOptions([
+	'version'         => 5,
+	'eccLevel'        => QRCode::ECC_L,
+	'outputType'      => QRCode::OUTPUT_CUSTOM,
+	'outputInterface' => MyCustomOutput::class,
+	// custom settings
+	'myParam'         => 'whatever value',
+]);
+
+(new QRCode($myCustomOptions))->render($data);
 ```
 
-...and dump the output, which is equivalent to `QRCode::render()`
+...or you can invoke the `QROutputInterface` manually.
 ```php
+$qrOutputInterface = new MyCustomOutput($myCustomOptions, (new QRCode($myCustomOptions))->getMatrix($data));
+
+//dump the output, which is equivalent to QRCode::render()
 $qrOutputInterface->dump();
 ```
 
@@ -296,6 +308,7 @@ name | description
 `OUTPUT_MARKUP_SVG`, `OUTPUT_MARKUP_HTML` | `QROptions::$outputType` markup
 `OUTPUT_IMAGE_PNG`, `OUTPUT_IMAGE_JPG`, `OUTPUT_IMAGE_GIF` | `QROptions::$outputType` image
 `OUTPUT_STRING_JSON`, `OUTPUT_STRING_TEXT` | `QROptions::$outputType` string
+`OUTPUT_CUSTOM` | `QROptions::$outputType`, requires `QROptions::$outputInterface`
 `ECC_L`, `ECC_M`, `ECC_Q`, `ECC_H`, | ECC-Level: 7%, 15%, 25%, 30%  in `QROptions::$eccLevel`
 `DATA_NUMBER`, `DATA_ALPHANUM`, `DATA_BYTE`, `DATA_KANJI` | `QRDataInterface::$datamode`
 
@@ -310,6 +323,7 @@ property | type | default | allowed | description
 `$addQuietzone` | bool | true | - | Add a "quiet zone" (margin) according to the QR code spec
 `$quietzoneSize` | int | 4 | clamped to 0 ... `$matrixSize / 2` | Size of the quiet zone
 `$outputType` | string | `QRCode::OUTPUT_IMAGE_PNG` | `QRCode::OUTPUT_*` | built-in output type
+`$outputInterface` | string | null | * | FQCN of the custom `QROutputInterface` if `QROptions::$outputType` is set to `QRCode::OUTPUT_CUSTOM`
 `$cachefile` | string | null | * | optional cache file path
 `$eol` | string | `PHP_EOL` | * | newline string (HTML, SVG, TEXT)
 `$scale` | int | 5 | * | size of a QR code pixel (SVG, IMAGE_*), HTML -> via CSS

+ 12 - 0
examples/custom_output.php

@@ -16,6 +16,7 @@ require_once __DIR__.'/../vendor/autoload.php';
 
 $data = 'https://www.youtube.com/watch?v=DLzxrzFCyOs&t=43s';
 
+// invoke the QROutputInterface manually
 $options = new QROptions([
 	'version'      => 5,
 	'eccLevel'     => QRCode::ECC_L,
@@ -24,3 +25,14 @@ $options = new QROptions([
 $qrOutputInterface = new MyCustomOutput($options, (new QRCode($options))->getMatrix($data));
 
 var_dump($qrOutputInterface->dump());
+
+
+// or just
+$options = new QROptions([
+	'version'         => 5,
+	'eccLevel'        => QRCode::ECC_L,
+	'outputType'      => QRCode::OUTPUT_CUSTOM,
+	'outputInterface' => MyCustomOutput::class,
+]);
+
+var_dump((new QRCode($options))->render($data));

+ 6 - 0
src/QRCode.php

@@ -44,6 +44,8 @@ class QRCode{
 	const OUTPUT_STRING_JSON  = 'json';
 	const OUTPUT_STRING_TEXT  = 'text';
 
+	const OUTPUT_CUSTOM       = 'custom';
+
 	const VERSION_AUTO        = -1;
 	const MASK_PATTERN_AUTO   = -1;
 
@@ -244,6 +246,10 @@ class QRCode{
 	 */
 	protected function initOutputInterface(string $data):QROutputInterface{
 
+		if($this->options->outputType === $this::OUTPUT_CUSTOM && $this->options->outputInterface !== null){
+			return $this->loadClass($this->options->outputInterface, QROutputInterface::class, $this->options, $this->getMatrix($data));
+		}
+
 		foreach($this::OUTPUT_MODES as $outputInterface => $modes){
 
 			if(in_array($this->options->outputType, $modes, true)){

+ 9 - 0
src/QROptions.php

@@ -25,6 +25,7 @@ use chillerlan\Traits\Container;
  * @property bool   $quietzoneSize
  *
  * @property string $outputType
+ * @property string $outputInterface
  * @property string $cachefile
  *
  * @property string $eol
@@ -111,11 +112,19 @@ class QROptions{
 	 * QRCode::OUTPUT_MARKUP_XXXX where XXXX = HTML, SVG
 	 * QRCode::OUTPUT_IMAGE_XXX where XXX = PNG, GIF, JPG
 	 * QRCode::OUTPUT_STRING_XXXX where XXXX = TEXT, JSON
+	 * QRCode::OUTPUT_CUSTOM
 	 *
 	 * @var string
 	 */
 	protected $outputType = QRCode::OUTPUT_IMAGE_PNG;
 
+	/**
+	 * the FQCN of the custom QROutputInterface if $outputType is set to QRCode::OUTPUT_CUSTOM
+	 *
+	 * @var string
+	 */
+	protected $outputInterface;
+
 	/**
 	 * /path/to/cache.file
 	 *

Fichier diff supprimé car celui-ci est trop grand
+ 10 - 0
tests/QRCodeTest.php


Certains fichiers n'ont pas été affichés car il y a eu trop de fichiers modifiés dans ce diff