Example #1
0
 /**
  * @todo When appending a file, there should be a way to detect if you need to prepend a newline char
  *       maybe check to see if the last char in the file is a newline char and if not, prepend one?
  */
 public function test_Writer_Accepts_Handle()
 {
     $content = "1,2,3\r\n4,5,6\r\n7,8,9\r\n";
     file_put_contents($this->file, $content);
     $file = fopen($this->file, 'ab');
     $writer = new Csv_Writer($file);
     $writer->writeRow(array(10, 11, 12));
     $this->assertEqual(file_get_contents($this->file), $content . "10,11,12" . $writer->getDialect()->lineterminator);
 }
 function testShouldWriteUTF8Text()
 {
     $writer = new Csv_Writer($this->file);
     $writer->writeRow(array('foo', 'ﺡ', 'bar'));
     $this->assertEquals('foo,ﺡ,bar' . "\r\n", file_get_contents($this->file));
 }
Example #3
0
 public function setUp()
 {
     $this->tmpfile = sys_get_temp_dir() . '/products.csv';
     $writer = new Csv_Writer($this->tmpfile, new Csv_Dialect(array('quoting' => Csv_Dialect::QUOTE_NONNUMERIC)));
     $writer->writeRows($this->data);
 }
<?php

require_once 'vendor/autoload.php';
use GetOptionKit\GetOptionKit;
$getopt = new GetOptionKit();
$getopt->add('n|number:=i', 'option requires a integer value');
try {
    $result = $getopt->parse($argv);
    $number = $result->number ? $result->number : 10;
} catch (Exception $e) {
    echo 'Try: create.php --number=10';
    exit;
}
$seedFile = 'seed.csv';
$reader = new Csv_Reader($seedFile, new Csv_Dialect());
$headerRow = $reader->getAssociativeRow();
$seedRow = $reader->getAssociativeRow();
$writer = new Csv_Writer(STDOUT, new Csv_Dialect(array('quoting' => Csv_Dialect::QUOTE_ALL)));
$writer->writeRow($headerRow);
for ($i = 1; $i <= $number; $i++) {
    $productRow = array_merge($seedRow, array('sku' => 'sku-' . $i, 'name' => 'product ' . $i));
    $writer->writeRow($productRow);
}
Example #5
0
 public function setUp()
 {
     $this->tmpfile = './data' . DIRECTORY_SEPARATOR . 'products.csv';
     $writer = new Csv_Writer($this->tmpfile, new Csv_Dialect(array('quoting' => Csv_Dialect::QUOTE_NONNUMERIC)));
     $writer->writeRows($this->data);
 }
/**
 * Export controller, generates CSV.
 * 
 * @param string $key
 */
function find_export_csv($key)
{
    $output = find_search($key);
    header('Content-Type: text/csv');
    header('Content-Disposition: attachment; filename="naturwerk-' . $output['search']['#key'] . '.csv"');
    $out = fopen('php://output', 'w');
    $writer = new Csv_Writer($out, new Csv_Dialect_Excel());
    $current = $output['search']['#current'];
    $result = $current->search();
    // build header row
    $data = array();
    foreach ($current->getActiveColumns() as $column) {
        $data[] = $column->getTitle();
    }
    $writer->writeRow($data);
    foreach ($result as $row) {
        // convert to ISO-8859-1 for Excel
        $data = array();
        foreach ($current->getActiveColumns() as $column) {
            $data[] = utf8_decode($row->__get($column->getName()));
        }
        $writer->writeRow($data);
    }
}