/** * @param $rows * @return $this * @throws \RuntimeException */ public function setData($rows) { if (empty($this->format)) { throw new \RuntimeException('First use setOptions!'); } if (empty($this->columns)) { throw new \RuntimeException('First use setColumns to set columns to export!'); } $accessor = PropertyAccess::getPropertyAccessor(); $separator = $this->separator; $escape = $this->escape; $hooks = $this->hooks; $format = $this->format; foreach ($rows as $row) { switch ($this->format) { case 'csv': case 'json': $tempRow = array(); break; case 'xls': case 'html': case 'xml': $tempRow = ''; break; } $tempRow = array_map(function ($column) use($row, $accessor, $separator, $escape, $hooks, $format) { return DataExporter::escape($accessor->getValue($row, $column), $separator, $escape, $column, $hooks, $format); }, $this->columns); switch ($this->format) { case 'csv': $this->data[] = implode($this->separator, $tempRow); break; case 'json': $this->data[] = array_combine($this->data[0], $tempRow); break; case 'xls': case 'html': $this->data .= '<tr>'; foreach ($tempRow as $val) { $this->data .= '<td>' . $val . '</td>'; } $this->data .= '</tr>'; break; case 'xml': $this->data .= '<row>'; $i = 0; foreach ($tempRow as $val) { $this->data .= '<column name="' . $this->columns[$i] . '">' . $val . '</column>'; $i++; } $this->data .= '</row>'; break; } } return $this; }