Esempio n. 1
0
 /** Remove the header, and write the input with predefined column order. */
 function prepareTempFile($csvText)
 {
     $this->inputFile = sys_get_temp_dir() . '/' . uniqid();
     $inputHandle = fopen($this->inputFile, 'w');
     fwrite($inputHandle, $csvText);
     fclose($inputHandle);
     $this->categoriesFile = sys_get_temp_dir() . '/' . uniqid();
     $this->categoriesHandle = fopen($this->categoriesFile, 'w');
     $inputReader = new \Csv_Reader($this->inputFile, new \Csv_Dialect());
     $i = 0;
     while ($row = $inputReader->getAssociativeRow()) {
         $i++;
         // skip the header
         if ($i == 1) {
             continue;
         }
         fputcsv($this->categoriesHandle, array('id' => isset($row['id']) ? $row['id'] : 0, 'path' => isset($row['path']) ? $row['path'] : '', 'name' => $row['name']));
     }
 }
Esempio n. 2
0
 /** Necessary pre-processing to the import rows, like removing the header, exploding multi-valued strings, etc. */
 function preProcessRows()
 {
     $inputReader = new \Csv_Reader($this->inputFile, new \Csv_Dialect());
     $i = 0;
     while ($row = $inputReader->getAssociativeRow()) {
         $i++;
         // skip the header
         if ($i == 1) {
             continue;
         }
         fputcsv($this->productHandle, array('sku' => $row['sku'], 'name' => $row['name'], 'active' => isset($row['active']) ? $row['active'] : '', 'base_price' => isset($row['base_price']) ? $row['base_price'] : '', 'attributes' => isset($row['attributes']) ? $row['attributes'] : ''));
         $categories = $this->explodeCategories($row);
         foreach ($categories as $category) {
             fputcsv($this->categoriesHandle, array('product_sku' => $row['sku'], 'category_id' => $category, 'category_name' => ''));
         }
         $categoryNames = $this->explodeCategoryNames($row);
         foreach ($categoryNames as $category) {
             fputcsv($this->categoriesHandle, array('product_sku' => $row['sku'], 'category_id' => '', 'category_name' => $category));
         }
     }
 }
Esempio n. 3
0
 function testShouldThrowExceptionForNotEnoughFields()
 {
     $dialect = new \Csv_Dialect();
     $data = "foo,bar,baz,bat\n";
     $data .= "test";
     $file = sys_get_temp_dir() . '/extra.csv';
     file_put_contents($file, $data);
     $reader = new Csv_Reader($file, $dialect);
     $header = $reader->getAssociativeRow();
     $this->setExpectedException('Exception', 'Line [2] has [1] fields, but header has [4]');
     $row = $reader->getAssociativeRow();
 }
 function testShouldHandleEOFAssociativeRow()
 {
     $reader = new Csv_Reader($this->files['tab-200']);
     $reader->setPosition(201);
     $this->assertFalse($reader->getAssociativeRow(), 'should handle EOF w/ associative rows');
 }
<?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);
}