Compare commits

..

No commits in common. "c7164be3230b78cbf98ef78ef7ba717c8b5b1578" and "80948d29440af9d34b1782798625c6a95daa8f29" have entirely different histories.

3 changed files with 5 additions and 50 deletions

View File

@ -8,26 +8,14 @@
use Maatwebsite\Excel\Concerns\FromQuery;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
use Maatwebsite\Excel\Concerns\WithColumnFormatting;
use Maatwebsite\Excel\Concerns\WithCustomValueBinder;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithMapping;
use PhpOffice\PhpSpreadsheet\Cell\Cell;
use PhpOffice\PhpSpreadsheet\Cell\DataType;
use PhpOffice\PhpSpreadsheet\Cell\DefaultValueBinder;
use PhpOffice\PhpSpreadsheet\Shared\Date as ExcelDate;
use PhpOffice\PhpSpreadsheet\Style\NumberFormat;
class SegmentContractsExport extends DefaultValueBinder implements FromQuery, ShouldAutoSize, WithColumnFormatting, WithCustomValueBinder, WithHeadings, WithMapping
class SegmentContractsExport implements FromQuery, ShouldAutoSize, WithColumnFormatting, WithHeadings, WithMapping
{
public const DATE_EXCEL_FORMAT = 'dd"."mm"."yyyy';
public const TEXT_EXCEL_FORMAT = NumberFormat::FORMAT_TEXT;
/**
* @var array<string, string>
*/
private array $columnLetterMap = [];
/**
* @var array<string, array{label: string}>
*/
@ -87,15 +75,9 @@ public function columnFormats(): array
{
$formats = [];
foreach ($this->getColumnLetterMap() as $letter => $column) {
if ($column === 'reference') {
$formats[$letter] = self::TEXT_EXCEL_FORMAT;
continue;
}
foreach ($this->columns as $index => $column) {
if (in_array($column, ['start_date', 'end_date'], true)) {
$formats[$letter] = self::DATE_EXCEL_FORMAT;
$formats[$this->columnLetter($index)] = self::DATE_EXCEL_FORMAT;
}
}
@ -140,31 +122,4 @@ private function columnLetter(int $index): string
return $letter;
}
public function bindValue(Cell $cell, $value): bool
{
$columnKey = $this->getColumnLetterMap()[$cell->getColumn()] ?? null;
if ($columnKey === 'reference') {
$cell->setValueExplicit((string) $value, DataType::TYPE_STRING);
return true;
}
return parent::bindValue($cell, $value);
}
/**
* @return array<string, string>
*/
private function getColumnLetterMap(): array
{
if ($this->columnLetterMap === []) {
foreach ($this->columns as $index => $column) {
$this->columnLetterMap[$this->columnLetter($index)] = $column;
}
}
return $this->columnLetterMap;
}
}

View File

@ -3,6 +3,7 @@
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="csrf-token" content="{{ csrf_token() }}">
<title inertia>{{ config('app.name', 'Laravel') }}</title>

View File

@ -141,7 +141,7 @@ public function test_export_filename_includes_client_name_when_filtered(): void
});
}
public function test_column_formats_apply_to_reference_and_date_columns(): void
public function test_column_formats_apply_to_date_columns(): void
{
$export = new SegmentContractsExport(
Contract::query(),
@ -150,7 +150,6 @@ public function test_column_formats_apply_to_reference_and_date_columns(): void
$this->assertSame(
[
'A' => SegmentContractsExport::TEXT_EXCEL_FORMAT,
'B' => SegmentContractsExport::DATE_EXCEL_FORMAT,
'D' => SegmentContractsExport::DATE_EXCEL_FORMAT,
],