Completed
Push — master ( ee5ef1...b9a862 )
by Stefan
02:49
created

Sheet::enableCellAutosizing()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 2
Bugs 1 Features 1
Metric Value
c 2
b 1
f 1
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace OneSheet;
4
5
use OneSheet\Xml\RowXml;
6
use OneSheet\Style\Style;
7
use OneSheet\Xml\SheetXml;
8
use OneSheet\Width\WidthCalculator;
9
10
/**
11
 * Class Sheet
12
 *
13
 * @package OneSheet
14
 */
15
class Sheet
16
{
17
    /**
18
     * @var CellBuilder
19
     */
20
    private $cellBuilder;
21
22
    /**
23
     * @var WidthCalculator
24
     */
25
    private $widthCalculator;
26
27
    /**
28
     * @var bool
29
     */
30
    private $useCellAutosizing = false;
31
32
    /**
33
     * @var int
34
     */
35
    private $freezePaneId;
36
37
    /**
38
     * Track next row index.
39
     *
40
     * @var int
41
     */
42
    private $rowIndex = 1;
43
44
    /**
45
     * Holds width/column count of the widest row.
46
     *
47
     * @var int
48
     */
49
    private $maxRowWidth;
50
51
    /**
52
     * Holds widths of the widest cells for column sizing.
53
     *
54
     * @var array
55
     */
56
    private $columnWidths = array();
57
58
    /**
59
     * Sheet constructor.
60
     */
61 6
    public function __construct()
62 1
    {
63 6
        $this->cellBuilder = new CellBuilder();
64 6
        $this->widthCalculator = new WidthCalculator();
65 6
    }
66
67
    /**
68
     * Enable cell autosizing (~30% performance hit!).
69
     */
70 3
    public function enableCellAutosizing()
71
    {
72 3
        $this->useCellAutosizing = true;
73 3
    }
74
75
    /**
76
     * Disable cell autosizing (default).
77
     */
78 1
    public function disableCellAutosizing()
79
    {
80 1
        $this->useCellAutosizing = false;
81 1
    }
82
83
    /**
84
     * Return array containing all column widths.
85
     *
86
     * @return array
87
     */
88 3
    public function getColumnWidths()
89
    {
90 3
        return $this->columnWidths;
91
    }
92
93
    /**
94
     * Return cellId for dimensions.
95
     *
96
     * @return string
97
     */
98 1
    public function getDimensionUpperBound()
99
    {
100 1
        return $this->cellBuilder->getCellId($this->maxRowWidth, $this->rowIndex - 1);
101
    }
102
103
    /**
104
     * Add single row and style to sheet.
105
     *
106
     * @param array $row
107
     * @param Style $style
108
     *
109
     * @return string
110
     */
111 4
    public function addRow(array $row, Style $style)
112
    {
113 4
        $rowWidth = count($row);
114 4
        $this->updateMaxRowWidth($rowWidth);
115
116 4
        $this->widthCalculator->setFont($style->getFont());
117 4
        $cellXml = $this->getCellXml($row, $style);
118
119 4
        if (!$this->useCellAutosizing || $style->getFont()->getSize() < 14) {
120 4
            return sprintf(RowXml::DEFAULT_XML, $this->rowIndex++, $rowWidth, $cellXml);
121
        }
122
123 1
        return sprintf(RowXml::HEIGHT_XML, $this->rowIndex++, $rowWidth,
124 1
            $style->getFont()->getSize() * 1.4, $cellXml);
125
    }
126
127
    /**
128
     * @param int $cellId
129
     */
130 1
    public function setFreezePaneId($cellId)
131
    {
132 1
        $this->freezePaneId = $cellId;
133 1
    }
134
135
    /**
136
     * Track widest row for dimensions xml (e.g. A1:K123).
137
     *
138
     * @param int $rowWidth
139
     */
140 4
    private function updateMaxRowWidth($rowWidth)
141
    {
142 4
        if ($this->maxRowWidth < $rowWidth) {
143 4
            $this->maxRowWidth = $rowWidth;
144 4
        }
145 4
    }
146
147
    /**
148
     * Get xml string for single cell and update cell widths.
149
     *
150
     * @param array $row
151
     * @param Style $style
152
     * @return string
153
     */
154 4
    private function getCellXml(array $row, Style $style)
155
    {
156 4
        $cellXml = '';
157 4
        foreach (array_values($row) as $cellIndex => $cellValue) {
158 4
            if (0 < strlen($cellValue)) {
159 4
                $this->updateColumnWidths($cellValue, $cellIndex, $style);
160 4
                $cellXml .= $this->cellBuilder->build($this->rowIndex, $cellIndex, $cellValue, $style->getId());
161 4
            }
162 4
        }
163
164 4
        return $cellXml;
165
    }
166
167
    /**
168
     * Track cell width for column width sizing if its enabled.
169
     *
170
     * @param mixed $value
171
     * @param int   $cellIndex
172
     * @param Style $style
173
     */
174 4
    private function updateColumnWidths($value, $cellIndex, Style $style)
175
    {
176 4
        if ($this->useCellAutosizing) {
177 3
            $cellWidth = $this->widthCalculator->getCellWidth($value, $style->getFont());
178 3
            if (!isset($this->columnWidths[$cellIndex])
179 3
                || $this->columnWidths[$cellIndex] < $cellWidth
180 3
            ) {
181 3
                $this->columnWidths[$cellIndex] = $cellWidth;
182 3
            }
183 3
        }
184 4
    }
185
186
    /**
187
     * Return freeze pane xml string for sheezview.
188
     *
189
     * @return string
190
     */
191 2
    public function getFreezePaneXml()
192
    {
193 2
        if (!$this->freezePaneId
194 2
            || 1 !== preg_match('~^[A-Z]+(\d+)$~', $this->freezePaneId, $m)
195 2
        ) {
196 2
            return '';
197
        }
198 1
        return sprintf(SheetXml::FREEZE_PANE_XML, array_pop($m) - 1, $this->freezePaneId);
199
    }
200
}
201