diff --git a/src/ColumnFormatter/BaseFormatter.php b/src/ColumnFormatter/BaseFormatter.php index 72ac131..b5b82e3 100644 --- a/src/ColumnFormatter/BaseFormatter.php +++ b/src/ColumnFormatter/BaseFormatter.php @@ -21,4 +21,9 @@ public function getNumberFormat(): ?array { return null; } + + public function getFormatCode(): ?string + { + return null; + } } diff --git a/src/ColumnFormatter/ColumnFormatter.php b/src/ColumnFormatter/ColumnFormatter.php index b9e6ebd..6bf4f79 100644 --- a/src/ColumnFormatter/ColumnFormatter.php +++ b/src/ColumnFormatter/ColumnFormatter.php @@ -15,4 +15,7 @@ public function getAlignment(): array; /** @return string[]|null */ public function getNumberFormat(): ?array; + + /** @return string|null */ + public function getFormatCode(): ?string; } diff --git a/src/ColumnFormatter/CurrencyFormatter.php b/src/ColumnFormatter/CurrencyFormatter.php new file mode 100644 index 0000000..eb7119b --- /dev/null +++ b/src/ColumnFormatter/CurrencyFormatter.php @@ -0,0 +1,56 @@ +decimalLength = $column->getDecimalLength(); + $this->currency = $currency; + } + + public function getAlignment(): array + { + return ['horizontal' => Alignment::HORIZONTAL_RIGHT]; + } + + public function getNumberFormat(): array + { + $formatCode = '0.' . str_repeat('0', $this->decimalLength); + + return ['formatCode' => $formatCode]; + } + + public function getFormatCode(): string + { + return match ($this->currency) { + self::CURRENCY_EUR => NumberFormat::FORMAT_CURRENCY_EUR, + self::CURRENCY_USD => NumberFormat::FORMAT_CURRENCY_USD, + default => throw new OutOfRangeException("Currency " . $this->currency . " is not supported"), + }; + } +} diff --git a/src/ColumnFormatter/IntegerFormatter.php b/src/ColumnFormatter/IntegerFormatter.php index 809ed4a..97a1049 100644 --- a/src/ColumnFormatter/IntegerFormatter.php +++ b/src/ColumnFormatter/IntegerFormatter.php @@ -11,7 +11,7 @@ use PhpOffice\PhpSpreadsheet\Style\Alignment; use PhpOffice\PhpSpreadsheet\Style\NumberFormat; -class IntegerFormatter implements ColumnFormatter +class IntegerFormatter extends BaseFormatter { public function getAlignment(): array { diff --git a/src/ColumnSetting.php b/src/ColumnSetting.php index f320f2c..f5a1779 100644 --- a/src/ColumnSetting.php +++ b/src/ColumnSetting.php @@ -9,6 +9,7 @@ namespace Fastbolt\ExcelWriter; use Fastbolt\ExcelWriter\ColumnFormatter\ColumnFormatter; +use Fastbolt\ExcelWriter\ColumnFormatter\CurrencyFormatter; use Fastbolt\ExcelWriter\ColumnFormatter\DateFormatter; use Fastbolt\ExcelWriter\ColumnFormatter\FloatFormatter; use Fastbolt\ExcelWriter\ColumnFormatter\IntegerFormatter; @@ -17,11 +18,13 @@ class ColumnSetting { - public const FORMAT_INTEGER = 'int'; - public const FORMAT_FLOAT = 'float'; - public const FORMAT_STRING = 'string'; - public const FORMAT_DATE = 'datetime'; + public const FORMAT_INTEGER = 'int'; + public const FORMAT_FLOAT = 'float'; + public const FORMAT_STRING = 'string'; + public const FORMAT_DATE = 'datetime'; public const FOMRAT_PERCENTAGE = 'percentage'; + public const FORMAT_CURRENCY_EUR = 'currency_eur'; + public const FORMAT_CURRENCY_USD = 'currency_usd'; private string $format; private string $name = ''; //excel-name for the column @@ -85,20 +88,16 @@ public function setName(string $name): ColumnSetting */ public function getFormatter(): ColumnFormatter { - switch ($this->format) { - case self::FORMAT_STRING: - return new StringFormatter(); - case self::FORMAT_DATE: - return new DateFormatter(); - case self::FORMAT_INTEGER: - return new IntegerFormatter(); - case self::FORMAT_FLOAT: - return new FloatFormatter($this); - case self::FOMRAT_PERCENTAGE: - return new PercentageFormatter($this); - } - - return new StringFormatter(); + return match ($this->format) { + self::FORMAT_STRING => new StringFormatter(), + self::FORMAT_DATE => new DateFormatter(), + self::FORMAT_INTEGER => new IntegerFormatter(), + self::FORMAT_FLOAT => new FloatFormatter($this), + self::FOMRAT_PERCENTAGE => new PercentageFormatter($this), + self::FORMAT_CURRENCY_EUR => new CurrencyFormatter($this, CurrencyFormatter::CURRENCY_EUR), + self::FORMAT_CURRENCY_USD => new CurrencyFormatter($this, CurrencyFormatter::CURRENCY_USD), + default => new StringFormatter(), + }; } /** diff --git a/src/ExcelGenerator.php b/src/ExcelGenerator.php index c8d9659..51b930c 100644 --- a/src/ExcelGenerator.php +++ b/src/ExcelGenerator.php @@ -321,6 +321,11 @@ public function applyColumnFormat(): void $format['numberFormat'] = $numberFormat; } + //needed for currencies + if (null !== ($formatCode = $formatter->getFormatCode())) { + $format['numberFormat']['formatCode'] = $formatCode; + } + $sheet->getStyle($column->getName() . ':' . $column->getName()) ->applyFromArray($format); }