Completed
Push — master ( b68650...58f9ca )
by Stefan
02:39
created

Sheet::fromDefaults()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
/**
3
 * @author neun
4
 * @since  2016-07-03
5
 */
6
7
namespace OneSheet;
8
9
/**
10
 * Class Sheet to write the sheet1.xml contents to file.
11
 * @package OneSheet
12
 */
13
class Sheet
14
{
15
    /**
16
     * Contains the <sheetData> section.
17
     *
18
     * @var \SplFileObject
19
     */
20
    private $sheetData;
21
22
    /**
23
     * @var RowBuilderInterface
24
     */
25
    private $rowBuilder;
26
27
    /**
28
     * Sheet constructor to init SplFileObject and write xml header.
29
     * Optionally supply a cell id like e.g. A2 to add freeze pane.
30
     * It has to be done right away, since everything is immediately
31
     * written to the SplFileObject.
32
     *
33
     * @param RowBuilderInterface $rowBuilder
34
     * @param string|null         $freezePaneCellId
35
     */
36 3
    public function __construct(RowBuilderInterface $rowBuilder, $freezePaneCellId = null)
37
    {
38 3
        $this->rowBuilder = $rowBuilder;
39
40 3
        $this->sheetData = new \SplFileObject(sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'sheet1.xml', 'wb+');
41 3
        $this->sheetData->fwrite('<?xml version="1.0" encoding="UTF-8" standalone="yes"?><worksheet xml:space="preserve" xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships">');
42
43 3
        if (1 == preg_match('~[A-Z]+([1-9]?[0-9]+)$~', $freezePaneCellId, $match)) {
44 1
            $this->sheetData->fwrite('<sheetViews><sheetView tabSelected="1" workbookViewId="0" showGridLines="true" showRowColHeaders="1"><pane ySplit="' . (array_pop($match) - 1) . '" topLeftCell="' . $freezePaneCellId . '" activePane="bottomLeft" state="frozen"/></sheetView></sheetViews>');
45 1
        }
46
47 3
        $this->sheetData->fwrite('<sheetData>');
48 3
    }
49
50
    /**
51
     * Instantiate new sheet with default RowBuilder implementation.
52
     *
53
     * @param string|null $freezePaneCellId
54
     * @return Sheet
55
     */
56 3
    public static function fromDefaults($freezePaneCellId = null)
57
    {
58 3
        return new self(new DefaultRowBuilder(new DefaultCellBuilder()), $freezePaneCellId);
59
    }
60
61
    /**
62
     * Add single data row to sheet and add new style,
63
     * if its not an integer.
64
     *
65
     * @param array     $dataRow
66
     * @param int|Style $style
67
     */
68 1
    public function addRow(array $dataRow, $style = 0)
69
    {
70 1
        if (!is_int($style)) {
71 1
            $style = $this->addStyle($style);
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $style. This often makes code more readable.
Loading history...
72 1
        }
73
74 1
        $this->sheetData->fwrite($this->rowBuilder->build($dataRow, $style));
75 1
    }
76
77
    /**
78
     * Add multiple data rows to sheet.
79
     *
80
     * @param array     $dataRows
81
     * @param int|Style $style
82
     */
83 1
    public function addRows(array $dataRows, $style = 0)
84
    {
85 1
        foreach ($dataRows as $dataRow) {
86 1
            $this->addRow($dataRow, $style);
87 1
        }
88 1
    }
89
90
    /**
91
     * Add new style and return its id.
92
     *
93
     * @param Style $style
94
     * @return int
95
     */
96 1
    public function addStyle(Style $style)
97
    {
98 1
        return StyleHelper::buildStyle($style);
99
    }
100
101
    /**
102
     * Write closing xml tag and return path to sheet file.
103
     *
104
     * @return string
105
     */
106 2
    public function sheetFilePath()
107
    {
108 2
        $this->sheetData->fwrite('</sheetData></worksheet>');
109 2
        return (string)$this->sheetData->getFileInfo();
110
    }
111
}
112