Completed
Push — master ( 3ce8ef...9b7d4c )
by Stefan
02:29
created

Writer::disableCellAutosizing()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 5
ccs 0
cts 3
cp 0
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
crap 2
1
<?php
2
3
namespace OneSheet;
4
5
use OneSheet\Style\Style;
6
use OneSheet\Style\Styler;
7
8
/**
9
 * Class Writer
10
 *
11
 * @package OneSheet
12
 */
13
class Writer
14
{
15
    /**
16
     * @var SheetFile
17
     */
18
    private $sheetFile;
19
20
    /**
21
     * @var Styler
22
     */
23
    private $styler;
24
25
    /**
26
     * @var Sheet
27
     */
28
    private $sheet;
29
30
    /**
31
     * @var string
32
     */
33
    private $encoding;
34
35
    /**
36
     * Writer constructor.
37
     *
38
     * @param string $encoding
39
     */
40 1
    public function __construct($encoding = 'utf-8')
41
    {
42 1
        $this->encoding = $encoding;
43 1
        $this->initialize();
44 1
    }
45
46
    /**
47
     * All cells _above_ this cell will be fozen/fixed.
48
     *
49
     * @param int $cellId
50
     */
51
    public function setfreezePaneCellId($cellId)
52
    {
53
        $this->sheet->setFreezePaneId($cellId);
54
    }
55
56
    /**
57
     * Start recording row specs for column autosizing.
58
     *
59
     *  @return Writer
60
     */
61 1
    public function enableCellAutosizing()
62
    {
63 1
        $this->sheet->enableCellAutosizing();
64 1
        return $this;
65
    }
66
67
    /**
68
     * Stop recording row specs for column autosizing.
69
     *
70
     *  @return Writer
71
     */
72
    public function disableCellAutosizing()
73
    {
74
        $this->sheet->disableCellAutosizing();
75
        return $this;
76
    }
77
78
    /**
79
     * Initialize writer defaults.
80
     */
81 1
    private function initialize()
82
    {
83 1
        $this->sheetFile = new SheetFile();
84 1
        $this->sheetFile->fwrite(str_repeat(' ', pow(2, 20)) . '<sheetData>');
85 1
        $this->styler = new Styler();
86 1
        $this->sheet = new Sheet();
87 1
    }
88
89
    /**
90
     * @param array $rows
91
     * @param Style $style
92
     */
93 1
    public function addRows(array $rows, Style $style = null)
94
    {
95 1
        if (count($rows) === count($rows, COUNT_RECURSIVE)) {
96
            throw new \InvalidArgumentException("Array must contain arrays!");
97
        }
98
99 1
        foreach ($rows as $row) {
100 1
            $this->addRow($row, $style);
101 1
        }
102 1
    }
103
104
    /**
105
     * @param array $row
106
     * @param Style $style
107
     */
108 1
    public function addRow(array $row, Style $style = null)
109
    {
110 1
        $style = $style instanceof Style ? $style : $this->styler->getDefaultStyle();
111 1
        $this->styler->addStyle($style);
112 1
        $this->sheetFile->fwrite($this->sheet->addRow($row, $style));
113 1
    }
114
115
    /**
116
     * Wrap things up and write/output xlsx.
117
     *
118
     * @param string $fileName
119
     */
120 1
    public function writeToFile($fileName = 'dummy.xlsx')
121
    {
122 1
        $finalizer = new Finalizer($this->sheet, $this->styler, $this->sheetFile, $this->encoding);
123 1
        $finalizer->finalize($fileName);
124 1
    }
125
}
126