Passed
Branch develop (e9a151)
by Stefan
03:34 queued 45s
created

Sheet::sheetFilePath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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