Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -72,18 +72,16 @@ public function readLines(string $separator = "\n", ?int $length = null) : \Gene
}

if (\substr_count($content, $separator) > 1) {
/**
* @phpstan-ignore-next-line
*/
$lines = \array_filter(\explode($separator, $content));
/** @phpstan-ignore argument.type */
$lines = \explode($separator, $content);

$lastIndex = \count($lines) - 1;

// Yield all lines except the last one
foreach (\array_slice($lines, 0, -1) as $line) {
yield $line;
for ($i = 0; $i < $lastIndex; $i++) {
yield $lines[$i];
}

// The last line is incomplete, so we need to keep it for the next iteration
$content = \end($lines);
$content = $lines[$lastIndex];
} elseif (\substr_count($content, $separator) === 1) {
// Split the content by the separator
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,18 @@ public function test_reading_lines_from_blob(int $lineLength) : void

$stream->close();
}

public function test_reading_lines_with_falsy_values_preserves_all_lines() : void
{
$content = "header\n0\n\nvalue\n0\nlast";
$this->givenFileExists(path('aws-s3://falsy.csv'), $content);

$stream = aws_s3_filesystem($this->bucket(), $this->s3Client())->readFrom(path('aws-s3://falsy.csv'));

$lines = \iterator_to_array($stream->readLines());

self::assertSame(['header', '0', '', 'value', '0', 'last'], $lines);

$stream->close();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,18 +74,16 @@ public function readLines(string $separator = "\n", ?int $length = null) : \Gene
}

if (\substr_count($content, $separator) > 1) {
/**
* @phpstan-ignore-next-line
*/
$lines = \array_filter(\explode($separator, $content));
/** @phpstan-ignore argument.type */
$lines = \explode($separator, $content);

$lastIndex = \count($lines) - 1;

// Yield all lines except the last one
foreach (\array_slice($lines, 0, -1) as $line) {
yield $line;
for ($i = 0; $i < $lastIndex; $i++) {
yield $lines[$i];
}

// The last line is incomplete, so we need to keep it for the next iteration
$content = \end($lines);
$content = $lines[$lastIndex];
} elseif (\substr_count($content, $separator) === 1) {
// Split the content by the separator
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,18 @@ public function test_reading_lines_from_blob(int $lineLength) : void

$stream->close();
}

public function test_reading_lines_with_falsy_values_preserves_all_lines() : void
{
$content = "header\n0\n\nvalue\n0\nlast";
$this->givenFileExists('flow-php', 'falsy.csv', $content);

$stream = azure_filesystem($this->blobService('flow-php'))->readFrom(path('azure-blob://falsy.csv'));

$lines = \iterator_to_array($stream->readLines());

self::assertSame(['header', '0', '', 'value', '0', 'last'], $lines);

$stream->close();
}
}
Loading