Exemplo n.º 1
0
 /**
  * Parses a csv file into a DataTable.
  *
  * Pass in a filepath to a csv file and an array of column types:
  * ['date', 'number', 'number', 'number'] for example and a DataTable
  * will be built.
  *
  * @access public
  * @since  1.0.0
  * @param  string $filepath    Path location to a csv file
  * @param  array  $columnTypes Array of column types to apply to the csv values
  * @throws \Khill\Lavacharts\Exceptions\InvalidFunctionParam
  * @return \Khill\Lavacharts\DataTable
  */
 public function parseCsvFile($filepath, $columnTypes = null)
 {
     if (Utils::nonEmptyString($filepath) === false) {
         throw new InvalidFunctionParam($filepath, __FUNCTION__, 'string');
     }
     $this->addNewColumns($columnTypes);
     $this->setReader(Reader::createFromPath($filepath));
     $this->reader->setFlags(\SplFileObject::READ_AHEAD | \SplFileObject::SKIP_EMPTY);
     $csvColumns = $this->reader->fetchOne();
     foreach ($this->newColumns as $index => $column) {
         if (in_array($column, $this->columnTypes, true) === false) {
             throw new InvalidColumnType($column, Utils::arrayToPipedString($this->columnTypes));
         }
         $this->addColumnFromStrings($columnTypes[$index], $csvColumns[$index]);
     }
     $csvRows = $this->reader->setOffset(1)->fetchAll(function ($row) {
         return array_map(function ($cell) {
             if (is_numeric($cell)) {
                 return $cell + 0;
             } else {
                 return $cell;
             }
         }, $row);
     });
     return $this->addRows($csvRows);
 }
Exemplo n.º 2
0
 /**
  * @param string $filePath  Path to the csv file to read from
  * @param string $delimiter The delimiter for the csv file
  * @param string $enclosure The enclosure for the csv file
  */
 public function __construct($filePath, $delimiter = ',', $enclosure = '"')
 {
     if (!is_file($filePath)) {
         throw new LogicException(sprintf('The file %s does not exist. \\Plum\\PlumCsv\\CsvReader needs an existing csv to work with', $filePath));
     }
     $this->csv = Reader::createFromPath($filePath);
     $this->csv->setFlags(\SplFileObject::READ_AHEAD | \SplFileObject::SKIP_EMPTY);
     $this->csv->setDelimiter($delimiter);
     $this->csv->setEnclosure($enclosure);
 }
Exemplo n.º 3
0
 public function testappendStreamFilter()
 {
     $csv = new Reader(__DIR__ . '/foo.csv');
     $csv->appendStreamFilter('string.toupper');
     $csv->setFlags(SplFileObject::READ_AHEAD | SplFileObject::SKIP_EMPTY);
     foreach ($csv->getIterator() as $row) {
         $this->assertSame($row, ['JOHN', 'DOE', '*****@*****.**']);
     }
 }