Failed Conditions
Pull Request — master (#532)
by
unknown
03:45
created

Writer::setColumnsWidth()   C

Complexity

Conditions 7
Paths 12

Size

Total Lines 26
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 0
Metric Value
dl 0
loc 26
ccs 0
cts 15
cp 0
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 15
nc 12
nop 3
crap 56
1
<?php
2
3
namespace Box\Spout\Writer\XLSX;
4
5
use Box\Spout\Writer\AbstractMultiSheetsWriter;
6
use Box\Spout\Writer\Style\StyleBuilder;
7
use Box\Spout\Writer\XLSX\Internal\Workbook;
8
9
/**
10
 * Class Writer
11
 * This class provides base support to write data to XLSX files
12
 *
13
 * @package Box\Spout\Writer\XLSX
14
 */
15
class Writer extends AbstractMultiSheetsWriter
16
{
17
    /** Default style font values */
18
    const DEFAULT_FONT_SIZE = 12;
19
    const DEFAULT_FONT_NAME = 'Calibri';
20
21
    /** @var string Content-Type value for the header */
22
    protected static $headerContentType = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';
23
24
    /** @var string Temporary folder where the files to create the XLSX will be stored */
25
    protected $tempFolder;
26
27
    /** @var bool Whether inline or shared strings should be used - inline string is more memory efficient */
28
    protected $shouldUseInlineStrings = true;
29
30
    /** @var Internal\Workbook The workbook for the XLSX file */
31
    protected $book;
32
33
    /** @var array Collection of column dimensions */
34
    protected $columnsWidth = [];
35
36
    /**
37
     * Defines column width for one or more columns of the worksheet.
38
     *
39
     * @param int $width
40
     * @param int $min first column affected by this 'column info' record
41
     * @param int|null $max last column affected by this 'column info' record
42
     * @return Writer
43
     */
44
    public function setColumnsWidth($width, $min = 1, $max = null)
45
    {
46
        $columnLength = count($this->columnsWidth);
47
48
        if ($columnLength > 0 and $min == 1) {
49
            $min = $columnLength + 1;
50
        } elseif ($columnLength > 0 and $min > 1) {
51
            $min = $columnLength;
52
        }
53
54
        if ($max === null) {
55
            $max = $min;
56
        }
57
58
        $this->columnsWidth[] = [
59
            'width' => $width,
60
            'min' => $min,
61
            'max' => $max
62
        ];
63
64
        if ($this->book) {
65
            $this->book->setColumnsWidth($this->columnsWidth);
66
        }
67
68
        return $this;
69
    }
70
71
    /**
72
     * Sets a custom temporary folder for creating intermediate files/folders.
73
     * This must be set before opening the writer.
74
     *
75
     * @api
76
     * @param string $tempFolder Temporary folder where the files to create the XLSX will be stored
77
     * @return Writer
78
     * @throws \Box\Spout\Writer\Exception\WriterAlreadyOpenedException If the writer was already opened
79
     */
80 2
    public function setTempFolder($tempFolder)
81
    {
82 2
        $this->throwIfWriterAlreadyOpened('Writer must be configured before opening it.');
83
84 1
        $this->tempFolder = $tempFolder;
85 1
        return $this;
86
    }
87
88
    /**
89
     * Use inline string to be more memory efficient. If set to false, it will use shared strings.
90
     * This must be set before opening the writer.
91
     *
92
     * @api
93
     * @param bool $shouldUseInlineStrings Whether inline or shared strings should be used
94
     * @return Writer
95
     * @throws \Box\Spout\Writer\Exception\WriterAlreadyOpenedException If the writer was already opened
96
     */
97 28
    public function setShouldUseInlineStrings($shouldUseInlineStrings)
98
    {
99 28
        $this->throwIfWriterAlreadyOpened('Writer must be configured before opening it.');
100
101 27
        $this->shouldUseInlineStrings = $shouldUseInlineStrings;
102 27
        return $this;
103
    }
104
105
    /**
106
     * Configures the write and sets the current sheet pointer to a new sheet.
107
     *
108
     * @return void
109
     * @throws \Box\Spout\Common\Exception\IOException If unable to open the file for writing
110
     */
111 44
    protected function openWriter()
112
    {
113 44
        if (!$this->book) {
114 44
            $tempFolder = ($this->tempFolder) ? : sys_get_temp_dir();
115 44
            $this->book = new Workbook($tempFolder, $this->shouldUseInlineStrings, $this->shouldCreateNewSheetsAutomatically, $this->defaultRowStyle, $this->columnsWidth);
116 44
            $this->book->addNewSheetAndMakeItCurrent();
117
        }
118 44
    }
119
120
    /**
121
     * @return Internal\Workbook The workbook representing the file to be written
122
     */
123 34
    protected function getWorkbook()
124
    {
125 34
        return $this->book;
126
    }
127
128
    /**
129
     * Adds data to the currently opened writer.
130
     * If shouldCreateNewSheetsAutomatically option is set to true, it will handle pagination
131
     * with the creation of new worksheets if one worksheet has reached its maximum capicity.
132
     *
133
     * @param array $dataRow Array containing data to be written.
134
     *          Example $dataRow = ['data1', 1234, null, '', 'data5'];
135
     * @param \Box\Spout\Writer\Style\Style $style Style to be applied to the row.
136
     * @return void
137
     * @throws \Box\Spout\Writer\Exception\WriterNotOpenedException If the book is not created yet
138
     * @throws \Box\Spout\Common\Exception\IOException If unable to write data
139
     */
140 31
    protected function addRowToWriter(array $dataRow, $style)
141
    {
142 31
        $this->throwIfBookIsNotAvailable();
143 31
        $this->book->addRowToCurrentWorksheet($dataRow, $style);
144 29
    }
145
146
    /**
147
     * Returns the default style to be applied to rows.
148
     *
149
     * @return \Box\Spout\Writer\Style\Style
150
     */
151 49
    protected function getDefaultRowStyle()
152
    {
153 49
        return (new StyleBuilder())
154 49
            ->setFontSize(self::DEFAULT_FONT_SIZE)
155 49
            ->setFontName(self::DEFAULT_FONT_NAME)
156 49
            ->build();
157
    }
158
159
    /**
160
     * Closes the writer, preventing any additional writing.
161
     *
162
     * @return void
163
     */
164 34
    protected function closeWriter()
165
    {
166 34
        if ($this->book) {
167 34
            $this->book->close($this->filePointer);
168
        }
169 34
    }
170
}
171