christophehurpeau/php-importer

This package is abandoned and no longer maintained. No replacement package was suggested.

Interfaces and CSV Importer in PHP

1.0.0 2015-05-13 14:45 UTC

This package is not auto-updated.

Last update: 2020-08-22 08:39:03 UTC


README

Build Status Scrutinizer Quality Score Code Climate Test Coverage

php-importer

Import and process files

Example:

namespace CountriesExample;

class CountriesCsvProcessor implements \Importer\HeaderValidator, \Importer\LineProcessor
{
  const HEADER_COUNTRY_NAME = 'country_name';

  /**
   * @return array|true
   */
  public function processFile($file) {

      $engine = new \Importer\Csv\Engine;
      $parser = new \Importer\Csv\Parser($file);
      return $engine->process($parser, $this, $this);
  }

  /**
     * @return array
     */
    public function getRequiredHeaders()
    {
        return array( self::HEADER_COUNTRY_NAME );
    }

    /**
     * @param array $line
     */
    public function processLine(array $line)
    {
        $countryName = $line[self::HEADER_COUNTRY];
        if (empty($countryName)) {
            return 'Country name  for country' . $countryName . 'is empty for line '.print_r($line, true);
        }
        echo $countryName . "\n";//do something
        return true; // everything went well
    }
}

How to use

ini_set('auto_detect_line_endings', true);
$countriesCsvProcessor = new CountriesCsvProcessor();
$result = $dataCountriesCsvProcessor->processFile(__DIR__ . '/../data/countries.csv');
if ($result !== true) {
    throw new \Exception('Failed lines: '. print_r($result, true));
}