6
6
use Picqer \Barcode \BarcodeBar ;
7
7
use Picqer \Barcode \Exceptions \InvalidCharacterException ;
8
8
use Picqer \Barcode \Exceptions \InvalidLengthException ;
9
- use Picqer \Barcode \Types \TypeInterface ;
10
9
11
10
class TypeITF14 implements TypeInterface
12
11
{
@@ -16,9 +15,7 @@ class TypeITF14 implements TypeInterface
16
15
*/
17
16
public function getBarcodeData (string $ code ): Barcode
18
17
{
19
- $ barcode = new Barcode ($ code );
20
-
21
- $ chr = array ();
18
+ $ chr = [];
22
19
$ chr ['0 ' ] = '11221 ' ;
23
20
$ chr ['1 ' ] = '21112 ' ;
24
21
$ chr ['2 ' ] = '12112 ' ;
@@ -29,84 +26,62 @@ public function getBarcodeData(string $code): Barcode
29
26
$ chr ['7 ' ] = '11122 ' ;
30
27
$ chr ['8 ' ] = '21121 ' ;
31
28
$ chr ['9 ' ] = '12121 ' ;
29
+ $ chr ['A ' ] = '11 ' ;
30
+ $ chr ['Z ' ] = '21 ' ;
32
31
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 ) {
51
33
throw new InvalidLengthException ();
52
34
}
53
35
54
- $ k = 0 ;
55
- $ pbegin = " 1010 " ;
56
- $ pbeginarr = str_split ( $ pbegin );
36
+ if ( strlen ( $ code ) === 13 ) {
37
+ $ code .= $ this -> getChecksum ( $ code ) ;
38
+ }
57
39
58
- foreach ($ pbeginarr as $ x ) {
59
- $ t = $ x === '1 ' ;
60
- $ w = 1 ;
40
+ $ barcode = new Barcode ($ code );
61
41
62
- $ barcode ->addBar (new BarcodeBar ($ w , 1 , $ t ));
63
- ++$ k ;
64
- }
42
+ // Add start and stop codes
43
+ $ code = 'AA ' . strtolower ($ code ) . 'ZA ' ;
65
44
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 ]])) {
68
48
throw new InvalidCharacterException ();
69
49
}
70
50
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 = '' ;
76
55
77
56
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 );
80
59
$ pspaces = substr ($ pspaces , 1 );
81
60
}
82
61
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 ;
96
65
}
97
66
}
98
67
99
- $ pend = " 1101 " ;
100
- $ pendarr = str_split ( $ pend );
68
+ return $ barcode ;
69
+ }
101
70
102
- foreach ( $ pendarr as $ x ) {
103
- $ t = $ x == ' 1 ' ;
104
- $ w = 1 ;
71
+ private function getChecksum ( string $ code ): string
72
+ {
73
+ $ total = 0 ;
105
74
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 );
108
78
}
109
79
110
- return $ barcode ;
80
+ $ checksum = 10 - ($ total % 10 );
81
+ if ($ checksum === 10 ) {
82
+ $ checksum = 0 ;
83
+ }
84
+
85
+ return (string )$ checksum ;
111
86
}
112
87
}
0 commit comments