nozavroni / csvelte

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

Allow array anywhere you allow flavor #54

Closed deni-zen closed 7 years ago

deni-zen commented 8 years ago

I want to accept an associative array containing flavor attributes anywhere that I accept a flavor object. And then internally the array will be converted to a flavor. For instance...

<?php
$reader = CSVelte::reader('./files/data.csv', ['delimiter' => "\t", 'doubleQuotes' => false, 'escapeChar' => '\\']);

And then internally I would do this:

// ...

public static function reader($filename, $flavor)
{
    if (!($flavor instanceof Flavor)) {
        if (is_array($flavor)) $flavor = new Flavor($flavor);
    }
}

// ...
deni-zen commented 8 years ago

Actually in the Flavor class I should do something like this:

// ... 
public static function instance($flavor)
{
    if ($flavor instanceof Flavor) return $flavor;
    if (is_array($flavor)) return new Flavor($flavor);
    if ($flavor instanceof Iterator) {
        $flv = array();
        foreach ($flavor as $prop => $val) {
            $flv[$prop] = $val;
        }
        return new Flavor($flv);
    }
    // now throw exception cuz it cant do anything with $flavor
}

And then anywhere that expects a flavor could simply call $flavor = Flavor::instance($flavor);

nozavroni commented 7 years ago

Implemented and Scrutinized*