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