Passed
Push — master ( 701355...7ade9b )
by Andy
07:12
created

Writer::getContents()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3.0175

Importance

Changes 4
Bugs 0 Features 0
Metric Value
eloc 7
dl 0
loc 15
ccs 7
cts 8
cp 0.875
rs 10
c 4
b 0
f 0
cc 3
nc 3
nop 0
crap 3.0175
1
<?php
2
3
namespace Palmtree\Csv;
4
5
/**
6
 * Writes an array to a CSV file.
7
 */
8
class Writer extends AbstractCsvDocument
9
{
10
    /** @var array */
11
    private $headers = [];
12
13
    public static function write(string $filePath, array $data): void
14
    {
15
        $writer = new self($filePath);
16
        $writer->setData($data);
17
        $writer->closeDocument();
18
    }
19
20
    /**
21
     * Sets headers and all rows on the CSV file and
22
     * then closes the file handle.
23
     *
24
     * Uses the first row's keys as headers.
25
     */
26
    public function setData(array $data): self
27
    {
28
        if ($this->hasHeaders) {
29
            $this->setHeaders(array_keys(reset($data)));
30
        }
31
32
        $this->addRows($data);
33
34
        return $this;
35
    }
36
37 3
    public function setHeaders(array $headers): self
38
    {
39 3
        $this->headers = $headers;
40
41 3
        $this->addRow($this->headers);
42
43 3
        return $this;
44
    }
45
46
    public function addHeader(string $header): self
47
    {
48
        $headers = $this->headers;
49
50
        $headers[] = $header;
51
52
        $this->setHeaders($headers);
53
54
        return $this;
55
    }
56
57
    /**
58
     * Adds multiple rows of data to the CSV file.
59
     */
60
    public function addRows(array $rows): void
61
    {
62
        foreach ($rows as $row) {
63
            $this->addRow($row);
64
        }
65
    }
66
67
    /**
68
     * Adds a row of data to the CSV file.
69
     *
70
     * @param array $row Row of data to add to the file.
71
     *
72
     * @return bool Whether the row was written to the file.
73
     */
74 3
    public function addRow(array $row): bool
75
    {
76 3
        if ($this->hasHeaders && empty($this->headers)) {
77 1
            $this->setHeaders(array_keys($row));
78
        }
79
80 3
        $result = $this->getDocument()->fwriteCsv($row);
81
82 3
        if ($result === 0) {
83
            // @todo: handle error
84
            return false;
85
        }
86
87 3
        return true;
88
    }
89
90 4
    public function getContents(): string
91
    {
92 4
        $this->getDocument()->trimFinalLineEnding();
93
94 4
        if (($size = $this->getDocument()->getSize()) === 0) {
95 1
            return '';
96
        }
97
98 3
        $this->getDocument()->fseek(0);
99
100 3
        if (($data = $this->getDocument()->fread($size)) === false) {
101
            return '';
102
        }
103
104 3
        return $data;
105
    }
106
107 4
    protected function getOpenMode(): string
108
    {
109 4
        return 'w+';
110
    }
111
}
112