BCGBarcode1D::setDisplayChecksum()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 2
rs 10
c 0
b 0
f 0
1
<?php
2
namespace tinymeng\code\Gateways\barcode;
3
4
/**
5
 *--------------------------------------------------------------------
6
 *
7
 * Holds all type of barcodes for 1D generation
8
 *
9
 *--------------------------------------------------------------------
10
 * Copyright (C) Jean-Sebastien Goupil
11
 * http://www.barcodephp.com
12
 */
13
abstract class BCGBarcode1D extends BCGBarcode {
14
    const SIZE_SPACING_FONT = 5;
15
16
    const AUTO_LABEL = '##!!AUTO_LABEL!!##';
17
18
    protected $thickness;       // int
19
    protected $keys, $code;     // string[]
20
    protected $positionX;       // int
21
    protected $font;            // BCGFont
22
    protected $text;            // string
23
    protected $checksumValue;   // int or int[]
24
    protected $displayChecksum; // bool
25
    protected $label;           // string
26
    protected $defaultLabel;    // BCGLabel
27
28
    /**
29
     * Constructor.
30
     */
31
    protected function __construct() {
32
        parent::__construct();
33
34
        $this->setThickness(30);
35
36
        $this->defaultLabel = new BCGLabel();
37
        $this->defaultLabel->setPosition(BCGLabel::POSITION_BOTTOM);
38
        $this->setLabel(self::AUTO_LABEL);
39
        $this->setFont(new BCGFontPhp(5));
40
41
        $this->text = '';
42
        $this->checksumValue = false;
43
        $this->positionX = 0;
44
    }
45
46
    /**
47
     * Gets the thickness.
48
     *
49
     * @return int
50
     */
51
    public function getThickness() {
52
        return $this->thickness;
53
    }
54
55
    /**
56
     * Sets the thickness.
57
     *
58
     * @param int $thickness
59
     */
60
    public function setThickness($thickness) {
61
        $thickness = intval($thickness);
62
        if ($thickness <= 0) {
63
            throw new BCGArgumentException('The thickness must be larger than 0.', 'thickness');
64
        }
65
66
        $this->thickness = $thickness;
67
    }
68
69
    /**
70
     * Gets the label.
71
     * If the label was set to BCGBarcode1D::AUTO_LABEL, the label will display the value from the text parsed.
72
     *
73
     * @return string
74
     */
75
    public function getLabel() {
76
        $label = $this->label;
77
        if ($this->label === self::AUTO_LABEL) {
78
            $label = $this->text;
79
            if ($this->displayChecksum === true && ($checksum = $this->processChecksum()) !== false) {
80
                $label .= $checksum;
81
            }
82
        }
83
84
        return $label;
85
    }
86
87
    /**
88
     * Sets the label.
89
     * You can use BCGBarcode::AUTO_LABEL to have the label automatically written based on the parsed text.
90
     *
91
     * @param string $label
92
     */
93
    public function setLabel($label) {
94
        $this->label = $label;
95
    }
96
97
    /**
98
     * Gets the font.
99
     *
100
     * @return BCGFont
101
     */
102
    public function getFont() {
103
        return $this->font;
104
    }
105
106
    /**
107
     * Sets the font.
108
     *
109
     * @param mixed $font BCGFont or int
110
     */
111
    public function setFont($font) {
112
        if (is_int($font)) {
113
            if ($font === 0) {
114
                $font = null;
115
            } else {
116
                $font = new BCGFontPhp($font);
117
            }
118
        }
119
120
        $this->font = $font;
121
    }
122
123
    /**
124
     * Parses the text before displaying it.
125
     *
126
     * @param mixed $text
127
     */
128
    public function parse($text) {
129
        $this->text = $text;
130
        $this->checksumValue = false; // Reset checksumValue
131
        $this->validate();
132
133
        parent::parse($text);
134
135
        $this->addDefaultLabel();
136
    }
137
138
    /**
139
     * Gets the checksum of a Barcode.
140
     * If no checksum is available, return FALSE.
141
     *
142
     * @return string
143
     */
144
    public function getChecksum() {
145
        return $this->processChecksum();
146
    }
147
148
    /**
149
     * Sets if the checksum is displayed with the label or not.
150
     * The checksum must be activated in some case to make this variable effective.
151
     *
152
     * @param boolean $displayChecksum
153
     */
154
    public function setDisplayChecksum($displayChecksum) {
155
        $this->displayChecksum = (bool)$displayChecksum;
156
    }
157
158
    /**
159
     * Adds the default label.
160
     */
161
    protected function addDefaultLabel() {
162
        $label = $this->getLabel();
163
        $font = $this->font;
164
        if ($label !== null && $label !== '' && $font !== null && $this->defaultLabel !== null) {
165
            $this->defaultLabel->setText($label);
166
            $this->defaultLabel->setFont($font);
167
            $this->addLabel($this->defaultLabel);
168
        }
169
    }
170
171
    /**
172
     * Validates the input
173
     */
174
    protected function validate() {
175
        // No validation in the abstract class.
176
    }
177
178
    /**
179
     * Returns the index in $keys (useful for checksum).
180
     *
181
     * @param mixed $var
182
     * @return mixed
183
     */
184
    protected function findIndex($var) {
185
        return array_search($var, $this->keys);
186
    }
187
188
    /**
189
     * Returns the code of the char (useful for drawing bars).
190
     *
191
     * @param mixed $var
192
     * @return string
193
     */
194
    protected function findCode($var) {
195
        return $this->code[$this->findIndex($var)];
196
    }
197
198
    /**
199
     * Draws all chars thanks to $code. If $startBar is true, the line begins by a space.
200
     * If $startBar is false, the line begins by a bar.
201
     *
202
     * @param resource $im
203
     * @param string $code
204
     * @param boolean $startBar
205
     */
206
    protected function drawChar($im, $code, $startBar = true) {
207
        $colors = array(BCGBarcode::COLOR_FG, BCGBarcode::COLOR_BG);
208
        $currentColor = $startBar ? 0 : 1;
209
        $c = strlen($code);
210
        for ($i = 0; $i < $c; $i++) {
211
            for ($j = 0; $j < intval($code[$i]) + 1; $j++) {
212
                $this->drawSingleBar($im, $colors[$currentColor]);
213
                $this->nextX();
214
            }
215
216
            $currentColor = ($currentColor + 1) % 2;
217
        }
218
    }
219
220
    /**
221
     * Draws a Bar of $color depending of the resolution.
222
     *
223
     * @param resource $img
224
     * @param int $color
225
     */
226
    protected function drawSingleBar($im, $color) {
227
        $this->drawFilledRectangle($im, $this->positionX, 0, $this->positionX, $this->thickness - 1, $color);
228
    }
229
230
    /**
231
     * Moving the pointer right to write a bar.
232
     */
233
    protected function nextX() {
234
        $this->positionX++;
235
    }
236
237
    /**
238
     * Method that saves FALSE into the checksumValue. This means no checksum
239
     * but this method should be overriden when needed.
240
     */
241
    protected function calculateChecksum() {
242
        $this->checksumValue = false;
243
    }
244
245
    /**
246
     * Returns FALSE because there is no checksum. This method should be
247
     * overriden to return correctly the checksum in string with checksumValue.
248
     *
249
     * @return string
250
     */
251
    protected function processChecksum() {
252
        return false;
253
    }
254
}
255
?>
0 ignored issues
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...