-
-
Notifications
You must be signed in to change notification settings - Fork 292
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
de3d321
commit 74dc3b2
Showing
6 changed files
with
117 additions
and
106 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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], | ||
]; | ||
} | ||
|
||
} |