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

Writer   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Test Coverage

Coverage 47.5%

Importance

Changes 16
Bugs 0 Features 0
Metric Value
wmc 15
eloc 32
dl 0
loc 102
ccs 19
cts 40
cp 0.475
rs 10
c 16
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A write() 0 5 1
A setHeaders() 0 7 1
A setData() 0 9 2
A addHeader() 0 9 1
A addRows() 0 4 2
A getContents() 0 15 3
A getOpenMode() 0 3 1
A addRow() 0 14 4
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