Coseva\CSV::getInstance PHP Method

getInstance() public static method

Get an instance of CSV, based on the filename.
public static getInstance ( string $filename, $open_mode = 'r', $use_include_path = false ) : CSV
$filename string the CSV file to read. Should be readable. Filenames will be resolved. Symlinks will be followed.
return CSV self::$_instances[$key]
    public static function getInstance($filename, $open_mode = 'r', $use_include_path = false)
    {
        // Create a combined key so different open modes can get their own instance.
        $key = $open_mode . ':' . $filename;
        // Check if an instance exists. If not, create one.
        if (!isset(self::$_instances[$key])) {
            // Collect the class name. This won't break when the class name changes.
            $class = __CLASS__;
            // Create a new instance of this class.
            self::$_instances[$key] = new $class($filename, $open_mode, $use_include_path);
        }
        return self::$_instances[$key];
    }

Usage Example

Beispiel #1
0
 * Example of a package file.
 *
 * To create the corresponding package, simply run:
 * ./package examples/example6.csv examples/example7-phar.php
 *
 * @package Coseva
 * @subpackage Examples
 */
// Coseva will be automatically included by the created package.
use Coseva\CSV;
$fromCurrency = 'EUR';
$toCurrency = 'USD';
// Fetch the current conversion from Google Finance.
$conversionRate = file_get_contents('http://www.google.com/finance/converter?' . http_build_query(array('a' => 1, 'from' => $fromCurrency, 'to' => $toCurrency)));
// Extract the conversion rate from the HTML.
$conversionRate = explode('<span class=bld>', $conversionRate, 2);
$conversionRate = explode('</span>', $conversionRate[1], 2);
$conversionRate = $conversionRate[0] + 0;
// Open the examples file with income for a week.
// SOURCE_FILE will be defined by the package bootstrap code.
$csv = CSV::getInstance(SOURCE_FILE);
// Filter the income.
$csv->filter(function (array $day, $conversionRate, $toCurrency) {
    // Convert the currency.
    $day[1] = number_format($day[1] * $conversionRate, 2);
    // Overwrite the currency unit.
    $day[2] = $toCurrency;
    return $day;
}, $conversionRate, $toCurrency);
// Output the converted income.
echo '<h1>Income in ' . $toCurrency . '</h1>' . $csv;
All Usage Examples Of Coseva\CSV::getInstance