66use Picqer \Barcode \BarcodeBar ;
77use Picqer \Barcode \Exceptions \InvalidCharacterException ;
88use Picqer \Barcode \Exceptions \InvalidLengthException ;
9- use Picqer \Barcode \Types \TypeInterface ;
109
1110class TypeITF14 implements TypeInterface
1211{
@@ -16,9 +15,7 @@ class TypeITF14 implements TypeInterface
1615 */
1716 public function getBarcodeData (string $ code ): Barcode
1817 {
19- $ barcode = new Barcode ($ code );
20-
21- $ chr = array ();
18+ $ chr = [];
2219 $ chr ['0 ' ] = '11221 ' ;
2320 $ chr ['1 ' ] = '21112 ' ;
2421 $ chr ['2 ' ] = '12112 ' ;
@@ -29,84 +26,62 @@ public function getBarcodeData(string $code): Barcode
2926 $ chr ['7 ' ] = '11122 ' ;
3027 $ chr ['8 ' ] = '21121 ' ;
3128 $ chr ['9 ' ] = '12121 ' ;
29+ $ chr ['A ' ] = '11 ' ;
30+ $ chr ['Z ' ] = '21 ' ;
3231
33- if (strlen ($ code ) === 13 ) {
34- $ total = 0 ;
35-
36- for ($ i = 0 ; $ i <= strlen ($ code ) - 1 ; $ i ++) {
37- $ temp = intval ($ code . substr ($ i , 1 ));
38- $ total += $ temp * (($ i === 0 || $ i % 2 === 0 ) ? 3 : 1 );
39- }
40-
41- $ cs = $ total % 10 ;
42- $ cs = 10 - $ cs ;
43- if ($ cs === 10 ) {
44- $ cs = 0 ;
45- }
46-
47- $ code .= (string ) $ cs ;
48- }
49-
50- if (strlen ($ code ) > 14 || strlen ($ code ) < 13 ) {
32+ if (strlen ($ code ) < 13 || strlen ($ code ) > 14 ) {
5133 throw new InvalidLengthException ();
5234 }
5335
54- $ k = 0 ;
55- $ pbegin = " 1010 " ;
56- $ pbeginarr = str_split ( $ pbegin );
36+ if ( strlen ( $ code ) === 13 ) {
37+ $ code .= $ this -> getChecksum ( $ code ) ;
38+ }
5739
58- foreach ($ pbeginarr as $ x ) {
59- $ t = $ x === '1 ' ;
60- $ w = 1 ;
40+ $ barcode = new Barcode ($ code );
6141
62- $ barcode ->addBar (new BarcodeBar ($ w , 1 , $ t ));
63- ++$ k ;
64- }
42+ // Add start and stop codes
43+ $ code = 'AA ' . strtolower ($ code ) . 'ZA ' ;
6544
66- for ($ i = 0 ; $ i < strlen ($ code ); $ i += 2 ) {
67- if (!isset ($ chr [$ code [$ i ]]) || !isset ($ chr [$ code [$ i + 1 ]])) {
45+ // Loop through 2 chars at once
46+ for ($ charIndex = 0 ; $ charIndex < strlen ($ code ); $ charIndex += 2 ) {
47+ if (! isset ($ chr [$ code [$ charIndex ]]) || ! isset ($ chr [$ code [$ charIndex + 1 ]])) {
6848 throw new InvalidCharacterException ();
6949 }
7050
71- $ bars = true ;
72- $ pbars = $ chr [$ code [$ i ]];
73- $ pspaces = $ chr [$ code [$ i + 1 ]];
74- $ pmixed = "" ;
75-
51+ $ drawBar = true ;
52+ $ pbars = $ chr [$ code [$ charIndex ]];
53+ $ pspaces = $ chr [$ code [$ charIndex + 1 ]];
54+ $ pmixed = '' ;
7655
7756 while (strlen ($ pbars ) > 0 ) {
78- $ pmixed .= $ pbars [0 ] . $ pspaces [0 ];
79- $ pbars = substr ($ pbars , 1 );
57+ $ pmixed .= $ pbars [0 ] . $ pspaces [0 ];
58+ $ pbars = substr ($ pbars , 1 );
8059 $ pspaces = substr ($ pspaces , 1 );
8160 }
8261
83- $ pmixedarr = str_split ($ pmixed );
84-
85- foreach ($ pmixedarr as $ x ) {
86- if ($ bars ) {
87- $ t = true ;
88- } else {
89- $ t = false ;
90- }
91- $ w = ($ x === '1 ' ) ? '1 ' : '2 ' ;
92-
93- $ barcode ->addBar (new BarcodeBar ($ w , 1 , $ t ));
94- $ bars = !$ bars ;
95- ++$ k ;
62+ foreach (str_split ($ pmixed ) as $ width ) {
63+ $ barcode ->addBar (new BarcodeBar ($ width , 1 , $ drawBar ));
64+ $ drawBar = ! $ drawBar ;
9665 }
9766 }
9867
99- $ pend = " 1101 " ;
100- $ pendarr = str_split ( $ pend );
68+ return $ barcode ;
69+ }
10170
102- foreach ( $ pendarr as $ x ) {
103- $ t = $ x == ' 1 ' ;
104- $ w = 1 ;
71+ private function getChecksum ( string $ code ): string
72+ {
73+ $ total = 0 ;
10574
106- $ barcode ->addBar (new BarcodeBar ($ w , 1 , $ t ));
107- ++$ k ;
75+ for ($ charIndex = 0 ; $ charIndex <= (strlen ($ code ) - 1 ); $ charIndex ++) {
76+ $ integerOfChar = intval ($ code . substr ($ charIndex , 1 ));
77+ $ total += $ integerOfChar * (($ charIndex === 0 || $ charIndex % 2 === 0 ) ? 3 : 1 );
10878 }
10979
110- return $ barcode ;
80+ $ checksum = 10 - ($ total % 10 );
81+ if ($ checksum === 10 ) {
82+ $ checksum = 0 ;
83+ }
84+
85+ return (string )$ checksum ;
11186 }
11287}
0 commit comments