Skip to content

Commit 4c22fe9

Browse files
committed
currency formatter
1 parent 3ae6eb1 commit 4c22fe9

File tree

2 files changed

+37
-4
lines changed

2 files changed

+37
-4
lines changed
+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace Fastbolt\ExcelWriter\ColumnFormatter;
4+
5+
use Fastbolt\ExcelWriter\ColumnSetting;
6+
use PhpOffice\PhpSpreadsheet\Style\Alignment;
7+
8+
class CurrencyFormatter extends BaseFormatter
9+
{
10+
private int $decimalLength;
11+
12+
public function __construct(ColumnSetting $column)
13+
{
14+
$this->decimalLength = $column->getDecimalLength();
15+
}
16+
17+
public function getAlignment(): array
18+
{
19+
return ['horizontal' => Alignment::HORIZONTAL_RIGHT];
20+
}
21+
22+
public function getNumberFormat(): array
23+
{
24+
$formatCode = '0.' . str_repeat('0', $this->decimalLength);
25+
26+
return ['formatCode' => $formatCode];
27+
}
28+
}

src/ColumnSetting.php

+9-4
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,22 @@
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;
1516
use Fastbolt\ExcelWriter\ColumnFormatter\PercentageFormatter;
1617
use Fastbolt\ExcelWriter\ColumnFormatter\StringFormatter;
18+
use PhpOffice\PhpSpreadsheet\Calculation\DateTimeExcel\Current;
1719

1820
class ColumnSetting
1921
{
20-
public const FORMAT_INTEGER = 'int';
21-
public const FORMAT_FLOAT = 'float';
22-
public const FORMAT_STRING = 'string';
23-
public const FORMAT_DATE = 'datetime';
22+
public const FORMAT_INTEGER = 'int';
23+
public const FORMAT_FLOAT = 'float';
24+
public const FORMAT_STRING = 'string';
25+
public const FORMAT_DATE = 'datetime';
2426
public const FOMRAT_PERCENTAGE = 'percentage';
27+
public const FORMAT_CURRENCY = 'currency';
2528

2629
private string $format;
2730
private string $name = ''; //excel-name for the column
@@ -96,6 +99,8 @@ public function getFormatter(): ColumnFormatter
9699
return new FloatFormatter($this);
97100
case self::FOMRAT_PERCENTAGE:
98101
return new PercentageFormatter($this);
102+
case self::FORMAT_CURRENCY:
103+
return new CurrencyFormatter($this);
99104
}
100105

101106
return new StringFormatter();

0 commit comments

Comments
 (0)