/** * @param $path string absolute path to file * @param $closure \Closure * @void */ public function parse($path, Closure $closure) { $config = new LexerConfig(); $config->setDelimiter($this->delimeter); $config->setFromCharset($this->fromCharset); $config->setToCharset($this->toCharset); $lexer = new Lexer($config); $interpreter = new Interpreter(); $interpreter->addObserver($closure); $lexer->parse($path, $interpreter); }
/** * When import CSV file with data in Japanese (2 bytes character), * data imported to database with error encoding * @link https://github.com/goodby/csv/issues/5 */ public function test_issue_5() { $csvFilename = CSVFiles::getIssue5CSV(); $csvContents = array(); $config = new LexerConfig(); $config->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("ID", "NAME", "MAKER"), array("1", "スティック型クリーナ", "*****@*****.**"), array("2", "bob", "*****@*****.**"), array("14", "スティック型クリーナ", "*****@*****.**"), array("16", "スティック型", "*****@*****.**")), $csvContents); }
public function test_shift_jis_CSV() { $shiftJisCsv = CSVFiles::getShiftJisCsv(); $lines = array(array('あ', 'い', 'う', 'え', 'お'), array('日本語', '日本語', '日本語', '日本語', '日本語'), array('ぱ', 'ぴ', 'ぷ', 'ぺ', 'ぽ'), array('"quoted"', "a'quote'", 'a, b and c', '', '')); $interpreter = $this->getMock('Goodby\\CSV\\Import\\Standard\\Interpreter', array('interpret')); $interpreter->expects($this->at(0))->method('interpret')->with($lines[0]); $interpreter->expects($this->at(1))->method('interpret')->with($lines[1]); $interpreter->expects($this->at(2))->method('interpret')->with($lines[2]); $interpreter->expects($this->at(3))->method('interpret')->with($lines[3]); $config = new LexerConfig(); $config->setToCharset('UTF-8')->setFromCharset('SJIS-win'); $lexer = new Lexer($config); $lexer->parse($shiftJisCsv, $interpreter); }
public function testToCharset() { $config = new LexerConfig(); $this->assertSame(null, $config->getToCharset()); $this->assertSame('UTF-8', $config->setToCharset('UTF-8')->getToCharset()); }
/** * CSV parser * * @param string $file Path of the file. * @return array Returns the array from csv. * @since 0.1.0 */ public static function csv_parser($csv_file) { if (!is_file($csv_file)) { return new WP_Error('error', 'The CSV file is not found.'); } elseif (!self::is_textfile($csv_file)) { return new WP_Error('error', 'The file is not CSV.'); } $csv = array(); /** * Filter the CSV setting for the csv parser. * * @param array settings for the csv parser. */ $format = apply_filters('acsv_csv_format', array('from_charset' => 'UTF-8', 'to_charset' => 'UTF-8', 'delimiter' => ',', 'enclosure' => '"', 'escape' => '\\')); $config = new LexerConfig(); $config->setFromCharset($format['from_charset']); $config->setToCharset($format['to_charset']); $config->setDelimiter($format['delimiter']); $config->setEnclosure($format['enclosure']); $config->setEscape($format['escape']); $lexer = new Lexer($config); $interpreter = new Interpreter(); $interpreter->unstrict(); $interpreter->addObserver(function (array $row) use(&$csv) { $csv[] = $row; }); $lexer->parse($csv_file, $interpreter); return $csv; }