FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
bardecoder/src/HIBCSecondaryDataDecoder.php at main · brixion/bardecoder · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
brixion
/
bardecoder
Public
Notifications
You must be signed in to change notification settings
Fork
0
Star
1
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
bardecoder
/
src
/
HIBCSecondaryDataDecoder.php
Copy path
More file actions
More file actions
Latest commit
History
History
History
304 lines (287 loc) · 12.7 KB
Breadcrumbs
bardecoder
/
src
/
HIBCSecondaryDataDecoder.php
Copy path
File metadata and controls
304 lines (287 loc) · 12.7 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
<?php
namespace
Brixion
\
Bardecoder
;
/**
* Class UdiDecoder.
*
* probably wrong
* +primary_data/[0-9]julian_date
* +primary_data/$[a-zA-Z0-9](lot||serial)
* +primary_data/$+[a-zA-Z0-9]serial <- backwads compatible
* +primary_data/$$[0-9]expiry_date/lot/
*/
class
HIBCSecondaryDataDecoder
{
private
UdiDecoder
$
decoder
;
public
function
__construct
(
UdiDecoder
&
$
decoder
)
{
$
this
->
decoder
=
$
decoder
;
if
(
empty
(
$
this
->
decoder
->
secondary_parts
)) {
throw
new
\
Exception
(
'
No secondary data found
'
);
}
foreach
(
$
this
->
decoder
->
secondary_parts
as
$
part
) {
// part is too short or empty to be valid
if
(!
\is_string
(
$
part
) ||
\strlen
(
$
part
) <
2
) {
continue
;
}
// Remove illegal + character
if
(
'
+
'
==
$
part
[
0
]) {
$
part
=
substr
(
$
part
,
1
);
}
// $$[0-9] -> date field
if
(
preg_match
(
'
/^\$\$[0-9]/
'
,
$
part
)) {
$
this
->
handleDateField
(
$
part
);
}
elseif
(
'
$+
'
==
substr
(
$
part
,
0
,
2
)) {
// This is a serial but we save it as a lot
$
this
->
decoder
->
lot
=
substr
(
$
part
,
2
);
}
elseif
(
'
$
'
==
$
part
[
0
]) {
$
this
->
decoder
->
lot
=
substr
(
$
part
,
1
);
}
elseif
(
'
16D
'
==
substr
(
$
part
,
0
,
3
)) {
$
this
->
decoder
->
date_of_manufacture
=
$
this
->
getDateFromYYYYMMDD
(
substr
(
$
part
,
3
));
}
elseif
(
'
14D
'
==
substr
(
$
part
,
0
,
3
)) {
$
this
->
decoder
->
expiry_date
=
$
this
->
getDateFromYYYYMMDD
(
substr
(
$
part
,
3
));
}
elseif
(
'
S
'
==
substr
(
$
part
,
0
,
1
)) {
$
this
->
decoder
->
serial_number
=
substr
(
$
part
,
1
);
}
}
}
public
function
getDateFromYYYYMMDD
(
$
date
)
{
return
substr
(
$
date
,
0
,
4
).
'
-
'
.
substr
(
$
date
,
4
,
2
).
'
-
'
.
substr
(
$
date
,
6
,
2
);
}
public
function
handleDateField
(
$
part
)
{
$
this
->
decoder
->
serial_number
=
null
;
switch
(
$
part
[
2
]) {
case
'
0
'
:
// MMYY format, followed by lot
$
mm
=
substr
(
$
part
,
3
,
2
);
$
yy
=
substr
(
$
part
,
5
,
2
);
$
max_day
=
date
(
'
t
'
,
strtotime
(
$
mm
.
'
-
'
.
$
yy
));
$
this
->
decoder
->
expiry_date
=
"
20
$
yy
-
$
mm
-
$
max_day
"
;
$
this
->
decoder
->
lot
=
substr
(
$
part
,
8
);
// no break
case
'
1
'
:
// first day of month, date in MMYY format
$
date
=
substr
(
$
part
,
2
,
4
);
$
month
=
substr
(
$
date
,
0
,
2
);
$
year
=
'
20
'
.
substr
(
$
date
,
2
,
2
);
$
last_day
=
date
(
'
t
'
,
strtotime
(
$
year
.
'
-
'
.
$
month
.
'
-01
'
));
$
this
->
decoder
->
expiry_date
=
"
$
year
-
$
month
-
$
last_day
"
;
$
this
->
decoder
->
lot
=
substr
(
$
part
,
6
);
break
;
case
'
2
'
:
// MMDDYY format
$
date
=
substr
(
$
part
,
3
,
6
);
$
year
=
'
20
'
.
substr
(
$
date
,
4
,
2
);
$
month
=
substr
(
$
date
,
0
,
2
);
$
day
=
substr
(
$
date
,
2
,
2
);
$
this
->
decoder
->
expiry_date
=
"
$
year
-
$
month
-
$
day
"
;
$
this
->
decoder
->
lot
=
substr
(
$
part
,
9
);
break
;
case
'
3
'
:
// YYMMDD format
$
year
=
'
20
'
.
substr
(
$
part
,
3
,
2
);
$
month
=
substr
(
$
part
,
5
,
2
);
$
day
=
substr
(
$
part
,
7
,
2
);
$
this
->
decoder
->
expiry_date
=
"
$
year
-
$
month
-
$
day
"
;
$
this
->
decoder
->
lot
=
substr
(
$
part
,
9
);
break
;
case
'
4
'
:
// YYMMDDHH format
$
year
=
'
20
'
.
substr
(
$
part
,
3
,
2
);
$
month
=
substr
(
$
part
,
7
,
2
);
$
day
=
substr
(
$
part
,
5
,
2
);
$
hour
=
substr
(
$
part
,
9
,
2
);
$
lot
=
substr
(
$
part
,
11
);
$
this
->
decoder
->
expiry_date
=
"
$
year
-
$
month
-
$
day
$
hour
"
;
$
this
->
decoder
->
lot
=
$
lot
;
break
;
case
'
5
'
:
// YYJJJ julian date format
$
this
->
decoder
->
expiry_date
=
$
this
->
julianToDate
(
substr
(
$
part
,
3
,
5
));
$
this
->
decoder
->
lot
=
substr
(
$
part
,
8
);
break
;
case
'
6
'
:
// YYJJJHH julian date format
$
this
->
decoder
->
expiry_date
=
jdtojulian
((
int
)
substr
(
$
part
,
3
,
5
)).
'
'
.
substr
(
$
part
,
9
,
2
);
$
this
->
decoder
->
lot
=
substr
(
$
part
,
10
);
break
;
case
'
7
'
:
// expiry_date is null, lot follows
$
this
->
decoder
->
expiry_date
=
null
;
$
this
->
decoder
->
lot
=
substr
(
$
part
,
3
);
break
;
case
'
8
'
:
case
'
9
'
:
$
this
->
handleLegacyQuantity
(
$
part
);
break
;
default
:
throw
new
\
Exception
(
"
Unknown date format:
$
part
"
);
}
}
public
function
julianToDate
(
$
part
)
{
if
(
5
!=
\strlen
(
$
part
)) {
throw
new
\
Exception
(
'
First part is a julian date, but is not 5 characters long
'
);
}
// get the first 2 characters from $part for the year, rest is days
$
year
=
'
20
'
.
substr
(
$
part
,
0
,
2
);
$
julian_date
= ((
int
)
substr
(
$
part
,
2
)) -
1
;
// use the year and the number of days to calculate date
return
date
(
'
Y-m-d
'
,
strtotime
(
"
$
year
-01-01 +
$
julian_date
days
"
));
}
private
function
handleLegacyQuantity
(
$
part
)
{
// $$9 is identifier, 5 digits for quantity always follows.
// After quantity is [2-7] date identifier
// If nothing follows quantity, then expiry date is null
// If 1 or 0 next to quantity, then MMYY follows quantity with rest is lot
if
(
preg_match
(
'
/^\$\$9[0-9]{5}[2-7]{1}\d+/
'
,
$
part
)) {
$
this
->
handleLegacyPentaQuantityWithData
(
$
part
);
}
elseif
(
preg_match
(
'
/^\$\$9[0-9]{5}[^2-7]\d+/
'
,
$
part
)) {
$
this
->
decoder
->
quantity
=
ltrim
(
substr
(
$
part
,
3
,
5
),
'
0
'
);
$
mm
=
substr
(
$
part
,
8
,
2
);
$
yy
=
substr
(
$
part
,
10
,
2
);
$
max_day
=
date
(
'
t
'
,
strtotime
(
$
mm
.
'
-
'
.
$
yy
));
$
this
->
decoder
->
expiry_date
=
"
20
$
yy
-
$
mm
-
$
max_day
"
;
$
this
->
decoder
->
lot
=
substr
(
$
part
,
12
);
}
elseif
(
preg_match
(
'
/^\$\$9[0-9]{5}[^\d]/
'
,
$
part
)) {
$
this
->
decoder
->
quantity
=
ltrim
(
substr
(
$
part
,
3
,
5
),
'
0
'
);
}
elseif
(
preg_match
(
'
/^\$\$8[0-9]{2}[2-7]{1}\d+/
'
,
$
part
)) {
$
this
->
handleLegacyDoubleQuantityFormat
(
$
part
);
}
elseif
(
preg_match
(
'
/^\$\$8[0-9]{2}[^2-7]\d+/
'
,
$
part
)) {
$
this
->
decoder
->
quantity
=
ltrim
(
substr
(
$
part
,
3
,
2
),
'
0
'
);
$
mm
=
substr
(
$
part
,
5
,
2
);
$
yy
=
substr
(
$
part
,
7
,
2
);
$
max_day
=
date
(
'
t
'
,
strtotime
(
$
mm
.
'
-
'
.
$
yy
));
$
this
->
decoder
->
expiry_date
=
"
20
$
yy
-
$
mm
-
$
max_day
"
;
$
this
->
decoder
->
lot
=
substr
(
$
part
,
9
);
}
}
private
function
handleLegacyDoubleQuantityFormat
(
$
part
)
{
// first 2 characters after $$8 is quantity
// next character is date identifier
// if identifier is 2, after quantity comes expiration date in MMDDYY format and rest is lot
// if identifier is 3, after quantity comes expiration date in YYMMDD format and rest is lot
// if identifier is 4, after quantity comes expiration date in YYMMDDHH format and rest is lot
// if identifier is 5, after quantity comes expiration date in YYJJJ format and rest is lot
// if identifier is 6, after quantity comes expiration date in YYJJJHH format and rest is lot
// if identifier is 7, after quantity is lot
$
identifier
=
substr
(
$
part
,
5
,
1
);
$
this
->
decoder
->
quantity
=
ltrim
(
substr
(
$
part
,
3
,
2
),
'
0
'
);
$
date_field
=
substr
(
$
part
,
6
);
switch
(
$
identifier
) {
case
2
:
$
mm
=
substr
(
$
date_field
,
0
,
2
);
$
dd
=
substr
(
$
date_field
,
2
,
2
);
$
yy
=
substr
(
$
date_field
,
4
,
2
);
$
this
->
decoder
->
expiry_date
=
"
20
$
yy
-
$
mm
-
$
dd
"
;
if
(
\strlen
(
$
date_field
) >
6
) {
$
this
->
decoder
->
lot
=
substr
(
$
date_field
,
6
);
}
break
;
case
3
:
$
yy
=
substr
(
$
date_field
,
0
,
2
);
$
mm
=
substr
(
$
date_field
,
2
,
2
);
$
dd
=
substr
(
$
date_field
,
4
,
2
);
$
this
->
decoder
->
expiry_date
=
"
20
$
yy
-
$
mm
-
$
dd
"
;
if
(
\strlen
(
$
date_field
) >
6
) {
$
this
->
decoder
->
lot
=
substr
(
$
date_field
,
6
);
}
break
;
case
4
:
$
yy
=
substr
(
$
date_field
,
0
,
2
);
$
mm
=
substr
(
$
date_field
,
2
,
2
);
$
dd
=
substr
(
$
date_field
,
4
,
2
);
$
hh
=
substr
(
$
date_field
,
6
,
2
);
$
this
->
decoder
->
expiry_date
=
"
20
$
yy
-
$
mm
-
$
dd
$
hh
"
;
if
(
\strlen
(
$
date_field
) >
8
) {
$
this
->
decoder
->
lot
=
substr
(
$
date_field
,
8
);
}
break
;
case
5
:
$
this
->
decoder
->
expiry_date
=
$
this
->
julianToDate
(
substr
(
$
date_field
,
0
,
5
));
if
(
\strlen
(
$
date_field
) >
5
) {
$
this
->
decoder
->
lot
=
substr
(
$
date_field
,
5
);
}
break
;
case
6
:
$
expiry_date
=
$
this
->
julianToDate
(
substr
(
$
date_field
,
0
,
5
));
$
hh
=
substr
(
$
date_field
,
5
,
2
);
$
this
->
decoder
->
expiry_date
=
date
(
'
Y-m-d H:i:s
'
,
strtotime
(
"
$
expiry_date
+
$
hh
hours
"
));
if
(
\strlen
(
$
date_field
) >
7
) {
$
this
->
decoder
->
lot
=
substr
(
$
date_field
,
7
);
}
break
;
case
7
:
$
this
->
decoder
->
lot
=
$
date_field
;
break
;
default
:
throw
new
\
Exception
(
"
Unknown identifier for legacy secondary quantity data. Identifier:
$
identifier
"
);
}
}
private
function
handleLegacyPentaQuantityWithData
(
$
part
)
{
// first 5 characters after $$9 is quantity
// next character after quantity is date identifier
// if identifier is 2, after quantity comes expiration date in MMDDYY and rest is lot
// if identifier is 3, after quantity comes expiration date in YYMMDD and rest is lot
// if identifier is 4, after quantity comes expiration date in YYMMDDHH and rest is lot
// if identifier is 5, after quantity comes expiration date in YYJJJ (JJJ is Julian date) and rest is lot
// if identifier is 6, after quantity comes expiration date in YYJJJHH and rest is lot
// if identifier is 7, after quantity is lot
$
identifier
=
substr
(
$
part
,
8
,
1
);
$
this
->
decoder
->
quantity
=
ltrim
(
substr
(
$
part
,
3
,
5
),
'
0
'
);
$
date_field
=
substr
(
$
part
,
9
);
switch
(
$
identifier
) {
case
'
2
'
:
$
mm
=
substr
(
$
date_field
,
0
,
2
);
$
dd
=
substr
(
$
date_field
,
2
,
2
);
$
yy
=
substr
(
$
date_field
,
4
,
2
);
$
this
->
decoder
->
expiry_date
=
"
20
$
yy
-
$
mm
-
$
dd
"
;
if
(
\strlen
(
$
date_field
) >
6
) {
$
this
->
decoder
->
lot
=
substr
(
$
date_field
,
6
);
}
break
;
case
'
3
'
:
$
yy
=
substr
(
$
date_field
,
0
,
2
);
$
mm
=
substr
(
$
date_field
,
2
,
2
);
$
dd
=
substr
(
$
date_field
,
4
,
2
);
$
this
->
decoder
->
expiry_date
=
"
20
$
yy
-
$
mm
-
$
dd
"
;
if
(
\strlen
(
$
date_field
) >
6
) {
$
this
->
decoder
->
lot
=
substr
(
$
date_field
,
6
);
}
break
;
case
'
4
'
:
$
yy
=
substr
(
$
date_field
,
0
,
2
);
$
mm
=
substr
(
$
date_field
,
2
,
2
);
$
dd
=
substr
(
$
date_field
,
4
,
2
);
$
hh
=
substr
(
$
date_field
,
6
,
2
);
$
this
->
decoder
->
expiry_date
=
"
20
$
yy
-
$
mm
-
$
dd
$
hh
:00:00
"
;
if
(
\strlen
(
$
date_field
) >
8
) {
$
this
->
decoder
->
lot
=
substr
(
$
date_field
,
8
);
}
break
;
case
'
5
'
:
$
this
->
decoder
->
expiry_date
=
$
this
->
julianToDate
(
$
date_field
);
if
(
\strlen
(
$
date_field
) >
5
) {
$
this
->
decoder
->
lot
=
substr
(
$
date_field
,
5
);
}
break
;
case
'
6
'
:
$
expiry_date
=
$
this
->
julianToDate
(
$
date_field
);
$
hh
=
substr
(
$
date_field
,
5
,
2
);
$
this
->
decoder
->
expiry_date
=
date
(
'
Y-m-d H:i:s
'
,
strtotime
(
"
$
expiry_date
+
$
hh
hours
"
));
if
(
\strlen
(
$
date_field
) >
7
) {
$
this
->
decoder
->
lot
=
substr
(
$
date_field
,
7
);
}
break
;
case
'
7
'
:
$
this
->
decoder
->
lot
=
$
date_field
;
break
;
default
:
throw
new
\
Exception
(
"
Unknown identifier for legacy secondary quantity data. Identifier:
$
identifier
"
);
}
}
}
Back
|
FazBrowse Home
|
New Git URL