Completed
Pull Request — master (#808)
by
unknown
11:17
created

ManagesCellSize::setColumnWidthForRange()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 3
1
<?php
2
3
namespace Box\Spout\Writer\Common\Manager;
4
5
trait ManagesCellSize
6
{
7
    /** @var float|null The default column width to use */
8
    private $defaultColumnWidth;
9
10
    /** @var float|null The default row height to use */
11
    private $defaultRowHeight;
12
13
    /** @var array Array of min-max-width arrays */
14
    private $columnWidths = [];
15
16
    /**
17
     * @param float|null $width
18
     */
19
    public function setDefaultColumnWidth($width)
20
    {
21
        $this->defaultColumnWidth = $width;
22
    }
23
24
    /**
25
     * @param float|null $height
26
     */
27
    public function setDefaultRowHeight($height)
28
    {
29
        $this->defaultRowHeight = $height;
30
    }
31
32
    /**
33
     * @param float $width
34
     * @param array $columns One or more columns with this width
35
     */
36
    public function setColumnWidth(float $width, ...$columns)
37
    {
38
        // Gather sequences
39
        $sequence = [];
40
        foreach ($columns as $i) {
41
            $sequenceLength = count($sequence);
42
            if ($sequenceLength > 0) {
43
                $previousValue = $sequence[$sequenceLength - 1];
44
                if ($i !== $previousValue + 1) {
45
                    $this->setColumnWidthForRange($width, $sequence[0], $previousValue);
46
                    $sequence = [];
47
                }
48
            }
49
            $sequence[] = $i;
50
        }
51
        $this->setColumnWidthForRange($width, $sequence[0], $sequence[count($sequence) - 1]);
52
    }
53
54
    /**
55
     * @param float $width The width to set
56
     * @param int $start First column index of the range
57
     * @param int $end Last column index of the range
58
     */
59
    public function setColumnWidthForRange(float $width, int $start, int $end)
60
    {
61
        $this->columnWidths[] = [$start, $end, $width];
62
    }
63
}
64