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

:octocat: extract RGBArrayModuleValueTrait

smiley 1 год назад
Родитель
Сommit
74dc3b2dbf

+ 3 - 57
src/Output/QRFpdf.php

@@ -16,7 +16,7 @@ use chillerlan\QRCode\Data\QRMatrix;
 use chillerlan\Settings\SettingsContainerInterface;
 use FPDF;
 
-use function array_values, class_exists, count, intval, is_array, is_numeric, max, min;
+use function class_exists;
 
 /**
  * QRFpdf output module (requires fpdf)
@@ -25,6 +25,7 @@ use function array_values, class_exists, count, intval, is_array, is_numeric, ma
  * @see http://www.fpdf.org/
  */
 class QRFpdf extends QROutputAbstract{
+	use RGBArrayModuleValueTrait;
 
 	final public const MIME_TYPE = 'application/pdf';
 
@@ -42,7 +43,7 @@ class QRFpdf extends QROutputAbstract{
 			// @codeCoverageIgnoreStart
 			throw new QRCodeOutputException(
 				'The QRFpdf output requires FPDF (https://github.com/Setasign/FPDF)'.
-				' as dependency but the class "\\FPDF" couldn\'t be found.'
+				' as dependency but the class "\\FPDF" could not be found.'
 			);
 			// @codeCoverageIgnoreEnd
 		}
@@ -50,61 +51,6 @@ class QRFpdf extends QROutputAbstract{
 		parent::__construct($options, $matrix);
 	}
 
-	/**
-	 * @inheritDoc
-	 */
-	public static function moduleValueIsValid(mixed $value):bool{
-
-		if(!is_array($value) || count($value) < 3){
-			return false;
-		}
-
-		// check the first 3 values of the array
-		foreach(array_values($value) as $i => $val){
-
-			if($i > 2){
-				break;
-			}
-
-			if(!is_numeric($val)){
-				return false;
-			}
-
-		}
-
-		return true;
-	}
-
-	/**
-	 * @inheritDoc
-	 * @throws \chillerlan\QRCode\Output\QRCodeOutputException
-	 */
-	protected function prepareModuleValue(mixed $value):array{
-		$values = [];
-
-		foreach(array_values($value) as $i => $val){
-
-			if($i > 2){
-				break;
-			}
-
-			$values[] = max(0, min(255, intval($val)));
-		}
-
-		if(count($values) !== 3){
-			throw new QRCodeOutputException('invalid color value');
-		}
-
-		return $values;
-	}
-
-	/**
-	 * @inheritDoc
-	 */
-	protected function getDefaultModuleValue(bool $isDark):array{
-		return ($isDark) ? [0, 0, 0] : [255, 255, 255];
-	}
-
 	/**
 	 * Initializes an FPDF instance
 	 */

+ 3 - 27
src/Output/QRGdImage.php

@@ -16,9 +16,9 @@ use chillerlan\QRCode\QROptions;
 use chillerlan\QRCode\Data\QRMatrix;
 use chillerlan\Settings\SettingsContainerInterface;
 use ErrorException, GdImage, Throwable;
-use function array_values, count, extension_loaded, imagecolorallocate, imagecolortransparent,
+use function extension_loaded, imagecolorallocate, imagecolortransparent,
 	imagecreatetruecolor, imagedestroy, imagefilledellipse, imagefilledrectangle,
-	imagescale, imagetypes, intdiv, intval, is_array, is_numeric, max, min, ob_end_clean, ob_get_contents, ob_start,
+	imagescale, imagetypes, intdiv, intval, max, min, ob_end_clean, ob_get_contents, ob_start,
 	restore_error_handler, set_error_handler, sprintf;
 use const IMG_AVIF, IMG_BMP, IMG_GIF, IMG_JPG, IMG_PNG, IMG_WEBP;
 
@@ -29,6 +29,7 @@ use const IMG_AVIF, IMG_BMP, IMG_GIF, IMG_JPG, IMG_PNG, IMG_WEBP;
  * @see https://github.com/chillerlan/php-qrcode/issues/223
  */
 abstract class QRGdImage extends QROutputAbstract{
+	use RGBArrayModuleValueTrait;
 
 	/**
 	 * The GD image resource
@@ -105,31 +106,6 @@ abstract class QRGdImage extends QROutputAbstract{
 
 	}
 
-	/**
-	 * @inheritDoc
-	 */
-	public static function moduleValueIsValid(mixed $value):bool{
-
-		if(!is_array($value) || count($value) < 3){
-			return false;
-		}
-
-		// check the first 3 values of the array
-		foreach(array_values($value) as $i => $val){
-
-			if($i > 2){
-				break;
-			}
-
-			if(!is_numeric($val)){
-				return false;
-			}
-
-		}
-
-		return true;
-	}
-
 	/**
 	 * @inheritDoc
 	 * @throws \chillerlan\QRCode\Output\QRCodeOutputException

+ 78 - 0
src/Output/RGBArrayModuleValueTrait.php

@@ -0,0 +1,78 @@
+<?php
+/**
+ * RGBArrayModuleValueTrait.php
+ *
+ * @created      06.05.2024
+ * @author       smiley <smiley@chillerlan.net>
+ * @copyright    2024 smiley
+ * @license      MIT
+ */
+
+namespace chillerlan\QRCode\Output;
+
+use function array_values, count, intval, is_array, is_numeric, max, min;
+
+/**
+ * Module value checks for output classes that use RGB color arrays
+ */
+trait RGBArrayModuleValueTrait{
+
+	/**
+	 * @implements \chillerlan\QRCode\Output\QROutputInterface::moduleValueIsValid()
+	 * @inheritDoc
+	 */
+	public static function moduleValueIsValid(mixed $value):bool{
+
+		if(!is_array($value) || count($value) < 3){
+			return false;
+		}
+
+		// check the first 3 values of the array
+		foreach(array_values($value) as $i => $val){
+
+			if($i > 2){
+				break;
+			}
+
+			if(!is_numeric($val)){
+				return false;
+			}
+
+		}
+
+		return true;
+	}
+
+	/**
+	 * @implements \chillerlan\QRCode\Output\QROutputAbstract::prepareModuleValue()
+	 * @inheritDoc
+	 * @throws \chillerlan\QRCode\Output\QRCodeOutputException
+	 */
+	protected function prepareModuleValue(mixed $value):array{
+		$values = [];
+
+		foreach(array_values($value) as $i => $val){
+
+			if($i > 2){
+				break;
+			}
+
+			$values[] = max(0, min(255, intval($val)));
+		}
+
+		if(count($values) !== 3){
+			throw new QRCodeOutputException('invalid color value');
+		}
+
+		return $values;
+	}
+
+	/**
+	 * @implements \chillerlan\QRCode\Output\QROutputAbstract::getDefaultModuleValue()
+	 * @inheritDoc
+	 */
+	protected function getDefaultModuleValue(bool $isDark):array{
+		return ($isDark) ? [0, 0, 0] : [255, 255, 255];
+	}
+
+}

+ 1 - 11
tests/Output/QRFpdfTest.php

@@ -21,6 +21,7 @@ use function class_exists;
  * Tests the QRFpdf output module
  */
 final class QRFpdfTest extends QROutputTestAbstract{
+	use RGBArrayModuleValueProviderTrait;
 
 	/**
 	 * @inheritDoc
@@ -41,17 +42,6 @@ final class QRFpdfTest extends QROutputTestAbstract{
 		return new QRFpdf($options, $matrix);
 	}
 
-	public static function moduleValueProvider():array{
-		return [
-			'valid: int'                     => [[123, 123, 123], true],
-			'valid: w/invalid extra element' => [[123, 123, 123, 'abc'], true],
-			'valid: numeric string'          => [['123', '123', '123'], true],
-			'invalid: wrong type'            => ['foo', false],
-			'invalid: array too short'       => [[1, 2], false],
-			'invalid: contains non-number'   => [[1, 'b', 3], false],
-		];
-	}
-
 	/**
 	 * @inheritDoc
 	 */

+ 1 - 11
tests/Output/QRGdImageTestAbstract.php

@@ -20,6 +20,7 @@ use function extension_loaded;
  * Tests the QRGdImage output module
  */
 abstract class QRGdImageTestAbstract extends QROutputTestAbstract{
+	use RGBArrayModuleValueProviderTrait;
 
 	/**
 	 * @inheritDoc
@@ -33,17 +34,6 @@ abstract class QRGdImageTestAbstract extends QROutputTestAbstract{
 		parent::setUp();
 	}
 
-	public static function moduleValueProvider():array{
-		return [
-			'valid: int'                     => [[123, 123, 123], true],
-			'valid: w/invalid extra element' => [[123, 123, 123, 'abc'], true],
-			'valid: numeric string'          => [['123', '123', '123'], true],
-			'invalid: wrong type'            => ['foo', false],
-			'invalid: array too short'       => [[1, 2], false],
-			'invalid: contains non-number'   => [[1, 'b', 3], false],
-		];
-	}
-
 	/**
 	 * @inheritDoc
 	 */

+ 31 - 0
tests/Output/RGBArrayModuleValueProviderTrait.php

@@ -0,0 +1,31 @@
+<?php
+/**
+ * RGBArrayModuleValueProviderTrait.php
+ *
+ * @created      06.05.2024
+ * @author       smiley <smiley@chillerlan.net>
+ * @copyright    2024 smiley
+ * @license      MIT
+ */
+
+namespace chillerlan\QRCodeTest\Output;
+
+/**
+ * A data provider for use in tests that include RGBArrayModuleValueTrait
+ *
+ * @see \chillerlan\QRCode\Output\RGBArrayModuleValueTrait
+ */
+trait RGBArrayModuleValueProviderTrait{
+
+	public static function moduleValueProvider():array{
+		return [
+			'valid: int'                     => [[123, 123, 123], true],
+			'valid: w/invalid extra element' => [[123, 123, 123, 'abc'], true],
+			'valid: numeric string'          => [['123', '123', '123'], true],
+			'invalid: wrong type'            => ['foo', false],
+			'invalid: array too short'       => [[1, 2], false],
+			'invalid: contains non-number'   => [[1, 'b', 3], false],
+		];
+	}
+
+}