Skip to content

Commit

Permalink
:octocat: extract RGBArrayModuleValueTrait
Browse files Browse the repository at this point in the history
  • Loading branch information
codemasher committed May 6, 2024
1 parent de3d321 commit 74dc3b2
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 106 deletions.
60 changes: 3 additions & 57 deletions src/Output/QRFpdf.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
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)
Expand All @@ -25,6 +25,7 @@
* @see http://www.fpdf.org/
*/
class QRFpdf extends QROutputAbstract{
use RGBArrayModuleValueTrait;

final public const MIME_TYPE = 'application/pdf';

Expand All @@ -42,69 +43,14 @@ public function __construct(SettingsContainerInterface|QROptions $options, QRMat
// @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
}

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
*/
Expand Down
30 changes: 3 additions & 27 deletions src/Output/QRGdImage.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
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;

Expand All @@ -29,6 +29,7 @@
* @see https://github.com/chillerlan/php-qrcode/issues/223
*/
abstract class QRGdImage extends QROutputAbstract{
use RGBArrayModuleValueTrait;

/**
* The GD image resource
Expand Down Expand Up @@ -105,31 +106,6 @@ protected function checkGD():void{

}

/**
* @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
Expand Down
78 changes: 78 additions & 0 deletions src/Output/RGBArrayModuleValueTrait.php
Original file line number Diff line number Diff line change
@@ -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];
}

}
12 changes: 1 addition & 11 deletions tests/Output/QRFpdfTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
* Tests the QRFpdf output module
*/
final class QRFpdfTest extends QROutputTestAbstract{
use RGBArrayModuleValueProviderTrait;

/**
* @inheritDoc
Expand All @@ -41,17 +42,6 @@ protected function getOutputInterface(
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
*/
Expand Down
12 changes: 1 addition & 11 deletions tests/Output/QRGdImageTestAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
* Tests the QRGdImage output module
*/
abstract class QRGdImageTestAbstract extends QROutputTestAbstract{
use RGBArrayModuleValueProviderTrait;

/**
* @inheritDoc
Expand All @@ -33,17 +34,6 @@ protected function setUp():void{
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
*/
Expand Down
31 changes: 31 additions & 0 deletions tests/Output/RGBArrayModuleValueProviderTrait.php
Original file line number Diff line number Diff line change
@@ -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],
];
}

}

0 comments on commit 74dc3b2

Please sign in to comment.