@@ -29,25 +29,26 @@ public function getBarcodeData(string $code): Barcode
2929 $ chr ['A ' ] = '11 ' ;
3030 $ chr ['Z ' ] = '21 ' ;
3131
32- if (strlen ($ code ) === 13 ) {
33- $ code .= $ this -> getChecksum ( $ code );
32+ if (strlen ($ code ) < 13 || strlen ( $ code ) > 14 ) {
33+ throw new InvalidLengthException ( );
3434 }
3535
36- if (strlen ($ code ) > 14 || strlen ( $ code ) < 13 ) {
37- throw new InvalidLengthException ( );
36+ if (strlen ($ code ) === 13 ) {
37+ $ code .= $ this -> getChecksum ( $ code );
3838 }
3939
4040 $ barcode = new Barcode ($ code );
4141
4242 // Add start and stop codes
4343 $ code = 'AA ' . strtolower ($ code ) . 'ZA ' ;
4444
45+ // Loop through 2 chars at once
4546 for ($ charIndex = 0 ; $ charIndex < strlen ($ code ); $ charIndex += 2 ) {
4647 if (! isset ($ chr [$ code [$ charIndex ]]) || ! isset ($ chr [$ code [$ charIndex + 1 ]])) {
4748 throw new InvalidCharacterException ();
4849 }
4950
50- $ bars = true ;
51+ $ drawBar = true ;
5152 $ pbars = $ chr [$ code [$ charIndex ]];
5253 $ pspaces = $ chr [$ code [$ charIndex + 1 ]];
5354 $ pmixed = '' ;
@@ -58,18 +59,9 @@ public function getBarcodeData(string $code): Barcode
5859 $ pspaces = substr ($ pspaces , 1 );
5960 }
6061
61- $ pmixedarr = str_split ($ pmixed );
62-
63- foreach ($ pmixedarr as $ x ) {
64- if ($ bars ) {
65- $ t = true ;
66- } else {
67- $ t = false ;
68- }
69- $ width = ($ x === '1 ' ) ? '1 ' : '2 ' ;
70-
71- $ barcode ->addBar (new BarcodeBar ($ width , 1 , $ t ));
72- $ bars = ! $ bars ;
62+ foreach (str_split ($ pmixed ) as $ width ) {
63+ $ barcode ->addBar (new BarcodeBar ($ width , 1 , $ drawBar ));
64+ $ drawBar = ! $ drawBar ;
7365 }
7466 }
7567
0 commit comments