Completed
Pull Request — develop_3.0 (#433)
by Adrien
06:44
created

StyleRegistry   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 146
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 2
dl 0
loc 146
ccs 44
cts 44
cp 1
rs 10
c 0
b 0
f 0

7 Methods

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