Completed
Pull Request — master (#557)
by Adrien
03:10
created

StyleRegistry::getFillIdForStyleId()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
crap 2
1
<?php
2
3
namespace Box\Spout\Writer\XLSX\Manager\Style;
4
5
use Box\Spout\Common\Entity\Style\Style;
6
7
/**
8
 * Class StyleRegistry
9
 * Registry for all used styles
10
 */
11
class StyleRegistry extends \Box\Spout\Writer\Common\Manager\Style\StyleRegistry
12
{
13
    /**
14
     * @var array
15
     */
16
    protected $registeredFills = [];
17
18
    /**
19
     * @var array [STYLE_ID] => [FILL_ID] maps a style to a fill declaration
20
     */
21
    protected $styleIdToFillMappingTable = [];
22
23
    /**
24
     * Excel preserves two default fills with index 0 and 1
25
     * Since Excel is the dominant vendor - we play along here
26
     *
27
     * @var int The fill index counter for custom fills.
28
     */
29
    protected $fillIndex = 2;
30
31
    /**
32
     * @var array
33
     */
34
    protected $registeredBorders = [];
35
36
    /**
37
     * @var array [STYLE_ID] => [BORDER_ID] maps a style to a border declaration
38
     */
39
    protected $styleIdToBorderMappingTable = [];
40
41
    /**
42
     * XLSX specific operations on the registered styles
43
     *
44
     * @param Style $style
45
     * @return Style
46
     */
47 42
    public function registerStyle(Style $style)
48
    {
49 42
        $registeredStyle = parent::registerStyle($style);
50 42
        $this->registerFill($registeredStyle);
51 42
        $this->registerBorder($registeredStyle);
52
53 42
        return $registeredStyle;
54
    }
55
56
    /**
57
     * Register a fill definition
58
     *
59
     * @param Style $style
60
     */
61 42
    private function registerFill(Style $style)
62
    {
63 42
        $styleId = $style->getId();
64
65
        // Currently - only solid backgrounds are supported
66
        // so $backgroundColor is a scalar value (RGB Color)
67 42
        $backgroundColor = $style->getBackgroundColor();
68
69 42
        if ($backgroundColor) {
70 4
            $isBackgroundColorRegistered = isset($this->registeredFills[$backgroundColor]);
71
72
            // We need to track the already registered background definitions
73 4
            if ($isBackgroundColorRegistered) {
74 3
                $registeredStyleId = $this->registeredFills[$backgroundColor];
75 3
                $registeredFillId = $this->styleIdToFillMappingTable[$registeredStyleId];
76 3
                $this->styleIdToFillMappingTable[$styleId] = $registeredFillId;
77
            } else {
78 4
                $this->registeredFills[$backgroundColor] = $styleId;
79 4
                $this->styleIdToFillMappingTable[$styleId] = $this->fillIndex++;
80
            }
81
        } else {
82
            // The fillId maps a style to a fill declaration
83
            // When there is no background color definition - we default to 0
84 42
            $this->styleIdToFillMappingTable[$styleId] = 0;
85
        }
86 42
    }
87
88
    /**
89
     * @param int $styleId
90
     * @return int|null Fill ID associated to the given style ID
91
     */
92 14
    public function getFillIdForStyleId($styleId)
93
    {
94 14
        return (isset($this->styleIdToFillMappingTable[$styleId])) ?
95 14
            $this->styleIdToFillMappingTable[$styleId] :
96 14
            null;
97
    }
98
99
    /**
100
     * Register a border definition
101
     *
102
     * @param Style $style
103
     */
104 42
    private function registerBorder(Style $style)
105
    {
106 42
        $styleId = $style->getId();
107
108 42
        if ($style->shouldApplyBorder()) {
109 5
            $border = $style->getBorder();
110 5
            $serializedBorder = serialize($border);
111
112 5
            $isBorderAlreadyRegistered = isset($this->registeredBorders[$serializedBorder]);
113
114 5
            if ($isBorderAlreadyRegistered) {
115 3
                $registeredStyleId = $this->registeredBorders[$serializedBorder];
116 3
                $registeredBorderId = $this->styleIdToBorderMappingTable[$registeredStyleId];
117 3
                $this->styleIdToBorderMappingTable[$styleId] = $registeredBorderId;
118
            } else {
119 5
                $this->registeredBorders[$serializedBorder] = $styleId;
120 5
                $this->styleIdToBorderMappingTable[$styleId] = count($this->registeredBorders);
121
            }
122
        } else {
123
            // If no border should be applied - the mapping is the default border: 0
124 42
            $this->styleIdToBorderMappingTable[$styleId] = 0;
125
        }
126 42
    }
127
128
    /**
129
     * @param int $styleId
130
     * @return int|null Fill ID associated to the given style ID
131
     */
132 14
    public function getBorderIdForStyleId($styleId)
133
    {
134 14
        return (isset($this->styleIdToBorderMappingTable[$styleId])) ?
135 14
            $this->styleIdToBorderMappingTable[$styleId] :
136 14
            null;
137
    }
138
139
    /**
140
     * @return array
141
     */
142 37
    public function getRegisteredFills()
143
    {
144 37
        return $this->registeredFills;
145
    }
146
147
    /**
148
     * @return array
149
     */
150 37
    public function getRegisteredBorders()
151
    {
152 37
        return $this->registeredBorders;
153
    }
154
}
155