Просмотр исходного кода

:octocat: QROptionsTrait cleanup

smiley 2 лет назад
Родитель
Сommit
ba3636cfe9
2 измененных файлов с 178 добавлено и 120 удалено
  1. 177 119
      src/QROptionsTrait.php
  2. 1 1
      tests/QROptionsTest.php

+ 177 - 119
src/QROptionsTrait.php

@@ -14,13 +14,17 @@ namespace chillerlan\QRCode;
 
 use chillerlan\QRCode\Output\QROutputInterface;
 use chillerlan\QRCode\Common\{EccLevel, MaskPattern, Version};
-use function array_values, count, extension_loaded, in_array, is_numeric, max, min, strtolower;
+use function extension_loaded, in_array, max, min, strtolower;
 
 /**
  * The QRCode plug-in settings & setter functionality
  */
 trait QROptionsTrait{
 
+	/*
+	 * QR Code specific settings
+	 */
+
 	/**
 	 * QR Code version number
 	 *
@@ -76,6 +80,11 @@ trait QROptionsTrait{
 	 */
 	protected int $quietzoneSize = 4;
 
+
+	/*
+	 * General output settings
+	 */
+
 	/**
 	 * The output type
 	 *
@@ -95,79 +104,59 @@ trait QROptionsTrait{
 	protected ?string $outputInterface = null;
 
 	/**
-	 * /path/to/cache.file
+	 * Return the image resource instead of a render if applicable.
+	 * This option overrides other output options, such as $cachefile and $imageBase64.
 	 *
-	 * please note that the $file parameter in QRCode::render*() takes precedence over the $cachefile value
-	 */
-	protected ?string $cachefile = null;
-
-	/**
-	 * newline string [HTML, SVG, TEXT]
-	 */
-	protected string $eol = PHP_EOL;
-
-	/**
-	 * size of a QR code pixel [SVG, IMAGE_*], HTML via CSS
-	 */
-	protected int $scale = 5;
-
-	/**
-	 * a common css class
-	 */
-	protected string $cssClass = 'qrcode';
-
-	/**
-	 * SVG opacity
-	 */
-	protected float $svgOpacity = 1.0;
-
-	/**
-	 * anything between <defs>
+	 * Supported by the following modules:
 	 *
-	 * @see https://developer.mozilla.org/en-US/docs/Web/SVG/Element/defs
+	 * - QRGdImage: resource (PHP < 8), GdImage
+	 * - QRImagick: Imagick
+	 * - QRFpdf:    FPDF
+	 *
+	 * @see \chillerlan\QRCode\Output\QROutputInterface::dump()
+	 *
+	 * @var bool
 	 */
-	protected string $svgDefs = '';
+	protected bool $returnResource = false;
 
 	/**
-	 * SVG viewBox size. a single integer number which defines width/height of the viewBox attribute.
-	 *
-	 * viewBox="0 0 x x"
+	 * /path/to/cache.file
 	 *
-	 * @see https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/viewBox
-	 * @see https://css-tricks.com/scale-svg/#article-header-id-3
+	 * please note that the $file parameter in QRCode::render*() takes precedence over the $cachefile value
 	 */
-	protected ?int $svgViewBoxSize = null;
+	protected ?string $cachefile = null;
 
 	/**
-	 * @see https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/preserveAspectRatio
+	 * toggle base64 or raw image data (if applicable)
 	 */
-	protected string $svgPreserveAspectRatio = 'xMidYMid';
+	protected bool $imageBase64 = true;
 
 	/**
-	 * optional "width" attribute with the specified value (note that the value is not checked!)
-	 *
-	 * @see https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/width
+	 * newline string
 	 */
-	protected ?string $svgWidth = null;
+	protected string $eol = PHP_EOL;
 
-	/**
-	 * optional "height" attribute with the specified value (note that the value is not checked!)
-	 *
-	 * @see https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/height
+	/*
+	 * Common visual modifications
 	 */
-	protected ?string $svgHeight = null;
 
 	/**
-	 * whether to connect the paths for the several module types to avoid weird glitches when using gradients etc.
+	 * Sets the image background color (if applicable)
 	 *
-	 * @see https://github.com/chillerlan/php-qrcode/issues/57
+	 * - QRGdImage: defaults to "white"
+	 * - QRImagick: defaults to [255, 255, 255]
+	 * - QRFpdf: defaults to blank internally (white page)
+	 *
+	 * @var mixed|null
 	 */
-	protected bool $connectPaths = false;
+	protected $bgColor = null;
 
 	/**
-	 * specify which paths/patterns to exclude from connecting if $svgConnectPaths is set to true
+	 * whether to draw the light (false) modules
+	 *
+	 * @var bool
 	 */
-	protected array $excludeFromConnect = [];
+	protected bool $drawLightModules = true;
 
 	/**
 	 * specify whether to draw the modules as filled circles
@@ -177,6 +166,8 @@ trait QROptionsTrait{
 	 * if QROptions::$scale is less than 20, the image will be upscaled internally, then the modules will be drawn
 	 * using imagefilledellipse() and then scaled back to the expected size
 	 *
+	 * No effect in: QREps, QRFpdf, QRMarkupHTML
+	 *
 	 * @see https://github.com/chillerlan/php-qrcode/issues/23
 	 * @see https://github.com/chillerlan/php-qrcode/discussions/122
 	 */
@@ -193,60 +184,60 @@ trait QROptionsTrait{
 	protected array $keepAsSquare = [];
 
 	/**
-	 * string substitute for dark
+	 * Module values map
+	 *
+	 *   - QRImagick, QRMarkupHTML, QRMarkupSVG: #ABCDEF, cssname, rgb(), rgba()...
+	 *   - QREps, QRFpdf, QRGdImage: [63, 127, 255] // R, G, B
 	 */
-	protected string $textDark = '🔴';
+	protected ?array $moduleValues = null;
 
 	/**
-	 * string substitute for light
+	 * Toggles logo space creation
 	 */
-	protected string $textLight = '⭕';
+	protected bool $addLogoSpace = false;
 
 	/**
-	 * markup substitute for dark (CSS value)
+	 * width of the logo space
+	 *
+	 * if only either $logoSpaceWidth or $logoSpaceHeight is given, the logo space is assumed a square of that size
 	 */
-	protected string $markupDark = '#000';
+	protected ?int $logoSpaceWidth = null;
 
 	/**
-	 * markup substitute for light (CSS value)
+	 * height of the logo space
+	 *
+	 * if only either $logoSpaceWidth or $logoSpaceHeight is given, the logo space is assumed a square of that size
 	 */
-	protected string $markupLight = '#fff';
+	protected ?int $logoSpaceHeight = null;
 
 	/**
-	 * Return the image resource instead of a render if applicable.
-	 * This option overrides other output options, such as $cachefile and $imageBase64.
-	 *
-	 * Supported by the following modules:
-	 *
-	 * - QRImage:   resource (PHP < 8), GdImage
-	 * - QRImagick: Imagick
-	 * - QRFpdf:    FPDF
-	 *
-	 * @see \chillerlan\QRCode\Output\QROutputInterface::dump()
-	 *
-	 * @var bool
+	 * optional horizontal start position of the logo space (top left corner)
 	 */
-	protected bool $returnResource = false;
+	protected ?int $logoSpaceStartX = null;
 
 	/**
-	 * toggle base64 or raw image data
+	 * optional vertical start position of the logo space (top left corner)
+	 */
+	protected ?int $logoSpaceStartY = null;
+
+
+	/*
+	 * Common raster image settings (QRGdImage, QRImagick)
 	 */
-	protected bool $imageBase64 = true;
 
 	/**
-	 * toggle transparency
-	 *
-	 * @see \chillerlan\QRCode\QROptions::$transparencyColor
-	 * @see https://github.com/chillerlan/php-qrcode/discussions/121
- 	 */
-	protected bool $imageTransparent = true;
+	 * pixel size of a QR code module
+	 */
+	protected int $scale = 5;
 
 	/**
-	 * whether to draw the light (false) modules
+	 * toggle transparency
 	 *
-	 * @var bool
+	 * - QRGdImage and QRImagick: the given {@see \chillerlan\QRCode\QROptions::$transparencyColor $transparencyColor} is set as transparent
+	 *
+	 * @see https://github.com/chillerlan/php-qrcode/discussions/121
 	 */
-	protected bool $drawLightModules = true;
+	protected bool $imageTransparent = true;
 
 	/**
 	 * Sets a transparency color for when {@see \chillerlan\QRCode\QROptions::$imageTransparent QROptions::$imageTransparent} is set to true.
@@ -259,15 +250,10 @@ trait QROptionsTrait{
 	 */
 	protected $transparencyColor = null;
 
-	/**
-	 * Sets the image background color (if applicable)
-	 *
-	 * - QRGdImage: defaults to "white"
-	 * - QRImagick: defaults to [255, 255, 255]
-	 *
-	 * @var mixed|null
+
+	/*
+	 * QRGdImage settings
 	 */
-	protected $bgColor = null;
 
 	/**
 	 * @see imagepng()
@@ -279,6 +265,11 @@ trait QROptionsTrait{
 	 */
 	protected int $jpegQuality = 85;
 
+
+	/*
+	 * QRImagick settings
+	 */
+
 	/**
 	 * Imagick output format
 	 *
@@ -287,66 +278,133 @@ trait QROptionsTrait{
 	 */
 	protected string $imagickFormat = 'png32';
 
+
+	/*
+	 * Common markup output settings (QRMarkupSVG, QRMarkupHTML)
+	 */
+
 	/**
-	 * Measurement unit for FPDF output: pt, mm, cm, in (defaults to "pt")
-	 *
-	 * @see \FPDF::__construct()
+	 * a common css class
 	 */
-	protected string $fpdfMeasureUnit = 'pt';
+	protected string $cssClass = 'qrcode';
 
 	/**
-	 * Module values map
+	 * markup substitute for dark (CSS value)
+	 */
+	protected string $markupDark = '#000';
+
+	/**
+	 * markup substitute for light (CSS value)
+	 */
+	protected string $markupLight = '#fff';
+
+
+	/*
+	 * QRMarkupSVG settings
+	 */
+
+	/**
+	 * SVG opacity
+	 */
+	protected float $svgOpacity = 1.0;
+
+	/**
+	 * anything between <defs>
 	 *
-	 *   - HTML, IMAGICK: #ABCDEF, cssname, rgb(), rgba()...
-	 *   - IMAGE: [63, 127, 255] // R, G, B
+	 * @see https://developer.mozilla.org/en-US/docs/Web/SVG/Element/defs
 	 */
-	protected ?array $moduleValues = null;
+	protected string $svgDefs = '';
 
 	/**
-	 * use Imagick (if available) when reading QR Codes
+	 * SVG viewBox size. a single integer number which defines width/height of the viewBox attribute.
+	 *
+	 * viewBox="0 0 x x"
+	 *
+	 * @see https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/viewBox
+	 * @see https://css-tricks.com/scale-svg/#article-header-id-3
 	 */
-	protected bool $readerUseImagickIfAvailable = false;
+	protected ?int $svgViewBoxSize = null;
 
 	/**
-	 * grayscale the image before reading
+	 * @see https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/preserveAspectRatio
 	 */
-	protected bool $readerGrayscale = false;
+	protected string $svgPreserveAspectRatio = 'xMidYMid';
 
 	/**
-	 * increase the contrast before reading
+	 * optional "width" attribute with the specified value (note that the value is not checked!)
 	 *
-	 * note that applying contrast works different in GD and Imagick, so mileage may vary
+	 * @see https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/width
 	 */
-	protected bool $readerIncreaseContrast = false;
+	protected ?string $svgWidth = null;
 
 	/**
-	 * Toggles logo space creation
+	 * optional "height" attribute with the specified value (note that the value is not checked!)
+	 *
+	 * @see https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/height
 	 */
-	protected bool $addLogoSpace = false;
+	protected ?string $svgHeight = null;
 
 	/**
-	 * width of the logo space
+	 * whether to connect the paths for the several module types to avoid weird glitches when using gradients etc.
 	 *
-	 * if only either $logoSpaceWidth or $logoSpaceHeight is given, the logo space is assumed a square of that size
+	 * @see https://github.com/chillerlan/php-qrcode/issues/57
 	 */
-	protected ?int $logoSpaceWidth = null;
+	protected bool $connectPaths = false;
 
 	/**
-	 * height of the logo space
+	 * specify which paths/patterns to exclude from connecting if $svgConnectPaths is set to true
+	 */
+	protected array $excludeFromConnect = [];
+
+
+	/*
+	 * QRString settings
+	 */
+
+	/**
+	 * string substitute for dark
+	 */
+	protected string $textDark = '🔴';
+
+	/**
+	 * string substitute for light
+	 */
+	protected string $textLight = '⭕';
+
+
+	/*
+	 * QRFpdf settings
+	 */
+
+	/**
+	 * Measurement unit for FPDF output: pt, mm, cm, in (defaults to "pt")
 	 *
-	 * if only either $logoSpaceWidth or $logoSpaceHeight is given, the logo space is assumed a square of that size
+	 * @see \FPDF::__construct()
+	 */
+	protected string $fpdfMeasureUnit = 'pt';
+
+
+	/*
+	 * QR Code reader settings
 	 */
-	protected ?int $logoSpaceHeight = null;
 
 	/**
-	 * optional horizontal start position of the logo space (top left corner)
+	 * use Imagick (if available) when reading QR Codes
 	 */
-	protected ?int $logoSpaceStartX = null;
+	protected bool $readerUseImagickIfAvailable = false;
 
 	/**
-	 * optional vertical start position of the logo space (top left corner)
+	 * grayscale the image before reading
 	 */
-	protected ?int $logoSpaceStartY = null;
+	protected bool $readerGrayscale = false;
+
+	/**
+	 * increase the contrast before reading
+	 *
+	 * note that applying contrast works different in GD and Imagick, so mileage may vary
+	 */
+	protected bool $readerIncreaseContrast = false;
+
 
 	/**
 	 * clamp min/max version number

+ 1 - 1
tests/QROptionsTest.php

@@ -12,7 +12,7 @@
 
 namespace chillerlan\QRCodeTest;
 
-use chillerlan\QRCode\{QRCodeException, QROptions};
+use chillerlan\QRCode\QROptions;
 use chillerlan\QRCode\Common\Version;
 use PHPUnit\Framework\TestCase;