Skip to content

Commit 8ccf158

Browse files
committed
fixed, added format code for currencies
1 parent 4c22fe9 commit 8ccf158

5 files changed

+48
-18
lines changed

src/ColumnFormatter/BaseFormatter.php

+5
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,9 @@ public function getNumberFormat(): ?array
2121
{
2222
return null;
2323
}
24+
25+
public function getFormatCode(): ?string
26+
{
27+
return null;
28+
}
2429
}

src/ColumnFormatter/ColumnFormatter.php

+3
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,7 @@ public function getAlignment(): array;
1515

1616
/** @return string[]|null */
1717
public function getNumberFormat(): ?array;
18+
19+
/** @return string|null */
20+
public function getFormatCode(): ?string;
1821
}

src/ColumnFormatter/CurrencyFormatter.php

+23-1
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,28 @@
33
namespace Fastbolt\ExcelWriter\ColumnFormatter;
44

55
use Fastbolt\ExcelWriter\ColumnSetting;
6+
use OutOfRangeException;
67
use PhpOffice\PhpSpreadsheet\Style\Alignment;
8+
use PhpOffice\PhpSpreadsheet\Style\NumberFormat;
79

810
class CurrencyFormatter extends BaseFormatter
911
{
12+
public const CURRENCY_EUR = "EUR";
13+
14+
public const CURRENCY_USD = "USD";
15+
1016
private int $decimalLength;
1117

12-
public function __construct(ColumnSetting $column)
18+
private string $currency;
19+
20+
/**
21+
* @param ColumnSetting $column
22+
* @param string{'EUR'|'USD'} $currency
23+
*/
24+
public function __construct(ColumnSetting $column, string $currency)
1325
{
1426
$this->decimalLength = $column->getDecimalLength();
27+
$this->currency = $currency;
1528
}
1629

1730
public function getAlignment(): array
@@ -25,4 +38,13 @@ public function getNumberFormat(): array
2538

2639
return ['formatCode' => $formatCode];
2740
}
41+
42+
public function getFormatCode(): string
43+
{
44+
return match ($this->currency) {
45+
self::CURRENCY_EUR => NumberFormat::FORMAT_CURRENCY_EUR,
46+
self::CURRENCY_USD => NumberFormat::FORMAT_CURRENCY_USD,
47+
default => throw new OutOfRangeException("Currency " . $this->currency . " is not supported"),
48+
};
49+
}
2850
}

src/ColumnSetting.php

+12-17
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ class ColumnSetting
2424
public const FORMAT_STRING = 'string';
2525
public const FORMAT_DATE = 'datetime';
2626
public const FOMRAT_PERCENTAGE = 'percentage';
27-
public const FORMAT_CURRENCY = 'currency';
27+
public const FORMAT_CURRENCY_EUR = 'currency_eur';
28+
public const FORMAT_CURRENCY_USD = 'currency_usd';
2829

2930
private string $format;
3031
private string $name = ''; //excel-name for the column
@@ -88,22 +89,16 @@ public function setName(string $name): ColumnSetting
8889
*/
8990
public function getFormatter(): ColumnFormatter
9091
{
91-
switch ($this->format) {
92-
case self::FORMAT_STRING:
93-
return new StringFormatter();
94-
case self::FORMAT_DATE:
95-
return new DateFormatter();
96-
case self::FORMAT_INTEGER:
97-
return new IntegerFormatter();
98-
case self::FORMAT_FLOAT:
99-
return new FloatFormatter($this);
100-
case self::FOMRAT_PERCENTAGE:
101-
return new PercentageFormatter($this);
102-
case self::FORMAT_CURRENCY:
103-
return new CurrencyFormatter($this);
104-
}
105-
106-
return new StringFormatter();
92+
return match ($this->format) {
93+
self::FORMAT_STRING => new StringFormatter(),
94+
self::FORMAT_DATE => new DateFormatter(),
95+
self::FORMAT_INTEGER => new IntegerFormatter(),
96+
self::FORMAT_FLOAT => new FloatFormatter($this),
97+
self::FOMRAT_PERCENTAGE => new PercentageFormatter($this),
98+
self::FORMAT_CURRENCY_EUR => new CurrencyFormatter($this, CurrencyFormatter::CURRENCY_EUR),
99+
self::FORMAT_CURRENCY_USD => new CurrencyFormatter($this, CurrencyFormatter::CURRENCY_USD),
100+
default => new StringFormatter(),
101+
};
107102
}
108103

109104
/**

src/ExcelGenerator.php

+5
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,11 @@ public function applyColumnFormat(): void
321321
$format['numberFormat'] = $numberFormat;
322322
}
323323

324+
//needed for currencies
325+
if (null !== ($formatCode = $formatter->getFormatCode())) {
326+
$format['formatCode'] = $formatCode;
327+
}
328+
324329
$sheet->getStyle($column->getName() . ':' . $column->getName())
325330
->applyFromArray($format);
326331
}

0 commit comments

Comments
 (0)