/**
  * Get a new CSVParser using defined settings.
  * @return Iterator
  */
 public function getIterator()
 {
     if (!file_exists($this->filepath)) {
         //TODO: throw exception instead?
         return null;
     }
     $header = $this->hasheader ? $this->getFirstRow() : null;
     $output = array();
     $config = new LexerConfig();
     $config->setDelimiter($this->delimiter);
     $config->setEnclosure($this->enclosure);
     $config->setIgnoreHeaderLine($this->hasheader);
     $interpreter = new Interpreter();
     // Ignore row column count consistency
     $interpreter->unstrict();
     $interpreter->addObserver(function (array $row) use(&$output, $header) {
         if ($header) {
             //create new row using headings as keys
             $newrow = array();
             foreach ($header as $k => $heading) {
                 if (isset($row[$k])) {
                     $newrow[$heading] = $row[$k];
                 }
             }
             $row = $newrow;
         }
         $output[] = $row;
     });
     $lexer = new Lexer($config);
     $lexer->parse($this->filepath, $interpreter);
     return new ArrayIterator($output);
 }
Ejemplo n.º 2
0
 public function test_ignore_header()
 {
     $csvFilename = CSVFiles::getIssue5CSV();
     $config = new LexerConfig();
     $config->setIgnoreHeaderLine(true)->setToCharset('UTF-8')->setFromCharset('UTF-8');
     $lexer = new Lexer($config);
     $interpreter = new Interpreter();
     $interpreter->addObserver(function (array $columns) use(&$csvContents) {
         $csvContents[] = $columns;
     });
     $lexer->parse($csvFilename, $interpreter);
     $this->assertSame(array(array("1", "スティック型クリーナ", "*****@*****.**"), array("2", "bob", "*****@*****.**"), array("14", "スティック型クリーナ", "*****@*****.**"), array("16", "スティック型", "*****@*****.**")), $csvContents);
 }
Ejemplo n.º 3
0
 $display = "<table>";
 $line_number = $prev_level = 0;
 $account = $text = "";
 $continue_on_next_line = false;
 // Open file
 if ($fp = fopen($file, "r")) {
     // data from CSV
     $valuesToImport = array();
     // load libraries
     require_once $_SESSION['settings']['cpassman_dir'] . '/includes/libraries/Goodby/CSV/Import/Standard/Lexer.php';
     require_once $_SESSION['settings']['cpassman_dir'] . '/includes/libraries/Goodby/CSV/Import/Standard/Interpreter.php';
     require_once $_SESSION['settings']['cpassman_dir'] . '/includes/libraries/Goodby/CSV/Import/Standard/LexerConfig.php';
     // Lexer configuration
     $config = new LexerConfig();
     $lexer = new Lexer($config);
     $config->setIgnoreHeaderLine("true");
     // extract data from CSV file
     $interpreter = new Interpreter();
     $interpreter->addObserver(function (array $row) use(&$valuesToImport) {
         $valuesToImport[] = array('Label' => $row[0], 'Login' => $row[1], 'Password' => $row[2], 'url' => $row[3], 'Comments' => $row[4]);
     });
     $lexer->parse($file, $interpreter);
     // extract one line
     foreach ($valuesToImport as $key => $row) {
         //Check number of fields. MUST be 5. if not stop importation
         if (count($row) != 5) {
             $importation_possible = false;
             //Stop if file has not expected structure
             if ($importation_possible == false) {
                 echo '[{"error":"bad_structure"}]';
                 break;
Ejemplo n.º 4
0
 public function testIgnoreHeaderLine()
 {
     $config = new LexerConfig();
     $this->assertSame(false, $config->getIgnoreHeaderLine());
     $this->assertSame(true, $config->setIgnoreHeaderLine(true)->getIgnoreHeaderLine());
 }