public function register(Application $app)
 {
     $app['csv.exporter.config'] = $app->share(function () {
         $config = new ExporterConfig();
         return $config->setDelimiter(";")->setEnclosure('"')->setEscape("\\")->setToCharset('UTF-8')->setFromCharset('UTF-8');
     });
     $app['csv.exporter'] = $app->share(function ($app) {
         return new Exporter($app['csv.exporter.config']);
     });
     $app['csv.lexer.config'] = $app->share(function ($app) {
         $lexer = new LexerConfig();
         $lexer->setDelimiter(';')->setEnclosure('"')->setEscape("\\")->setToCharset('UTF-8')->setFromCharset('UTF-8');
         return $lexer;
     });
     $app['csv.lexer'] = $app->share(function ($app) {
         return new Lexer($app['csv.lexer.config']);
     });
     $app['csv.interpreter'] = $app->share(function ($app) {
         return new Interpreter();
     });
     $app['csv.response'] = $app->protect(function ($callback) use($app) {
         // set headers to fix ie issues
         $response = new StreamedResponse($callback, 200, ['Expires' => 'Mon, 26 Jul 1997 05:00:00 GMT', 'Last-Modified' => gmdate('D, d M Y H:i:s') . ' GMT', 'Cache-Control' => 'no-store, no-cache, must-revalidate', 'Cache-Control' => 'post-check=0, pre-check=0', 'Pragma' => 'no-cache', 'Content-Type' => 'text/csv', 'Cache-Control' => 'max-age=3600, must-revalidate', 'Content-Disposition' => 'max-age=3600, must-revalidate']);
         $response->headers->set('Content-Disposition', $response->headers->makeDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT, 'export.csv'));
     });
 }
Example #2
0
 /**
  * @param array $config
  */
 public function __construct(array $config = array())
 {
     $this->config = new LexerConfig();
     $this->config->setDelimiter(array_get($config, 'delimiter', ";"))->setEnclosure(array_get($config, 'enclosure', "'"))->setEscape(array_get($config, 'escape', "\\"))->setToCharset(array_get($config, 'to_charset', 'UTF-8'))->setFromCharset(array_get($config, 'from_charset', 'UTF-8'));
     if (array_get($config, 'unstrict')) {
         $this->unstrict = true;
     }
 }
 /**
  * @param        $filePath
  * @param string $fromCharSet
  * @param string $toCharSet
  * @return array
  */
 public function import_csv($filePath, $fromCharSet = 'UTF-8', $toCharSet = 'UTF-8')
 {
     $config = new LexerConfig();
     $config->setDelimiter(';')->setEnclosure("'")->setEscape("\\")->setToCharset($toCharSet)->setFromCharset($fromCharSet);
     $lexer = new Lexer($config);
     $interpreter = new Interpreter();
     $data = array();
     $interpreter->addObserver(function (array $row) use(&$data) {
         $data[] = $row;
     });
     $lexer->parse($filePath, $interpreter);
     $attributes = array();
     $items = array();
     foreach ($data as $i => $row) {
         if (1 == $i) {
             $attributes = $row;
         }
         if ($i > 1) {
             $one = array();
             foreach ($attributes as $j => $attribute) {
                 if (array_key_exists($j, $row)) {
                     $one[$attribute] = $row[$j];
                 }
             }
             $items[] = $one;
         }
     }
     return $items;
 }
Example #4
0
 public function testDelimiter()
 {
     $config = new LexerConfig();
     $this->assertSame(',', $config->getDelimiter());
     $config->setDelimiter('del');
     $this->assertSame('del', $config->getDelimiter());
 }
 /**
  * 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);
 }
 public function csvInterpreter($file, \Closure $closure)
 {
     $config = new LexerConfig();
     $config->setDelimiter("\t")->setEnclosure("'")->setEscape("\\")->setToCharset('UTF-8')->setFromCharset('UTF-8');
     $lexer = new Lexer(new LexerConfig());
     $interpreter = new Interpreter();
     $interpreter->addObserver($closure);
     $lexer->parse($file, $interpreter);
 }
Example #7
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);
 }
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $this->file = $input->getArgument('file');
     $this->bag = array(self::BAG_UBICACION => array(), self::BAG_LUGAR => array(), self::BAG_PERSONA => array());
     $config = new CSV\LexerConfig();
     $config->setDelimiter(',')->setEnclosure('"')->setFromCharset('UTF-8')->setToCharset('UTF-8');
     $this->manager = $this->getContainer()->get('doctrine.orm.entity_manager');
     $this->lexer = new CSV\Lexer($config);
     $servicio = $this->manager->getRepository('SisesApplicationBundle:ServicioContratado')->find($input->getArgument('service'));
     $this->loadData($servicio);
 }
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $file = $input->getArgument('file');
     $config = new CSV\LexerConfig();
     $config->setDelimiter(',')->setEnclosure('"')->setFromCharset('UTF-8')->setToCharset('UTF-8');
     $this->manager = $this->getContainer()->get('doctrine.orm.entity_manager');
     $lexer = new CSV\Lexer($config);
     $interpreter = new CSV\Interpreter();
     $interpreter->addObserver($this->getObserver());
     $lexer->parse($file, $interpreter);
     $this->manager->flush();
 }
Example #10
0
 public function test_colon_separated_csv()
 {
     $csv = CSVFiles::getColonSeparatedCsv();
     $lines = CSVFiles::getColonSeparatedLines();
     $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]);
     $config = new LexerConfig();
     $config->setDelimiter(':');
     $lexer = new Lexer($config);
     $lexer->parse($csv, $interpreter);
 }
Example #11
0
 public static function import($file, $callback)
 {
     $config = new LexerConfig();
     $config->setDelimiter(";");
     $lexer = new Lexer($config);
     $interpreter = new Interpreter();
     $header = null;
     $observer = new CSVImporterLexerAssociativeObserver();
     $observer->setCallback($callback);
     $interpreter->addObserver($observer->observer);
     $lexer->parse($file->getRealPath(), $interpreter);
 }
Example #12
0
<?php

require_once __DIR__ . '/../vendor/autoload.php';
// load composer
use Goodby\CSV\Import\Standard\Lexer;
use Goodby\CSV\Import\Standard\Interpreter;
use Goodby\CSV\Import\Standard\LexerConfig;
// the result comes into this variable
$temperature = array();
// set up lexer
$config = new LexerConfig();
$config->setDelimiter("\t");
$config->setFlags(\SplFileObject::READ_AHEAD | \SplFileObject::SKIP_EMPTY | \SplFileObject::READ_CSV);
$lexer = new Lexer($config);
// set up interpreter
$interpreter = new Interpreter();
$interpreter->addObserver(function (array $row) use(&$temperature) {
    $temperature[] = array('temperature' => $row[0], 'city' => $row[1]);
});
// parse
$lexer->parse('temperature.tsv', $interpreter);
var_dump($temperature);
 /**
  * Extract data from de CSV and create new Location objects
  *
  * @param string $countryCode Country code
  * @param string $pathname    Pathname
  *
  * @return LocationInterface Root location
  */
 private function loadLocationsFrom($countryCode, $pathname)
 {
     $countryInfo = $this->getCountryInfo($countryCode);
     $country = $this->locationBuilder->addLocation($countryInfo[0], $countryInfo[1], $countryInfo[0], 'country');
     $interpreter = new Interpreter();
     $interpreter->unstrict();
     $nbItems = 0;
     $interpreter->addObserver(function (array $columns) use($country, &$nbItems) {
         if (isset($columns[1], $columns[2], $columns[3], $columns[4], $columns[5], $columns[6])) {
             $nbItems++;
             $stateId = $this->normalizeId($columns[4]);
             $state = $this->locationBuilder->addLocation($country->getId() . '_' . $stateId, $columns[3], $stateId, 'state', $country);
             $provinceId = $this->normalizeId($columns[6]);
             $province = $this->locationBuilder->addLocation($state->getId() . '_' . $provinceId, $columns[5], $provinceId, 'province', $state);
             $cityId = $this->normalizeId($columns[2]);
             $city = $this->locationBuilder->addLocation($province->getId() . '_' . $cityId, $columns[2], $cityId, 'city', $province);
             $postalCodeId = $this->normalizeId($columns[1]);
             $this->locationBuilder->addLocation($city->getId() . '_' . $postalCodeId, $columns[1], $postalCodeId, 'postalcode', $city);
         }
     });
     $config = new LexerConfig();
     $config->setDelimiter("\t");
     $lexer = new Lexer($config);
     $lexer->parse($pathname, $interpreter);
     return $country;
 }
 /**
  * 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;
 }
 public function loadBeneficio(ServicioContratado $servicio)
 {
     $file = sprintf(__DIR__ . '/../../Resources/files/%s.csv', $servicio->getNombre() == "Desayunos" ? '100' : '200');
     $config = new LexerConfig();
     $config->setDelimiter(',')->setEnclosure('"')->setFromCharset('UTF-8')->setToCharset('UTF-8');
     $lexer = new Lexer($config);
     $manager = $this->manager;
     $bag =& $this->bag;
     $repo = $this->manager->getRepository('SisesApplicationBundle:Ubicacion\\CentroPoblado');
     $findLugar = function ($columns) use($repo, &$bag, $manager) {
         $nombre = $columns[LoadBeneficiarios::COL_NOM_LUGAR];
         $codigoLugar = $columns[LoadBeneficiarios::COL_CODIGO_UBICACION];
         if (!isset($bag[LoadBeneficiarios::BAG_UBICACION][$nombre])) {
             if (!isset($bag[LoadBeneficiarios::BAG_LUGAR][$codigoLugar])) {
                 $bag[LoadBeneficiarios::BAG_LUGAR][$codigoLugar] = $repo->findOneBy(array('codigoDane' => "{$codigoLugar}000"));
             }
             $lugar = new LugarEntrega();
             $lugar->setUbicacion($bag[LoadBeneficiarios::BAG_LUGAR][$codigoLugar]);
             $lugar->setNombre($nombre);
             $manager->persist($lugar);
             $bag[LoadBeneficiarios::BAG_UBICACION][$nombre] = $lugar;
         }
         return $bag[LoadBeneficiarios::BAG_UBICACION][$nombre];
     };
     $repo2 = $this->manager->getRepository('SisesApplicationBundle:Persona');
     $findPersona = function ($column) use($repo2, &$bag, $manager) {
         $documento = $column[LoadBeneficiarios::COL_DOCUMENTO];
         // Busca la persona en la base d datos
         if (!isset($bag[LoadBeneficiarios::BAG_PERSONA][$documento])) {
             $bag[LoadBeneficiarios::BAG_PERSONA][$documento] = $repo2->findOneBy(array('documento' => $documento));
         }
         // Crea la persona
         if (!isset($bag[LoadBeneficiarios::BAG_PERSONA][$documento])) {
             $persona = new Persona();
             $persona->setNombre(trim("{$column[LoadBeneficiarios::COL_1_NOM]} {$column[LoadBeneficiarios::COL_2_NOM]}"));
             $persona->setApellidos(trim("{$column[LoadBeneficiarios::COL_1_APE]} {$column[LoadBeneficiarios::COL_2_APE]}"));
             $persona->setDocumento($documento);
             $manager->persist($persona);
             $bag[LoadBeneficiarios::BAG_PERSONA][$documento] = $persona;
         }
         return $bag[LoadBeneficiarios::BAG_PERSONA][$documento];
     };
     $interpreter = new Interpreter();
     $interpreter->addObserver(function (array $columns) use($bag, $findLugar, $findPersona, $servicio, $manager) {
         if ($columns[0] == "AÑO") {
             return;
         }
         $beneficiario = new Beneficiario();
         $beneficiario->setPersona($findPersona($columns));
         $beneficiario->setContrato($servicio->getContrato());
         $beneficio = new Beneficio();
         $beneficio->setBeneficiario($beneficiario);
         $beneficio->setLugar($findLugar($columns));
         $beneficio->setServicio($servicio);
         $manager->persist($beneficiario);
         $manager->persist($beneficio);
     });
     $lexer->parse($file, $interpreter);
     ksort($bag['ubicacion']);
     $manager->flush();
 }
Example #16
0
 /**
  * Lists all Person models.
  * @return mixed
  */
 public function actionTest()
 {
     $config = new LexerConfig();
     $config->setDelimiter("\t")->setEnclosure("'")->setEscape("\\")->setToCharset('UTF-8')->setFromCharset('SJIS-win');
 }
Example #17
-1
 /**
  * Populate a country
  *
  * @param OutputInterface $output                      The output interface
  * @param string          $countryCode                 Country Code
  * @param boolean         $sourcePackageMustbeReloaded Source package must be reloaded
  *
  * @return CountryInterface|null Country populated if created
  */
 public function populateCountry(OutputInterface $output, $countryCode, $sourcePackageMustbeReloaded)
 {
     $countryCode = strtoupper($countryCode);
     $file = $this->downloadRemoteFileFromCountryCode($output, $countryCode, $sourcePackageMustbeReloaded);
     $countryData = $this->getCountryArray()[$countryCode];
     $country = $this->geoBuilder->addCountry($countryData[0], $countryData[1]);
     $interpreter = new Interpreter();
     $interpreter->unstrict();
     $interpreter->addObserver(function (array $columns) use($country) {
         $state = $this->geoBuilder->addState($country, $columns[4], $columns[3]);
         $province = $this->geoBuilder->addProvince($state, $columns[6], $columns[5]);
         $city = $this->geoBuilder->addCity($province, $columns[2]);
         $this->geoBuilder->addPostalCode($city, $columns[1]);
     });
     $config = new LexerConfig();
     $config->setDelimiter("\t");
     $lexer = new Lexer($config);
     $started = new DateTime();
     $output->writeln('<header>[Geo]</header> <body>Starting file processing</body>');
     $lexer->parse($file->getRealpath(), $interpreter);
     $finished = new DateTime();
     $elapsed = $finished->diff($started);
     $output->writeln('<header>[Geo]</header> <body>File processed in ' . $elapsed->format('%s') . ' seconds</body>');
     return $country;
 }