/**
  * 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);
 }
Exemplo n.º 2
0
 /**
  * use un-strict won't throw exception with inconsistent columns
  *
  */
 public function testInconsistentColumnsWithUnStrict()
 {
     $lines[] = array('test', 'test', 'test');
     $lines[] = array('test', 'test');
     $interpreter = new Interpreter();
     $interpreter->unstrict();
     foreach ($lines as $line) {
         $interpreter->interpret($line);
     }
 }
Exemplo n.º 3
0
 /**
  * @param $file
  * @param Closure|null $callback
  * @return array|null
  */
 public function import($file, Closure $callback = null)
 {
     $interpreter = new Interpreter();
     if ($this->unstrict) {
         $interpreter->unstrict();
     }
     if (is_null($callback)) {
         $results = array();
         $callback = function (array $row) use(&$results) {
             $results[] = $row;
         };
     } else {
         $results = null;
     }
     $interpreter->addObserver($callback);
     $lexer = new Lexer($this->config);
     $lexer->parse($file, $interpreter);
     return $results;
 }
 /**
  * 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;
 }
Exemplo n.º 5
0
 /**
  * @return Interpreter
  */
 protected function getInterpreter()
 {
     $interpreter = new Interpreter();
     $interpreter->unstrict();
     return $interpreter;
 }
Exemplo n.º 6
0
 /**
  * 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;
 }
Exemplo n.º 7
-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;
 }