Skip to content

Commit 5197202

Browse files
authored
Merge pull request #22 from fastbolt/21-currency-column-formatting
21 currency column formatting
2 parents 3ae6eb1 + f0b76fd commit 5197202

6 files changed

+87
-19
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
}
+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
/**
4+
* Copyright © Fastbolt Schraubengroßhandels GmbH.
5+
* For the full copyright and license information, please view the LICENSE
6+
* file that was distributed with this source code.
7+
*/
8+
9+
namespace Fastbolt\ExcelWriter\ColumnFormatter;
10+
11+
use Fastbolt\ExcelWriter\ColumnSetting;
12+
use OutOfRangeException;
13+
use PhpOffice\PhpSpreadsheet\Style\Alignment;
14+
use PhpOffice\PhpSpreadsheet\Style\NumberFormat;
15+
16+
class CurrencyFormatter extends BaseFormatter
17+
{
18+
public const CURRENCY_EUR = "EUR";
19+
20+
public const CURRENCY_USD = "USD";
21+
22+
private int $decimalLength;
23+
24+
private string $currency;
25+
26+
/**
27+
* @param ColumnSetting $column
28+
* @param 'EUR'|'USD' $currency
29+
*/
30+
public function __construct(ColumnSetting $column, string $currency)
31+
{
32+
$this->decimalLength = $column->getDecimalLength();
33+
$this->currency = $currency;
34+
}
35+
36+
public function getAlignment(): array
37+
{
38+
return ['horizontal' => Alignment::HORIZONTAL_RIGHT];
39+
}
40+
41+
public function getNumberFormat(): array
42+
{
43+
$formatCode = '0.' . str_repeat('0', $this->decimalLength);
44+
45+
return ['formatCode' => $formatCode];
46+
}
47+
48+
public function getFormatCode(): string
49+
{
50+
return match ($this->currency) {
51+
self::CURRENCY_EUR => NumberFormat::FORMAT_CURRENCY_EUR,
52+
self::CURRENCY_USD => NumberFormat::FORMAT_CURRENCY_USD,
53+
default => throw new OutOfRangeException("Currency " . $this->currency . " is not supported"),
54+
};
55+
}
56+
}

src/ColumnFormatter/IntegerFormatter.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
use PhpOffice\PhpSpreadsheet\Style\Alignment;
1212
use PhpOffice\PhpSpreadsheet\Style\NumberFormat;
1313

14-
class IntegerFormatter implements ColumnFormatter
14+
class IntegerFormatter extends BaseFormatter
1515
{
1616
public function getAlignment(): array
1717
{

src/ColumnSetting.php

+17-18
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
namespace Fastbolt\ExcelWriter;
1010

1111
use Fastbolt\ExcelWriter\ColumnFormatter\ColumnFormatter;
12+
use Fastbolt\ExcelWriter\ColumnFormatter\CurrencyFormatter;
1213
use Fastbolt\ExcelWriter\ColumnFormatter\DateFormatter;
1314
use Fastbolt\ExcelWriter\ColumnFormatter\FloatFormatter;
1415
use Fastbolt\ExcelWriter\ColumnFormatter\IntegerFormatter;
@@ -17,11 +18,13 @@
1718

1819
class ColumnSetting
1920
{
20-
public const FORMAT_INTEGER = 'int';
21-
public const FORMAT_FLOAT = 'float';
22-
public const FORMAT_STRING = 'string';
23-
public const FORMAT_DATE = 'datetime';
21+
public const FORMAT_INTEGER = 'int';
22+
public const FORMAT_FLOAT = 'float';
23+
public const FORMAT_STRING = 'string';
24+
public const FORMAT_DATE = 'datetime';
2425
public const FOMRAT_PERCENTAGE = 'percentage';
26+
public const FORMAT_CURRENCY_EUR = 'currency_eur';
27+
public const FORMAT_CURRENCY_USD = 'currency_usd';
2528

2629
private string $format;
2730
private string $name = ''; //excel-name for the column
@@ -85,20 +88,16 @@ public function setName(string $name): ColumnSetting
8588
*/
8689
public function getFormatter(): ColumnFormatter
8790
{
88-
switch ($this->format) {
89-
case self::FORMAT_STRING:
90-
return new StringFormatter();
91-
case self::FORMAT_DATE:
92-
return new DateFormatter();
93-
case self::FORMAT_INTEGER:
94-
return new IntegerFormatter();
95-
case self::FORMAT_FLOAT:
96-
return new FloatFormatter($this);
97-
case self::FOMRAT_PERCENTAGE:
98-
return new PercentageFormatter($this);
99-
}
100-
101-
return new StringFormatter();
91+
return match ($this->format) {
92+
self::FORMAT_STRING => new StringFormatter(),
93+
self::FORMAT_DATE => new DateFormatter(),
94+
self::FORMAT_INTEGER => new IntegerFormatter(),
95+
self::FORMAT_FLOAT => new FloatFormatter($this),
96+
self::FOMRAT_PERCENTAGE => new PercentageFormatter($this),
97+
self::FORMAT_CURRENCY_EUR => new CurrencyFormatter($this, CurrencyFormatter::CURRENCY_EUR),
98+
self::FORMAT_CURRENCY_USD => new CurrencyFormatter($this, CurrencyFormatter::CURRENCY_USD),
99+
default => new StringFormatter(),
100+
};
102101
}
103102

104103
/**

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['numberFormat']['formatCode'] = $formatCode;
327+
}
328+
324329
$sheet->getStyle($column->getName() . ':' . $column->getName())
325330
->applyFromArray($format);
326331
}

0 commit comments

Comments
 (0)