Completed
Push — master ( 1f4ad9...320f5c )
by John
03:15
created

FieldParserBarcode::getText()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 22
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
cc 4
eloc 10
nc 6
nop 0
dl 0
loc 22
ccs 0
cts 13
cp 0
crap 20
rs 9.9332
c 0
b 0
f 0
1
<?php
2
3
namespace Graze\CiffRenderer\Parser\FieldParser;
4
5
use Graze\CiffRenderer\Parser\FieldParser\FieldParserInterface;
6
7
class FieldParserBarcode extends FieldParserFixedText implements FieldParserInterface
8
{
9
    /**
10
     * @return float
11
     */
12
    public function getFontSize()
13
    {
14
        return (float) $this->xmlField->Barcode->HR->HRFont->Pitch;
15
    }
16
17
    /**
18
     * @return bool
19
     */
20
    public function getIsInverse()
21
    {
22
        return (bool) (string) $this->xmlField->Inverse;
23
    }
24
25
    /**
26
    * Only EAN13 currently supported.
27
    *
28
    * @return string
29
    */
30
    public function getText()
31
    {
32
        $barcode = parent::getText();
33
34
        // add up each digit in the barcode - odd digits counting
35
        // from the right are tripled before being added
36
        $sum = 0;
37
        $triple = true;
38
        for ($i = strlen($barcode)-1; $i >= 0; $i--) {
39
            $sum += ($triple ? 3 : 1) * $barcode[$i];
40
            $triple = !$triple;
0 ignored issues
show
introduced by
The condition $triple is always true.
Loading history...
41
        }
42
43
        //check digit is the inverse remainder after / by 10
44
        //e.g. 4 becomes 6, 8 becomes 2 etc...
45
        $remainder = $sum % 10;
46
47
        if ($remainder > 0) {
48
            $remainder = 10 - $remainder;
49
        }
50
51
        return $this->convertToFontCode($barcode . $remainder);
52
    }
53
    /**
54
    * @param string $barcode
55
    * @return string
56
    */
57
    private function convertToFontCode($barcode)
58
    {
59
        /**
60
        * The barcode is split into the following sections:
61
        *  First digit
62
        *  First group of 6 digits (2nd-7th)
63
        *  Last group of 6 digits (8th-13th)
64
        *      e.g.        1234567890128
65
        *      becomes     1   234567  890128
66
        *
67
        * The font converts the first digit to a char between q-z
68
        *
69
        * The first group of digits are converted to chars starting at either A or Q,
70
        * the first digit affects the format of the first group as follows:
71
        *  1st digit     First group format
72
        *      0           AAAAAA
73
        *      1           AAQAQQ
74
        *      2           AAQQAQ
75
        *      3           AAQQQA
76
        *      4           AQAAQQ
77
        *      5           AQQAAQ
78
        *      6           AQQQAA
79
        *      7           AQAQAQ
80
        *      8           AQAQQA
81
        *      9           AQQAQA
82
        *
83
        * The last group of digits remain unchanged
84
        *
85
        * The actual format used by the font is:
86
        *       firstDigit  {{{{{|  firstGroup  {|{  secondGroup  |
87
        */
88
89
        // convert first digit
90
        $firstDigit = (int)substr($barcode, 0, 1);
91
        $firstDigitChar = chr(113 + $firstDigit);
92
93
        // convert first group
94
        $firstGroup = '';
95
        $firstGroupFormats = [
96
            'AAAAAA',
97
            'AAQAQQ',
98
            'AAQQAQ',
99
            'AAQQQA',
100
            'AQAAQQ',
101
            'AQQAAQ',
102
            'AQQQAA',
103
            'AQAQAQ',
104
            'AQAQQA',
105
            'AQQAQA'
106
        ];
107
        $firstGroupFormat = $firstGroupFormats[$firstDigit];
108
        for ($i = 1; $i <= 6; $i++) {
109
            $digit = (int)substr($barcode, $i, 1);
110
            $startingChar = substr($firstGroupFormat, $i - 1, 1);
111
            $firstGroup .= chr(ord($startingChar) + $digit);
112
        }
113
114
        // extract second group
115
        $secondGroup = substr($barcode, 7);
116
117
        // combine the barcode with the special chars used by the font
118
        return $firstDigitChar.'{{{{{|'.$firstGroup.'{|{'.$secondGroup.'|';
119
    }
120
}
121