PaymentSlipFpdf::setBackground()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
/**
3
 * Swiss Payment Slip FPDF
4
 *
5
 * @license http://www.opensource.org/licenses/mit-license.php MIT License
6
 * @copyright 2012-2015 Some nice Swiss guys
7
 * @author Marc Würth <[email protected]>
8
 * @author Manuel Reinhard <[email protected]>
9
 * @author Peter Siska <[email protected]>
10
 * @link https://github.com/ravage84/SwissPaymentSlipFpdf
11
 */
12
13
namespace SwissPaymentSlip\SwissPaymentSlipFpdf;
14
15
use SwissPaymentSlip\SwissPaymentSlipPdf\PaymentSlipPdf;
16
use fpdf\FPDF;
17
18
/**
19
 * Create Swiss payment slips (ESR/ES) as PDFs with FPDF
20
 *
21
 * Responsible for generating standard Swiss payment Slips as PDFs using FPDF as engine.
22
 * Layout done by utilizing PaymentSlip and
23
 * data organisation through PaymentSlipData.
24
 *
25
 * @link https://github.com/ravage84/SwissPaymentSlipPdf/ SwissPaymentSlipPdf
26
 * @link https://github.com/ravage84/SwissPaymentSlip/ SwissPaymentSlip
27
 */
28
class PaymentSlipFpdf extends PaymentSlipPdf
29
{
30
    /**
31
     * A caching table for hex to RGB conversions
32
     *
33
     * @var array
34
     */
35
    protected $rgbColors = array();
36
37
    /**
38
     * The PDF engine object to generate the PDF output with
39
     *
40
     * @var null|FPDF The PDF engine object
41
     */
42
    protected $pdfEngine = null;
43
44
    /**
45
     * The last set font family, prevents the PDF engine to re-set the same values over and over
46
     *
47
     * @var string;
48
     */
49
    protected $lastFontFamily = '';
50
51
    /**
52
     * The last set font size, prevents the PDF engine to re-set the same values over and over
53
     *
54
     * @var int|double;
55
     */
56
    protected $lastFontSize = '';
57
58
    /**
59
     * The last set font color, prevents the PDF engine to re-set the same values over and over
60
     *
61
     * @var string;
62
     */
63
    protected $lastFontColor = '';
64
65
    /**
66
     * {@inheritDoc}
67
     */
68
    protected function displayImage($background)
69
    {
70
        // TODO check if slipBackground is a color or a path to a file
71
72
        $this->pdfEngine->Image(
73
            $background,
74
            $this->paymentSlip->getSlipPosX(),
75
            $this->paymentSlip->getSlipPosY(),
76
            $this->paymentSlip->getSlipWidth(),
77
            $this->paymentSlip->getSlipHeight(),
78
            strtoupper(substr($background, -3, 3))
79
        );
80
81
        return $this;
82
    }
83
84
    /**
85
     * {@inheritDoc}
86
     */
87
    protected function setFont($fontFamily, $fontSize, $fontColor)
88
    {
89
        if ($fontColor) {
90
            if ($this->lastFontColor != $fontColor) {
91
                $this->lastFontColor = $fontColor;
92
93
                $rgbArray = $this->convertColor2Rgb($fontColor);
94
                $this->pdfEngine->SetTextColor($rgbArray['red'], $rgbArray['green'], $rgbArray['blue']);
95
            }
96
        }
97
        if ($this->lastFontFamily != $fontFamily || $this->lastFontSize != $fontSize) {
98
            $this->lastFontFamily = $fontFamily;
99
            $this->lastFontSize = $fontSize;
0 ignored issues
show
Documentation Bug introduced by
It seems like $fontSize of type integer or double is incompatible with the declared type string of property $lastFontSize.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
100
101
            $this->pdfEngine->SetFont($fontFamily, '', $fontSize);
102
        }
103
104
        return $this;
105
    }
106
107
    /**
108
     * {@inheritDoc}
109
     */
110
    protected function setBackground($background)
111
    {
112
        // TODO check if it's a path to a file
113
        // TODO else it should be a color
114
        $rgbArray = $this->convertColor2Rgb($background);
115
        $this->pdfEngine->SetFillColor($rgbArray['red'], $rgbArray['green'], $rgbArray['blue']);
116
117
        return $this;
118
    }
119
120
    /**
121
     * {@inheritDoc}
122
     */
123
    protected function setPosition($posX, $posY)
124
    {
125
        $this->pdfEngine->SetXY($posX, $posY);
126
127
        return $this;
128
    }
129
130
    /**
131
     * {@inheritDoc}
132
     */
133
    protected function createCell($width, $height, $line, $textAlign, $fill)
134
    {
135
        $this->pdfEngine->Cell($width, $height, $line, 0, 0, $textAlign, $fill);
136
137
        return $this;
138
    }
139
140
    /**
141
     * {@inheritDoc}
142
     */
143
    protected function convertColor2Rgb($color)
144
    {
145
        if (isset($this->rgbColors[$color])) {
146
            return $this->rgbColors[$color];
147
        }
148
        $this->rgbColors[$color] = $this->hex2RGB($color);
149
        return $this->rgbColors[$color];
150
    }
151
152
    /**
153
     * Convert hexadecimal color code into an associative array or string of RGB values
154
     *
155
     * @param string $hexStr The hexadecimal color code (3 or 6 characters long)
156
     * @param bool $returnAsString Whether to return as a string or array.
157
     * @param string $separator The separator for the RGB value string, defaults to ",".
158
     * @return array|string|false The RGB values as an associative array or a string.
159
     * False if an invalid color code was given.
160
     *
161
     * @copyright 2010 hafees at msn dot com
162
     * @link http://www.php.net/manual/en/function.hexdec.php#99478
163
     * @todo Throw an exception if an invalid hex color code was given
164
     */
165
    protected function hex2RGB($hexStr, $returnAsString = false, $separator = ',')
166
    {
167
        $hexStr = preg_replace("/[^0-9A-Fa-f]/", '', $hexStr); // Gets a proper hex string
168
        $rgbArray = array();
169
        if (strlen($hexStr) == 6) {
170
            // If a proper hex code, convert using bitwise operation. No overhead... faster
171
            $colorVal = hexdec($hexStr);
172
            $rgbArray['red'] = 0xFF & ($colorVal >> 0x10);
173
            $rgbArray['green'] = 0xFF & ($colorVal >> 0x8);
174
            $rgbArray['blue'] = 0xFF & $colorVal;
175
        } elseif (strlen($hexStr) == 3) {
176
            // If shorthand notation, need some string manipulations
177
            $rgbArray['red'] = hexdec(str_repeat(substr($hexStr, 0, 1), 2));
178
            $rgbArray['green'] = hexdec(str_repeat(substr($hexStr, 1, 1), 2));
179
            $rgbArray['blue'] = hexdec(str_repeat(substr($hexStr, 2, 1), 2));
180
        } else {
181
            return false; //Invalid hex color code
182
        }
183
184
        // Returns the RGB values either as string or as associative array
185
        return $returnAsString ? implode($separator, $rgbArray) : $rgbArray;
186
    }
187
}
188