Passed
Pull Request — master (#4581)
by Owen
11:44
created

ColumnDimension::setAutoSize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
ccs 0
cts 0
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace PhpOffice\PhpSpreadsheet\Worksheet;
4
5
use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
6
use PhpOffice\PhpSpreadsheet\Helper\Dimension as CssDimension;
7
8
class ColumnDimension extends Dimension
9
{
10
    public const EXCEL_MAX_WIDTH = 255.0;
11
12
    /**
13
     * Column index.
14
     */
15
    private ?string $columnIndex;
16
17
    /**
18
     * Column width.
19
     *
20
     * When this is set to a negative value, the column width should be ignored by IWriter
21
     */
22
    private float $width = -1;
23
24
    /**
25
     * Auto size?
26
     */
27
    private bool $autoSize = false;
28
29
    /**
30
     * Create a new ColumnDimension.
31
     *
32 10697
     * @param ?string $index Character column index
33
     */
34
    public function __construct(?string $index = 'A')
35 10697
    {
36
        // Initialise values
37
        $this->columnIndex = $index;
38 10697
39
        // set dimension as unformatted by default
40
        parent::__construct(0);
41
    }
42
43
    /**
44 178
     * Get column index as string eg: 'A'.
45
     */
46 178
    public function getColumnIndex(): ?string
47
    {
48
        return $this->columnIndex;
49
    }
50
51
    /**
52 20
     * Set column index as string eg: 'A'.
53
     */
54 20
    public function setColumnIndex(string $index): self
55
    {
56 20
        $this->columnIndex = $index;
57
58
        return $this;
59
    }
60
61
    /**
62 99
     * Get column index as numeric.
63
     */
64 99
    public function getColumnNumeric(): int
65
    {
66
        return Coordinate::columnIndexFromString($this->columnIndex ?? '');
67
    }
68
69
    /**
70 18
     * Set column index as numeric.
71
     */
72 18
    public function setColumnNumeric(int $index): self
73
    {
74 18
        $this->columnIndex = Coordinate::stringFromColumnIndex($index);
75
76
        return $this;
77
    }
78
79
    /**
80
     * Get Width.
81
     *
82
     * Each unit of column width is equal to the width of one character in the default font size. A value of -1
83
     *      tells Excel to display this column in its default width.
84
     * By default, this will be the return value; but this method also accepts an optional unit of measure argument
85 593
     *    and will convert the returned value to the specified UoM..
86
     */
87 593
    public function getWidth(?string $unitOfMeasure = null): float
88 593
    {
89 593
        return ($unitOfMeasure === null || $this->width < 0)
90
            ? $this->width
91
            : (new CssDimension((string) $this->width))->toUnit($unitOfMeasure);
92
    }
93
94
    public function getWidthForOutput(bool $restrictMax): float
95
    {
96
        return ($restrictMax && $this->width > self::EXCEL_MAX_WIDTH) ? self::EXCEL_MAX_WIDTH : $this->width;
97
    }
98
99
    /**
100
     * Set Width.
101
     *
102
     * Each unit of column width is equal to the width of one character in the default font size. A value of -1
103 646
     *      tells Excel to display this column in its default width.
104
     * By default, this will be the unit of measure for the passed value; but this method also accepts an
105 646
     *    optional unit of measure argument, and will convert the value from the specified UoM using an
106 601
     *    approximation method.
107 50
     *
108
     * @return $this
109 646
     */
110
    public function setWidth(float $width, ?string $unitOfMeasure = null): static
111
    {
112
        $this->width = ($unitOfMeasure === null || $width < 0)
113
            ? $width
114
            : (new CssDimension("{$width}{$unitOfMeasure}"))->width();
115 158
116
        return $this;
117 158
    }
118
119
    /**
120
     * Get Auto Size.
121
     */
122
    public function getAutoSize(): bool
123
    {
124
        return $this->autoSize;
125 69
    }
126
127 69
    /**
128
     * Set Auto Size.
129 69
     *
130
     * @return $this
131
     */
132
    public function setAutoSize(bool $autosizeEnabled): static
133
    {
134
        $this->autoSize = $autosizeEnabled;
135
136
        return $this;
137
    }
138
}
139