nozavroni / csvelte

🕺🏻 CSV and Tabular Data library for PHP
http://phpcsv.com/
Other
6 stars 0 forks source link

Write CSVelte\Schema class and implement it #16

Open deni-zen opened 8 years ago

deni-zen commented 8 years ago

Schema is the word I've been looking for. There are all sorts of "flavors" of CSV data. They can contain this delimiter or that, double or single quotes, doublequote escaping, quoting of ALL or just the bare MINIMUM columns, etc. All of these little formatting properties belong in the "Flavor" class. But whether or not a file has a header, to me, never seemed to fit into that category (format). And that's because it isn't a part of the format. It's a part of the schema.

My Taster class can usually tell whether or not a particular file has a header row, but if you wanted to just provide this information to a reader/writer and be sure you adhere to a particular schema, you can do something like this:

$input = new Input\Stream('file:///var/www/foo.csv');
$flavor = new Flavor([
    'quoteChar' => '"',
    'quoteStyle' => Flavor::QUOTE_MINIMAL,
    'escapeChar' => '\\'
]);
$schema = new Schema([
    'header' => true,
    'columns' => [
        'id' => 'int(10) unsigned',
        'name' => 'string(25)',
        'email' => 'string(255)',
        'street' => string(50)',
        'is_active' => 'boolean',
        'is_accurate' => 'boolean',
        'created_at' => 'datetime(mm-dd-yyyy hh:mm:ss)',
        'updated_at' => 'datetime(mm-dd-yyyy hh:mm:ss)',
    ]
]);
try {
   $reader = new Reader($input, $flavor, $schema);
} catch (MalformedDataException $e) {
    // data within $input does not conform to $schema
}
// or if you leave out flavor and schema... 
$reader = new Reader($input);
// it will do its best to figure these out on its own 
$schema = $reader->getSchema();
$flavor->getFlavor();

These guys are doing some interesting things with CSV... maybe in the distant future I'll implement some of this...

deni-zen commented 8 years ago

Don't forget about these functions (I always do) when writing this functionality (schema validation and the like) http://php.net/manual/en/book.ctype.php