Skip to content

Commit c438c68

Browse files
author
Alex Boyce
authored
Merge pull request #59 from advisors-excel-llc/develop
v1.3.6
2 parents 0c8f921 + 0a97859 commit c438c68

File tree

2 files changed

+138
-33
lines changed

2 files changed

+138
-33
lines changed

Tests/Psr7/CsvStreamTest.php

Lines changed: 92 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,49 +17,122 @@ class CsvStreamTest extends TestCase
1717
public function testRead()
1818
{
1919
$testLine = '"Item 1","Item 2","This is a longer'.PHP_EOL.'multiline field","Last Field"'.PHP_EOL;
20-
$stream = stream_for($testLine);
21-
$csv = new CsvStream($stream);
20+
$stream = stream_for($testLine);
21+
$csv = new CsvStream($stream);
2222

2323
$line = CsvStream::readline($stream);
2424

2525
$this->assertEquals(rtrim($testLine, "\n"), $line);
2626

2727
$stream->rewind();
28+
$rows = [];
2829

29-
$row = $csv->read();
30+
while ($row = $csv->read()) {
31+
$rows[] = $row;
32+
}
3033

3134
$this->assertEquals(
3235
[
33-
'Item 1',
34-
'Item 2',
35-
'This is a longer'.PHP_EOL.'multiline field',
36-
'Last Field'
36+
[
37+
'Item 1',
38+
'Item 2',
39+
'This is a longer'.PHP_EOL.'multiline field',
40+
'Last Field',
41+
],
3742
],
38-
$row
43+
$rows
3944
);
4045
}
4146

4247
public function testGetContents()
4348
{
4449
$testLine = '"Name","Alt Name","Description","Alt Desc"'.PHP_EOL.
45-
'"Item 1","Item 2","This is a longer'.PHP_EOL.'multiline field","Last Field"'.PHP_EOL;
46-
$stream = stream_for($testLine);
47-
$csv = new CsvStream($stream);
50+
'"Item 1","Item 2","This is a longer'.PHP_EOL.'multiline field","Last Field"'.PHP_EOL.
51+
'"Item 1.5","Item 2.2","This is a longer'.PHP_EOL.'multiline field","Last Field"'.PHP_EOL;
52+
$stream = stream_for($testLine);
53+
$csv = new CsvStream($stream);
54+
55+
$rows = [];
56+
foreach ($csv->getContents(true) as $row) {
57+
$rows[] = $row;
58+
}
59+
60+
$this->assertEquals(
61+
[
62+
[
63+
"Name" => 'Item 1',
64+
"Alt Name" => 'Item 2',
65+
"Description" => 'This is a longer'.PHP_EOL.'multiline field',
66+
"Alt Desc" => 'Last Field',
67+
],
68+
[
69+
"Name" => 'Item 1.5',
70+
"Alt Name" => 'Item 2.2',
71+
"Description" => 'This is a longer'.PHP_EOL.'multiline field',
72+
"Alt Desc" => 'Last Field',
73+
],
74+
],
75+
$rows
76+
);
77+
}
78+
79+
public function testWrite()
80+
{
81+
$resource = fopen("php://memory", 'r+');
82+
$stream = stream_for($resource);
83+
$csv = new CsvStream($stream);
84+
85+
$csv->write(
86+
[
87+
'Name',
88+
'Alt Name',
89+
'Description',
90+
'Alt Desc',
91+
]
92+
);
93+
94+
$csv->write(
95+
[
96+
'Test 1',
97+
'Test Alt 1',
98+
'Test Description'.PHP_EOL.' thingy',
99+
'Other description',
100+
]
101+
);
102+
103+
$csv->write(
104+
[
105+
'Test 2',
106+
'Test Alt 2',
107+
'Test Description'.PHP_EOL.' thingy 2',
108+
'Other description 2',
109+
]
110+
);
111+
112+
$csv->rewind();
113+
114+
$rows = [];
48115

49116
foreach ($csv->getContents(true) as $row) {
50-
if (false === $row) {
51-
break;
52-
}
117+
$rows[] = $row;
53118
}
54119

55120
$this->assertEquals(
56121
[
57-
"Name" => 'Item 1',
58-
"Alt Name" => 'Item 2',
59-
"Description" => 'This is a longer'.PHP_EOL.'multiline field',
60-
"Alt Desc" => 'Last Field',
122+
[
123+
'Name' => 'Test 1',
124+
'Alt Name' => 'Test Alt 1',
125+
'Description' => 'Test Description'.PHP_EOL.' thingy',
126+
'Alt Desc' => 'Other description',
127+
],
128+
[
129+
'Name' => 'Test 2',
130+
'Alt Name' => 'Test Alt 2',
131+
'Description' => 'Test Description'.PHP_EOL.' thingy 2',
132+
'Alt Desc' => 'Other description 2',
133+
],
61134
],
62-
$row
135+
$rows
63136
);
64137
}
65138
}

src/Psr7/CsvStream.php

Lines changed: 46 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,34 @@ public function isWritable()
6969
return $this->stream->isWritable();
7070
}
7171

72-
public function write($string)
72+
/**
73+
* @param string|array $string
74+
* @param string $delimiter
75+
* @param string $enclosure
76+
* @param string $escapeChar
77+
*
78+
* @return int
79+
*/
80+
public function write($string, $delimiter = ',', $enclosure = '"', $escapeChar = "\\")
7381
{
82+
if (!$this->isWritable()) {
83+
throw new \RuntimeException("The stream is not writable.");
84+
}
85+
86+
if (is_array($string)) {
87+
$arr = array_values($string);
88+
$res = tmpfile();
89+
90+
if (!$res) {
91+
throw new \RuntimeException("The stream is not writable.");
92+
}
93+
94+
fputcsv($res, $arr, $delimiter, $enclosure, $escapeChar);
95+
rewind($res);
96+
$string = stream_get_contents($res);
97+
fclose($res);
98+
}
99+
74100
return $this->stream->write($string);
75101
}
76102

@@ -122,7 +148,13 @@ public static function readline(
122148
$escFlag = $byte === $escape && !$escFlag;
123149
}
124150

125-
return rtrim($buffer, "\n");
151+
$buffer = rtrim($buffer, "\n");
152+
153+
if (strlen($buffer) === 0) {
154+
return false;
155+
}
156+
157+
return $buffer;
126158
}
127159

128160
public function read($length = 0, string $delimiter = ",", string $enclosure = '"', string $escape = "\\")
@@ -133,6 +165,10 @@ public function read($length = 0, string $delimiter = ",", string $enclosure = '
133165

134166
$line = self::readline($this->stream, $length > 0 ? $length : null, $enclosure, $escape);
135167

168+
if (!$line) {
169+
return false;
170+
}
171+
136172
return str_getcsv($line, $delimiter, $enclosure, $escape);
137173
}
138174

@@ -154,19 +190,15 @@ public function getContents(
154190
string $escape = "\\"
155191
) {
156192
$headers = [];
157-
$row = $this->read(0, $delimiter, $enclosure, $escape);
158-
159-
if (false === $row) {
160-
return;
161-
} else {
162-
if ($hasHeaders && empty($headers)) {
163-
$headers = $row;
164-
$row = $this->read(0, $delimiter, $enclosure, $escape);
165-
if (false === $row) {
166-
return;
167-
}
168-
}
169193

194+
if ($hasHeaders) {
195+
$headers = $this->read(0, $delimiter, $enclosure, $escape);
196+
}
197+
198+
while (false !== ($row = $this->read(0, $delimiter, $enclosure, $escape))
199+
&& (count($row) > 0
200+
&& (count($row) > 1 || null !== $row[0]))
201+
) {
170202
yield $hasHeaders ? array_combine($headers, $row) : $row;
171203
}
172204
}

0 commit comments

Comments
 (0)