Esempio n. 1
0
 /**
  * @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);
 }
Esempio n. 2
0
 public function testFromCharset()
 {
     $config = new LexerConfig();
     $this->assertSame(null, $config->getFromCharset());
     $this->assertSame('UTF-8', $config->setFromCharset('UTF-8')->getFromCharset());
 }
 /**
  * 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;
 }