Debian Codegraph @

vendor/chillerlan/php-qrcode/src/Output/RGBArrayModuleValueTrait.php

raw
1 <?php
2 /**
3 * RGBArrayModuleValueTrait.php
4 *
5 * @created 06.05.2024
6 * @author smiley <[email protected]>
7 * @copyright 2024 smiley
8 * @license MIT
9 *
10 * @phan-file-suppress PhanTypeMismatchDeclaredParamNullable
11 */
12 declare(strict_types=1);
13
14 namespace chillerlan\QRCode\Output;
15
16 use function array_values, count, intval, is_array, is_numeric, max, min;
17
18 /**
19 * Module value checks for output classes that use RGB color arrays
20 */
21 trait RGBArrayModuleValueTrait{
22
23 /**
24 * implements \chillerlan\QRCode\Output\QROutputInterface::moduleValueIsValid()
25 *
26 * @param int[] $value
27 */
28 public static function moduleValueIsValid(mixed $value):bool{
29
30 if(!is_array($value) || count($value) < 3){
31 return false;
32 }
33
34 // check the first 3 values of the array
35 foreach(array_values($value) as $i => $val){
36
37 if($i > 2){
38 break;
39 }
40
41 if(!is_numeric($val)){
42 return false;
43 }
44
45 }
46
47 return true;
48 }
49
50 /**
51 * implements \chillerlan\QRCode\Output\QROutputAbstract::prepareModuleValue()
52 *
53 * @param int[] $value
54 * @return int[]
55 *
56 * @throws \chillerlan\QRCode\Output\QRCodeOutputException
57 */
58 protected function prepareModuleValue(mixed $value):array{
59 $values = [];
60
61 foreach(array_values($value) as $i => $val){
62
63 if($i > 2){
64 break;
65 }
66
67 $values[] = max(0, min(255, intval($val)));
68 }
69
70 if(count($values) !== 3){
71 throw new QRCodeOutputException('invalid color value');
72 }
73
74 return $values;
75 }
76
77 /**
78 * implements \chillerlan\QRCode\Output\QROutputAbstract::getDefaultModuleValue()
79 *
80 * @return int[]
81 */
82 protected function getDefaultModuleValue(bool $isDark):array{
83 return ($isDark) ? [0, 0, 0] : [255, 255, 255];
84 }
85
86 }