Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

21 currency column formatting #22

Merged
merged 6 commits into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
56 changes: 56 additions & 0 deletions src/ColumnFormatter/CurrencyFormatter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

/**
* Copyright © Fastbolt Schraubengroßhandels GmbH.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

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;

private string $currency;

/**
* @param ColumnSetting $column
* @param 'EUR'|'USD' $currency
*/
public function __construct(ColumnSetting $column, string $currency)
{
$this->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"),
};
}
}
2 changes: 1 addition & 1 deletion src/ColumnFormatter/IntegerFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
35 changes: 17 additions & 18 deletions src/ColumnSetting.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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(),
};
}

/**
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['numberFormat']['formatCode'] = $formatCode;
}

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