Skip to content

Commit

Permalink
fixed, added format code for currencies
Browse files Browse the repository at this point in the history
  • Loading branch information
StvL97 committed Oct 2, 2024
1 parent 4c22fe9 commit 8ccf158
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 18 deletions.
5 changes: 5 additions & 0 deletions src/ColumnFormatter/BaseFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,9 @@ public function getNumberFormat(): ?array
{
return null;
}

public function getFormatCode(): ?string
{
return null;
}
}
3 changes: 3 additions & 0 deletions src/ColumnFormatter/ColumnFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,7 @@ public function getAlignment(): array;

/** @return string[]|null */
public function getNumberFormat(): ?array;

/** @return string|null */
public function getFormatCode(): ?string;
}
24 changes: 23 additions & 1 deletion src/ColumnFormatter/CurrencyFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,28 @@
namespace Fastbolt\ExcelWriter\ColumnFormatter;

use Fastbolt\ExcelWriter\ColumnSetting;
use OutOfRangeException;
use PhpOffice\PhpSpreadsheet\Style\Alignment;
use PhpOffice\PhpSpreadsheet\Style\NumberFormat;

class CurrencyFormatter extends BaseFormatter
{
public const CURRENCY_EUR = "EUR";

public const CURRENCY_USD = "USD";

private int $decimalLength;

public function __construct(ColumnSetting $column)
private string $currency;

/**
* @param ColumnSetting $column
* @param string{'EUR'|'USD'} $currency

Check failure on line 22 in src/ColumnFormatter/CurrencyFormatter.php

View workflow job for this annotation

GitHub Actions / Psalm

InvalidDocblock

src/ColumnFormatter/CurrencyFormatter.php:22:15: InvalidDocblock: Unexpected brace character in docblock for Fastbolt\ExcelWriter\ColumnFormatter\CurrencyFormatter::__construct (see https://psalm.dev/008)
*/
public function __construct(ColumnSetting $column, string $currency)
{
$this->decimalLength = $column->getDecimalLength();
$this->currency = $currency;
}

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

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"),
};
}
}
29 changes: 12 additions & 17 deletions src/ColumnSetting.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ class ColumnSetting
public const FORMAT_STRING = 'string';
public const FORMAT_DATE = 'datetime';
public const FOMRAT_PERCENTAGE = 'percentage';
public const FORMAT_CURRENCY = 'currency';
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
Expand Down Expand Up @@ -88,22 +89,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);
case self::FORMAT_CURRENCY:
return new CurrencyFormatter($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(),
};
}

/**
Expand Down
5 changes: 5 additions & 0 deletions src/ExcelGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,11 @@ public function applyColumnFormat(): void
$format['numberFormat'] = $numberFormat;
}

//needed for currencies
if (null !== ($formatCode = $formatter->getFormatCode())) {
$format['formatCode'] = $formatCode;
}

$sheet->getStyle($column->getName() . ':' . $column->getName())
->applyFromArray($format);
}
Expand Down

0 comments on commit 8ccf158

Please sign in to comment.