* 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;
<?php /** * Table example * * @package Coseva * @subpackage Examples */ require_once '../src/Coseva/CSV.php'; use Coseva\CSV; // Get an instance of CSV. echo CSV::getInstance('example1.csv');
<?php /** * Example of using Coseva CSV with instances. * * @package Coseva * @subpackage Examples */ require_once '../src/Coseva/CSV.php'; use Coseva\CSV; // Get an instance of CSV. $csv = CSV::getInstance('example1.csv'); // Get a duplicate reference. $dupe = CSV::getInstance('example1.csv'); // Add a filter. $csv->filter(function (array $row) { // Cast each cell as an integer. foreach ($row as &$cell) { $cell += 0; } return $row; }); // Parse the CSV. $dupe->parse(); // Output the CSV as JSON. $json = $csv->toJSON(); echo $json; // [[1,2,3,4],[1,2,3,4]] // No matter in which scope you currently are, you can always fetch a previous // instance and use currently parsed data. This allows for data to be parsed // only once and to be used all over your code base.
<?php /** * Converting currency on-the-fly with Coseva. * * @package Coseva * @subpackage Examples */ require_once '../src/Coseva/CSV.php'; 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. $csv = CSV::getInstance('example6.csv'); // 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;
} return $row; }; // Get an instance of CSV. $csv = CSV::getInstance($testFile); // Set the flag to remove rows when empty. // Pro tip, this can actually speed up filters of the second iteration. // Especially on large data sets. // If Coseva finds your file to be large, it'll enable this by default. $csv->flushEmptyRows(true); // If you've got the memory to spare, disable garbage collection when using large // data sets, since garbage collection actually takes time. // If Coseva finds you have enough memory for it, it'll disable this by default. $csv->collectGarbage(false); // Get a duplicate reference. $dupe = CSV::getInstance($testFile); // Add a filter. $csv->filter($testFilter); // Parse the CSV for the first time. This should open the file and apply filters. $start = microtime(true); $csv->parse(); $end = microtime(true); echo 'Round #1: Parsing took ' . number_format(($end - $start) * 1000, 2) . ' ms' . PHP_EOL; // Add the filter again, since parse will flush the filters. $dupe->filter($testFilter); // Parse it again. We use $dupe for this, but it's the same instance as $csv. // It shouldn't have to parse the file again, but will iterate over each row. $start = microtime(true); $dupe->parse(); $end = microtime(true); echo 'Round #2: Parsing took ' . number_format(($end - $start) * 1000, 2) . ' ms' . PHP_EOL;