Example #1
0
 /**
  * @throws ParserException
  *
  * @return array $rows
  */
 public function load()
 {
     $file = new File($this->getConfig());
     $content = $file->open();
     $rows = [];
     $afterHeader = true;
     $this->step = 0;
     while (($line = $file->getLine($content)) !== false) {
         if ($afterHeader) {
             if (count($rows) < 1) {
                 // prepare head of file with column names
                 $this->setHead($line);
             }
             if (count($line) != count($this->head)) {
                 $this->checkColumnsCount($line);
             }
             $array = [];
             foreach ($line as $key => $value) {
                 $this->checkRequiredColumn($key, $value);
                 // check column content format
                 $this->checkFormat($key, $value);
                 // get canonical column name
                 $columnName = $this->getColumnName($key);
                 if ($this->getConfig()->removeId && $columnName == 'id') {
                     continue;
                 }
                 $array[(string) $columnName] = $value;
             }
             $rows[] = $array;
             $this->step++;
         }
         if ($this->getConfig()->stopOnEmpty) {
             // skip file until empty line
             if ($line[0] == null) {
                 $afterHeader = false;
             }
         }
     }
     $file->close();
     if ($this->getConfig()->skipHead && !$this->getConfig()->noHead) {
         unset($rows[0]);
     }
     return $rows;
 }