Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
23 / 23 |
|
100.00% |
8 / 8 |
CRAP | |
100.00% |
1 / 1 |
CodeAbstract | |
100.00% |
23 / 23 |
|
100.00% |
8 / 8 |
11 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
setDimension | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
3 | |||
setMargin | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
setOrientation | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
getContent | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
setContent | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
saveToPngFile | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
saveToJpgFile | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
get | n/a |
0 / 0 |
n/a |
0 / 0 |
0 |
1 | <?php |
2 | /** |
3 | * Jingga |
4 | * |
5 | * PHP Version 8.1 |
6 | * |
7 | * @package phpOMS\Utils\Barcode |
8 | * @copyright Dennis Eichhorn |
9 | * @license OMS License 2.0 |
10 | * @version 1.0.0 |
11 | * @link https://jingga.app |
12 | */ |
13 | declare(strict_types=1); |
14 | |
15 | namespace phpOMS\Utils\Barcode; |
16 | |
17 | use phpOMS\Stdlib\Base\Exception\InvalidEnumValue; |
18 | |
19 | /** |
20 | * Code class. |
21 | * |
22 | * @package phpOMS\Utils\Barcode |
23 | * @license OMS License 2.0 |
24 | * @link https://jingga.app |
25 | * @since 1.0.0 |
26 | * |
27 | * @SuppressWarnings(PHPMD.CamelCasePropertyName) |
28 | * @SuppressWarnings(PHPMD.CamelCaseVariableName) |
29 | */ |
30 | abstract class CodeAbstract |
31 | { |
32 | /** |
33 | * Code. |
34 | * |
35 | * @var string |
36 | * @since 1.0.0 |
37 | */ |
38 | protected string $codestring = ''; |
39 | |
40 | /** |
41 | * Code. |
42 | * |
43 | * Bool array since I don't know the internal size of a char. |
44 | * Bool should be at least same internal size or smaller (has no length). |
45 | * In a different programming language char is most likeley better. |
46 | * |
47 | * You could even consider to change from 2D to 1D by using strings and than iterate the string. |
48 | * |
49 | * @var array<int, array<int, bool>> |
50 | * @since 1.0.0 |
51 | */ |
52 | protected array $codearray = [[]]; |
53 | |
54 | /** |
55 | * Orientation. |
56 | * |
57 | * @var int |
58 | * @since 1.0.0 |
59 | */ |
60 | protected int $orientation = 0; |
61 | |
62 | /** |
63 | * Barcode dimension. |
64 | * |
65 | * @var int[] |
66 | * @since 1.0.0 |
67 | */ |
68 | protected array $dimension = ['width' => 0, 'height' => 0]; |
69 | |
70 | /** |
71 | * Barcode dimension. |
72 | * |
73 | * @var int |
74 | * @since 1.0.0 |
75 | */ |
76 | protected int $margin = 10; |
77 | |
78 | /** |
79 | * Content to encrypt. |
80 | * |
81 | * @var string |
82 | * @since 1.0.0 |
83 | */ |
84 | protected string $content = ''; |
85 | |
86 | /** |
87 | * Background color. |
88 | * |
89 | * @var int[] |
90 | * @since 1.0.0 |
91 | */ |
92 | protected array $background = ['r' => 0, 'g' => 0, 'b' => 0, 'a' => 0]; |
93 | |
94 | /** |
95 | * Front color. |
96 | * |
97 | * @var int[] |
98 | * @since 1.0.0 |
99 | */ |
100 | protected array $front = ['r' => 0, 'g' => 0, 'b' => 0, 'a' => 0]; |
101 | |
102 | /** |
103 | * Constructor |
104 | * |
105 | * @param string $content Content to encrypt |
106 | * @param int $width Barcode width |
107 | * @param int $height Barcode height |
108 | * @param int $orientation Orientation of the barcode |
109 | * |
110 | * @since 1.0.0 |
111 | */ |
112 | public function __construct(string $content = '', int $width = 100, int $height = 20, int $orientation = OrientationType::HORIZONTAL) |
113 | { |
114 | $this->setContent($content); |
115 | $this->setDimension($width, $height); |
116 | $this->setOrientation($orientation); |
117 | } |
118 | |
119 | /** |
120 | * Set barcode dimensions |
121 | * |
122 | * @param int $width Barcode width |
123 | * @param int $height Barcode height |
124 | * |
125 | * @return void |
126 | * |
127 | * @throws \OutOfBoundsException |
128 | * |
129 | * @since 1.0.0 |
130 | */ |
131 | public function setDimension(int $width, int $height) : void |
132 | { |
133 | if ($width < 0) { |
134 | throw new \OutOfBoundsException((string) $width); |
135 | } |
136 | |
137 | if ($height < 0) { |
138 | throw new \OutOfBoundsException((string) $height); |
139 | } |
140 | |
141 | $this->dimension['width'] = $width; |
142 | $this->dimension['height'] = $height; |
143 | } |
144 | |
145 | /** |
146 | * Set barcode margins |
147 | * |
148 | * @param int $margin Barcode margin |
149 | * |
150 | * @return void |
151 | * |
152 | * @since 1.0.0 |
153 | */ |
154 | public function setMargin(int $margin) : void |
155 | { |
156 | $this->margin = $margin; |
157 | } |
158 | |
159 | /** |
160 | * Set barcode orientation |
161 | * |
162 | * @param int $orientation Barcode orientation |
163 | * |
164 | * @return void |
165 | * |
166 | * @throws InvalidEnumValue |
167 | * |
168 | * @since 1.0.0 |
169 | */ |
170 | public function setOrientation(int $orientation) : void |
171 | { |
172 | if (!OrientationType::isValidValue($orientation)) { |
173 | throw new InvalidEnumValue($orientation); |
174 | } |
175 | |
176 | $this->orientation = $orientation; |
177 | } |
178 | |
179 | /** |
180 | * Get content |
181 | * |
182 | * @return string Returns the string representation of the code |
183 | * |
184 | * @since 1.0.0 |
185 | */ |
186 | public function getContent() : string |
187 | { |
188 | return $this->content; |
189 | } |
190 | |
191 | /** |
192 | * Set content to encrypt |
193 | * |
194 | * @param string $content Barcode content |
195 | * |
196 | * @return void |
197 | * |
198 | * @since 1.0.0 |
199 | */ |
200 | public function setContent(string $content) : void |
201 | { |
202 | $this->content = $content; |
203 | |
204 | $this->codestring = ''; |
205 | $this->codearray = []; |
206 | } |
207 | |
208 | /** |
209 | * Save to file |
210 | * |
211 | * @param string $file File path/name |
212 | * |
213 | * @return void |
214 | * |
215 | * @since 1.0.0 |
216 | */ |
217 | public function saveToPngFile(string $file) : void |
218 | { |
219 | $res = $this->get(); |
220 | |
221 | \imagepng($res, $file); |
222 | \imagedestroy($res); |
223 | } |
224 | |
225 | /** |
226 | * Save to file |
227 | * |
228 | * @param string $file File path/name |
229 | * |
230 | * @return void |
231 | * |
232 | * @since 1.0.0 |
233 | */ |
234 | public function saveToJpgFile(string $file) : void |
235 | { |
236 | $res = $this->get(); |
237 | |
238 | \imagejpeg($res, $file); |
239 | \imagedestroy($res); |
240 | } |
241 | |
242 | /** |
243 | * Get image reference |
244 | * |
245 | * @return \GdImage |
246 | * |
247 | * @since 1.0.0 |
248 | */ |
249 | abstract public function get() : mixed; |
250 | } |