1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the Csv-Machine package. |
5
|
|
|
* |
6
|
|
|
* (c) Dan McAdams <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace RoadBunch\Csv; |
13
|
|
|
|
14
|
|
|
use RoadBunch\Csv\Exception\InvalidInputArrayException; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class Writer |
18
|
|
|
* |
19
|
|
|
* @author Dan McAdams |
20
|
|
|
* @package RoadBunch\Csv |
21
|
|
|
*/ |
22
|
|
|
class Writer extends Csv implements WriterInterface |
23
|
|
|
{ |
24
|
|
|
/** @var array */ |
25
|
|
|
protected $header; |
26
|
|
|
|
27
|
|
|
/** @var array */ |
28
|
|
|
protected $rows = []; |
29
|
|
|
|
30
|
|
|
/** @var bool */ |
31
|
|
|
protected $isStreamSeekable = false; |
32
|
|
|
|
33
|
|
|
/** @var resource */ |
34
|
|
|
protected $handle; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param array $header |
38
|
|
|
*/ |
39
|
2 |
|
public function setHeader(array $header) |
40
|
|
|
{ |
41
|
2 |
|
$this->header = $header; |
42
|
2 |
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param array $row |
46
|
|
|
* @return WriterInterface |
47
|
|
|
*/ |
48
|
1 |
|
public function addRow(array $row): WriterInterface |
49
|
|
|
{ |
50
|
1 |
|
$this->rows[] = $row; |
51
|
1 |
|
return $this; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param array $rows |
56
|
|
|
* @throws InvalidInputArrayException |
57
|
|
|
*/ |
58
|
3 |
|
public function addRows(array $rows) |
59
|
|
|
{ |
60
|
3 |
|
foreach ($rows as $row) { |
61
|
3 |
|
if (!is_array($row)) { |
62
|
1 |
|
throw new InvalidInputArrayException('Element must be an array'); |
63
|
|
|
} |
64
|
2 |
|
$this->rows[] = $row; |
65
|
|
|
} |
66
|
2 |
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Write the CSV to the stream |
70
|
|
|
* |
71
|
|
|
* @param string $filename |
72
|
|
|
*/ |
73
|
2 |
|
public function write(string $filename) |
74
|
|
|
{ |
75
|
2 |
|
$this->openStream($filename); |
76
|
2 |
|
$this->writeRows(); |
77
|
2 |
|
$this->closeStream(); |
78
|
2 |
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param string $filename |
82
|
|
|
*/ |
83
|
2 |
|
private function openStream(string $filename) |
84
|
|
|
{ |
85
|
2 |
|
$this->handle = fopen($filename, 'w+'); |
|
|
|
|
86
|
2 |
|
$this->setSeekableFlag(); |
87
|
2 |
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Close the stream |
91
|
|
|
*/ |
92
|
2 |
|
private function closeStream() |
93
|
|
|
{ |
94
|
2 |
|
fclose($this->handle); |
95
|
2 |
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* If the requested newline is not PHP default \n update the newline character |
99
|
|
|
*/ |
100
|
2 |
|
private function updateNewLine() |
101
|
|
|
{ |
102
|
2 |
|
if ((Newline::NEWLINE_LF !== $this->newline) && (0 === fseek($this->handle, -1, SEEK_CUR))) { |
103
|
1 |
|
fwrite($this->handle, $this->newline); |
104
|
|
|
} |
105
|
2 |
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Write a row to the stream, if needed, update line endings |
109
|
|
|
* |
110
|
|
|
* @param $row |
111
|
|
|
*/ |
112
|
2 |
|
private function writeRow(array $row) |
113
|
|
|
{ |
114
|
2 |
|
fputcsv($this->handle, $row, $this->delimiter, $this->enclosure, $this->escape); |
115
|
|
|
|
116
|
2 |
|
if ($this->isStreamSeekable) { |
117
|
2 |
|
$this->updateNewLine(); |
118
|
|
|
} |
119
|
2 |
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Write CSV header and rows to stream |
123
|
|
|
*/ |
124
|
2 |
|
private function writeRows() |
125
|
|
|
{ |
126
|
2 |
|
if ($this->header) { |
|
|
|
|
127
|
2 |
|
$this->writeRow($this->header); |
128
|
|
|
} |
129
|
2 |
|
foreach ($this->rows as $row) { |
130
|
2 |
|
$this->writeRow($row); |
131
|
|
|
} |
132
|
2 |
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Checks resource, if it's seekable set the seekable flag |
136
|
|
|
* this will be used when determining if line endings should be updated |
137
|
|
|
*/ |
138
|
2 |
|
private function setSeekableFlag() |
139
|
|
|
{ |
140
|
2 |
|
if (stream_get_meta_data($this->handle)['seekable']) { |
141
|
2 |
|
$this->isStreamSeekable = true; |
142
|
2 |
|
return; |
143
|
|
|
} |
144
|
|
|
// @todo: add return for not seekable. I haven't written a test for it, so that's why it's not here |
145
|
|
|
// @todo: but it is definitely a bug |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.