Xf::checkBorders()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 2
eloc 3
nc 2
nop 1
1
<?php
2
namespace Xls\Record;
3
4
use Xls\Format as XlsFormat;
5
use Xls\NumberFormat;
6
7
class Xf extends AbstractRecord
8
{
9
    const NAME = 'XF';
10
    const ID = 0x00E0;
11
12
    /**
13
     * Generate an Excel BIFF XF record.
14
     *
15
     * @param XlsFormat $format
16
     * @param string $style The type of the XF record ('style' or 'cell').
17
     *
18
     * @return string
19
     */
20
    public function getData($format, $style)
21
    {
22
        $style = $this->getStyle($style, $format);
23
24
        $this->checkBorders($format);
25
26
        $border1 = $this->getBorder1($format);
27
        $border2 = $this->getBorder2($format);
28
29
        $icv = $format->fgColor; // fg and bg pattern colors
30
        $icv |= $format->bgColor << 7;
31
32
        $options = 0x00;
33
        $data = pack(
34
            "vvvC",
35
            $format->getFont()->getIndex(),
36
            $format->getNumFormatIndex(),
37
            $style,
38
            $this->getAlignment($format)
39
        );
40
        $data .= pack("CCC", $format->rotation, $options, $this->getUsedAttr($format));
41
        $data .= pack("VVv", $border1, $border2, $icv);
42
43
        return $this->getFullRecord($data);
44
    }
45
46
    /**
47
     * @param XlsFormat $format
48
     *
49
     * @return int
50
     */
51
    protected function getAlignment($format)
52
    {
53
        $align = $format->textHorAlign;
54
        $align |= $format->textWrap << 3;
55
        $align |= $format->textVertAlign << 4;
56
57
        $textJustlast = 0x00;
58
        $align |= $textJustlast << 7;
59
60
        return $align;
61
    }
62
63
    /**
64
     * @param XlsFormat $format
65
     *
66
     * @return int
67
     */
68
    protected function getUsedAttr($format)
69
    {
70
        $flags = $this->getFlags($format);
71
72
        $usedAttr = $flags['Num'] << 2;
73
        $usedAttr |= $flags['Fnt'] << 3;
74
        $usedAttr |= $flags['Alc'] << 4;
75
        $usedAttr |= $flags['Bdr'] << 5;
76
        $usedAttr |= $flags['Pat'] << 6;
77
        $usedAttr |= $flags['Prot'] << 7;
78
79
        return $usedAttr;
80
    }
81
82
    /**
83
     * @param XlsFormat $format
84
     *
85
     * @return int
86
     */
87
    protected function getBorder1($format)
88
    {
89
        $border1 = $format->getBorderStyle('left'); // Border line style and color
90
        $border1 |= $format->getBorderStyle('right') << 4;
91
        $border1 |= $format->getBorderStyle('top') << 8;
92
        $border1 |= $format->getBorderStyle('bottom') << 12;
93
        $border1 |= $format->getBorderColor('left') << 16;
94
        $border1 |= $format->getBorderColor('right') << 23;
95
        $diagTlToRb = 0;
96
        $diagTrToLb = 0;
97
        $border1 |= $diagTlToRb << 30;
98
        $border1 |= $diagTrToLb << 31;
99
100
        return $border1;
101
    }
102
103
    /**
104
     * @param XlsFormat $format
105
     *
106
     * @return int
107
     */
108
    protected function getBorder2($format)
109
    {
110
        $border2 = $format->getBorderColor('top'); // Border color
111
        $border2 |= $format->getBorderColor('bottom') << 7;
112
        $border2 |= $format->diagColor << 14;
113
        $border2 |= $format->diag << 21;
114
        $border2 |= $format->pattern << 26;
115
116
        return $border2;
117
    }
118
119
    /**
120
     * @param string $style
121
     * @param XlsFormat $format
122
     *
123
     * @return int
124
     */
125
    protected function getStyle($style, $format)
126
    {
127
        // Set the type of the XF record and some of the attributes.
128
        if ($style == 'style') {
129
            $style = 0xFFF5;
130
        } else {
131
            $style = $format->locked;
132
            $style |= $format->hidden << 1;
133
        }
134
135
        return $style;
136
    }
137
138
    /**
139
     * Zero border colors if no borders set
140
     * @param XlsFormat $format
141
     */
142
    protected function checkBorders($format)
143
    {
144
        if ($format->diag == 0) {
145
            $format->diagColor = 0;
146
        }
147
    }
148
149
    /**
150
     * @param XlsFormat $format
151
     *
152
     * @return array
153
     */
154
    protected function getFlags($format)
155
    {
156
        return array(
157
            'Num' => ($format->getNumFormat() != NumberFormat::TYPE_GENERAL) ? 1 : 0,
158
            'Fnt' => ($format->getFont()->getIndex() != 0) ? 1 : 0,
159
            'Alc' => ($format->textWrap) ? 1 : 0,
160
            'Bdr' => ($format->getBorderStyle('top')
0 ignored issues
show
Bug Best Practice introduced by
The expression $format->getBorderStyle('top') of type integer|null is loosely compared to true; this is ambiguous if the integer can be zero. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
161
                || $format->getBorderStyle('right')
0 ignored issues
show
Bug Best Practice introduced by
The expression $format->getBorderStyle('right') of type integer|null is loosely compared to true; this is ambiguous if the integer can be zero. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
162
                || $format->getBorderStyle('bottom')
0 ignored issues
show
Bug Best Practice introduced by
The expression $format->getBorderStyle('bottom') of type integer|null is loosely compared to true; this is ambiguous if the integer can be zero. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
163
                || $format->getBorderStyle('left')) ? 1 : 0,
0 ignored issues
show
Bug Best Practice introduced by
The expression $format->getBorderStyle('left') of type integer|null is loosely compared to true; this is ambiguous if the integer can be zero. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
164
            'Pat' => ($format->fgColor != 0x40
165
                || $format->bgColor != 0x41
166
                || $format->pattern) ? 1 : 0,
167
            'Prot' => $format->locked | $format->hidden
168
        );
169
    }
170
}
171