Skip to content

Commit

Permalink
Merge pull request #4 from RonasIT/dpankratov/excel-update
Browse files Browse the repository at this point in the history
Dpankratov/excel update
  • Loading branch information
DenTray authored Apr 24, 2019
2 parents 9189952 + baf170e commit 3830091
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 59 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
}
],
"require": {
"php": ">=5.6",
"php": ">=7.0",
"laravel/framework": ">=5.2.0",
"guzzlehttp/guzzle": "~6.0",
"tymon/jwt-auth": "0.5.* || 1.0.*",
Expand Down
98 changes: 40 additions & 58 deletions src/Exporters/Exporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,98 +2,80 @@

namespace RonasIT\Support\Exporters;

use Maatwebsite\Excel\Facades\Excel;
use RonasIT\Support\Iterators\DBIterator;

/**
* @property DBIterator $iterator
*/
class Exporter
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Storage;
use Maatwebsite\Excel\Concerns\FromQuery;
use Maatwebsite\Excel\Concerns\Exportable;
use Maatwebsite\Excel\Concerns\WithMapping;
use Maatwebsite\Excel\Concerns\WithHeadings;
use RonasIT\Support\Interfaces\ExporterInterface;

abstract class Exporter implements FromQuery, WithHeadings, WithMapping, ExporterInterface
{
protected $iterator;
use Exportable;

protected $query;
protected $fileName;
protected $filters;
protected $type = 'csv';
protected $fields = [];

public function setQuery($query)
public function setQuery($query): self
{
$this->iterator = new DBIterator($query);
$this->query = $query;

return $this;
}

public function setFileName($fileName)
public function query()
{
$this->fileName = $fileName;

return $this;
return $this->query;
}

public function setFilters($filters)
public function setFileName($fileName): self
{
$this->filters = array_except($filters, ['token', 'export_type']);
$this->fileName = $fileName;

return $this;
}

public function setType($type)
/**
* @param $type string should be one of presented here https://docs.laravel-excel.com/3.0/exports/export-formats.html
*
* @return $this
*/
public function setType($type): self
{
$this->type = $type;

return $this;
}

public function export()
public function export(): string
{
$info = Excel::create($this->getFileName(), function ($excel) {
$excel->sheet('export', function ($sheet) {
$this->exportFilters($sheet);
$filename = $this->getFileName();

$sheet->appendRow($this->fields);
$this->store($filename, null, ucfirst($this->type));

foreach ($this->iterator->getGenerator() as $line) {
$sheet->appendRow($this->getLine($line));
}
});
})->store($this->type, false, true);

return $info['full'];
return Storage::path($filename);
}

private function getFileName()
public function headings(): array
{
return $this->fileName ?? uniqid();
return is_associative($this->getFields()) ? array_keys($this->getFields()) : $this->getFields();
}

public function getLine($item)
public function map($row): array
{
return array_map(function ($field) use ($item) {
$value = array_get($item, $field);

if (is_array($value)) {
return json_encode($item[$field]);
}

return $value;
}, $this->fields);
return array_map(function ($fieldName) use ($row) {
return Arr::get($row, $fieldName);
}, $this->getFields());
}

protected function exportFilters($sheet)
{
$sheet->appendRow(['Filters:']);

foreach ($this->filters as $key => $value) {
$line = [$key];
abstract public function getFields(): array;

if (is_array($value)) {
$line = array_merge($line, $value);
} else {
$line[] = $value;
}
$sheet->appendRow($line);
}
protected function getFileName(): string
{
$this->fileName = empty($this->fileName) ? uniqid() : $this->fileName;

$sheet->appendRow(['']);
return $this->fileName . '.' . $this->type;
}
}
}
32 changes: 32 additions & 0 deletions src/Interfaces/ExporterInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace RonasIT\Support\Interfaces;

interface ExporterInterface
{
/**
* Set fields to export
*
* Use associative array to use keys as headings
*
* @return array
*/
public function getFields();

/**
* Set name of exported file
*
* @param $fileName string
*/
public function setFileName($fileName);

/**
* Set exporting format
*
* @param $type string should be one of presented here https://docs.laravel-excel.com/3.0/exports/export-formats.html
* @return $this
*/
public function setType($type);
}


0 comments on commit 3830091

Please sign in to comment.