2020-01-20 02:05:40 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Abstracts;
|
|
|
|
|
2020-03-25 20:21:42 +03:00
|
|
|
use App\Utilities\Date;
|
2020-01-20 02:05:40 +03:00
|
|
|
use Illuminate\Support\Str;
|
|
|
|
use Maatwebsite\Excel\Concerns\FromCollection;
|
|
|
|
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
|
|
|
|
use Maatwebsite\Excel\Concerns\WithHeadings;
|
|
|
|
use Maatwebsite\Excel\Concerns\WithMapping;
|
|
|
|
use Maatwebsite\Excel\Concerns\WithTitle;
|
2020-10-08 15:22:13 +03:00
|
|
|
use PhpOffice\PhpSpreadsheet\Shared\Date as ExcelDate;
|
2020-01-20 02:05:40 +03:00
|
|
|
|
|
|
|
abstract class Export implements FromCollection, ShouldAutoSize, WithHeadings, WithMapping, WithTitle
|
|
|
|
{
|
|
|
|
public $ids;
|
|
|
|
|
2020-12-24 01:28:38 +03:00
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $type;
|
|
|
|
|
|
|
|
public function __construct($ids = null, string $type = '')
|
2020-01-20 02:05:40 +03:00
|
|
|
{
|
|
|
|
$this->ids = $ids;
|
2020-12-24 01:28:38 +03:00
|
|
|
$this->type = $type;
|
2020-01-20 02:05:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function title(): string
|
|
|
|
{
|
|
|
|
return Str::snake((new \ReflectionClass($this))->getShortName());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function fields(): array
|
|
|
|
{
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function map($model): array
|
|
|
|
{
|
|
|
|
$map = [];
|
|
|
|
|
2020-11-13 03:07:30 +03:00
|
|
|
$date_fields = ['paid_at', 'invoiced_at', 'billed_at', 'due_at', 'issued_at', 'created_at', 'transferred_at'];
|
2020-01-20 02:05:40 +03:00
|
|
|
|
2020-04-24 23:28:43 +03:00
|
|
|
$evil_chars = ['=', '+', '-', '@'];
|
|
|
|
|
2020-01-20 02:05:40 +03:00
|
|
|
foreach ($this->fields() as $field) {
|
|
|
|
$value = $model->$field;
|
|
|
|
|
|
|
|
if (in_array($field, $date_fields)) {
|
2020-10-08 15:22:13 +03:00
|
|
|
$value = ExcelDate::PHPToExcel(Date::parse($value)->format('Y-m-d'));
|
2020-01-20 02:05:40 +03:00
|
|
|
}
|
|
|
|
|
2020-04-24 23:28:43 +03:00
|
|
|
// Prevent CSV injection https://security.stackexchange.com/a/190848
|
|
|
|
if (Str::startsWith($value, $evil_chars)) {
|
|
|
|
$value = "'" . $value;
|
|
|
|
}
|
|
|
|
|
2020-01-20 02:05:40 +03:00
|
|
|
$map[] = $value;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $map;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function headings(): array
|
|
|
|
{
|
|
|
|
return $this->fields();
|
|
|
|
}
|
|
|
|
}
|