Skip to content

Commit

Permalink
Merge pull request #59 from advisors-excel-llc/develop
Browse files Browse the repository at this point in the history
v1.3.6
  • Loading branch information
Alex Boyce authored Aug 15, 2019
2 parents 0c8f921 + 0a97859 commit c438c68
Show file tree
Hide file tree
Showing 2 changed files with 138 additions and 33 deletions.
111 changes: 92 additions & 19 deletions Tests/Psr7/CsvStreamTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,49 +17,122 @@ class CsvStreamTest extends TestCase
public function testRead()
{
$testLine = '"Item 1","Item 2","This is a longer'.PHP_EOL.'multiline field","Last Field"'.PHP_EOL;
$stream = stream_for($testLine);
$csv = new CsvStream($stream);
$stream = stream_for($testLine);
$csv = new CsvStream($stream);

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

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

$stream->rewind();
$rows = [];

$row = $csv->read();
while ($row = $csv->read()) {
$rows[] = $row;
}

$this->assertEquals(
[
'Item 1',
'Item 2',
'This is a longer'.PHP_EOL.'multiline field',
'Last Field'
[
'Item 1',
'Item 2',
'This is a longer'.PHP_EOL.'multiline field',
'Last Field',
],
],
$row
$rows
);
}

public function testGetContents()
{
$testLine = '"Name","Alt Name","Description","Alt Desc"'.PHP_EOL.
'"Item 1","Item 2","This is a longer'.PHP_EOL.'multiline field","Last Field"'.PHP_EOL;
$stream = stream_for($testLine);
$csv = new CsvStream($stream);
'"Item 1","Item 2","This is a longer'.PHP_EOL.'multiline field","Last Field"'.PHP_EOL.
'"Item 1.5","Item 2.2","This is a longer'.PHP_EOL.'multiline field","Last Field"'.PHP_EOL;
$stream = stream_for($testLine);
$csv = new CsvStream($stream);

$rows = [];
foreach ($csv->getContents(true) as $row) {
$rows[] = $row;
}

$this->assertEquals(
[
[
"Name" => 'Item 1',
"Alt Name" => 'Item 2',
"Description" => 'This is a longer'.PHP_EOL.'multiline field',
"Alt Desc" => 'Last Field',
],
[
"Name" => 'Item 1.5',
"Alt Name" => 'Item 2.2',
"Description" => 'This is a longer'.PHP_EOL.'multiline field',
"Alt Desc" => 'Last Field',
],
],
$rows
);
}

public function testWrite()
{
$resource = fopen("php://memory", 'r+');
$stream = stream_for($resource);
$csv = new CsvStream($stream);

$csv->write(
[
'Name',
'Alt Name',
'Description',
'Alt Desc',
]
);

$csv->write(
[
'Test 1',
'Test Alt 1',
'Test Description'.PHP_EOL.' thingy',
'Other description',
]
);

$csv->write(
[
'Test 2',
'Test Alt 2',
'Test Description'.PHP_EOL.' thingy 2',
'Other description 2',
]
);

$csv->rewind();

$rows = [];

foreach ($csv->getContents(true) as $row) {
if (false === $row) {
break;
}
$rows[] = $row;
}

$this->assertEquals(
[
"Name" => 'Item 1',
"Alt Name" => 'Item 2',
"Description" => 'This is a longer'.PHP_EOL.'multiline field',
"Alt Desc" => 'Last Field',
[
'Name' => 'Test 1',
'Alt Name' => 'Test Alt 1',
'Description' => 'Test Description'.PHP_EOL.' thingy',
'Alt Desc' => 'Other description',
],
[
'Name' => 'Test 2',
'Alt Name' => 'Test Alt 2',
'Description' => 'Test Description'.PHP_EOL.' thingy 2',
'Alt Desc' => 'Other description 2',
],
],
$row
$rows
);
}
}
60 changes: 46 additions & 14 deletions src/Psr7/CsvStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,34 @@ public function isWritable()
return $this->stream->isWritable();
}

public function write($string)
/**
* @param string|array $string
* @param string $delimiter
* @param string $enclosure
* @param string $escapeChar
*
* @return int
*/
public function write($string, $delimiter = ',', $enclosure = '"', $escapeChar = "\\")
{
if (!$this->isWritable()) {
throw new \RuntimeException("The stream is not writable.");
}

if (is_array($string)) {
$arr = array_values($string);
$res = tmpfile();

if (!$res) {
throw new \RuntimeException("The stream is not writable.");
}

fputcsv($res, $arr, $delimiter, $enclosure, $escapeChar);
rewind($res);
$string = stream_get_contents($res);
fclose($res);
}

return $this->stream->write($string);
}

Expand Down Expand Up @@ -122,7 +148,13 @@ public static function readline(
$escFlag = $byte === $escape && !$escFlag;
}

return rtrim($buffer, "\n");
$buffer = rtrim($buffer, "\n");

if (strlen($buffer) === 0) {
return false;
}

return $buffer;
}

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

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

if (!$line) {
return false;
}

return str_getcsv($line, $delimiter, $enclosure, $escape);
}

Expand All @@ -154,19 +190,15 @@ public function getContents(
string $escape = "\\"
) {
$headers = [];
$row = $this->read(0, $delimiter, $enclosure, $escape);

if (false === $row) {
return;
} else {
if ($hasHeaders && empty($headers)) {
$headers = $row;
$row = $this->read(0, $delimiter, $enclosure, $escape);
if (false === $row) {
return;
}
}

if ($hasHeaders) {
$headers = $this->read(0, $delimiter, $enclosure, $escape);
}

while (false !== ($row = $this->read(0, $delimiter, $enclosure, $escape))
&& (count($row) > 0
&& (count($row) > 1 || null !== $row[0]))
) {
yield $hasHeaders ? array_combine($headers, $row) : $row;
}
}
Expand Down

0 comments on commit c438c68

Please sign in to comment.