Skip to content

Instantly share code, notes, and snippets.

@donquixote
Created September 2, 2016 18:52
Show Gist options
  • Save donquixote/5c2ab89a60f96b89c205ab2bd65bb117 to your computer and use it in GitHub Desktop.
Save donquixote/5c2ab89a60f96b89c205ab2bd65bb117 to your computer and use it in GitHub Desktop.
<?php
// A "stream" represents a CSV file or another data source.
// "streams" exist on different levels. And a higher-level stream can use a lower-level stream as a data source.
// Problem: Find better nomenclature.
// Currently: RowStream, (AssocRowStream), AssocStream, ObjectStream.
assert($rowStream instanceof RowStreamInterface);
$rows = [];
while (false !== $row = $rowStream->fetchRow()) {
assert(is_array($row));
// Keys are column indices, so they are integers, properly ordered.
assert(array_values($row) === $row);
foreach ($row as $iColumn => $cell) {
// Values are string cell contents.
assert(is_string($cell));
}
$rows[] = $row;
}
assert($assocStream insteancof AssocStreamInterface);
$entries = [];
while (false !== $entry = $assocStream->fetchAssoc()) {
assert(is_array($row));
// Keys are column names, so they are generally strings.
# assert(array_values($row) === $row);
foreach ($entry as $colName => $cellValue) {
// $cellValue can have any type whatsoever.
# assert(is_string($cellValue));
}
$entries[] = $entry;
}
// Between these two:
assert($assocRowStream insteancof AssocRowStreamInterface);
$entries = [];
while (false !== $entry = $assocRowStream->fetchAssocRow()) {
assert(is_array($row));
// Keys are column names, so they are generally strings.
# assert(array_values($entry) === $entry);
foreach ($entry as $colName => $cell) {
// Values are string cell contents.
assert(is_string($cell));
}
$entries[] = $entry;
}
assert($objectStream instanceof ObjectStreamInterface);
$objects = [];
while (false !== $object = $objectStream->fetchObject()) {
assert(is_object($object));
$objects[] = $object;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment