-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
updating: adding modifiers for currency, numbers and time
- Loading branch information
1 parent
6e84a39
commit 6e577c4
Showing
4 changed files
with
116 additions
and
32 deletions.
There are no files selected for viewing
51 changes: 51 additions & 0 deletions
51
src/StingerSoft/ExcelCreator/CellModifier/AbstractModifier.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the Stinger Excel Creator package. | ||
* | ||
* (c) Oliver Kotte <oliver.kotte@stinger-soft.net> | ||
* (c) Florian Meyer <florian.meyer@stinger-soft.net> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace StingerSoft\ExcelCreator\CellModifier; | ||
|
||
use Box\Spout\Common\Entity\Cell as SpoutCell; | ||
use Box\Spout\Common\Entity\Style\Style; | ||
use PhpOffice\PhpSpreadsheet\Cell\Cell as SpreadsheetCell; | ||
use StingerSoft\ExcelCreator\ColumnBinding; | ||
|
||
abstract class AbstractModifier { | ||
|
||
public static function createFormatModifier(string $formatCode, ?string $spoutFormatCode = null): callable { | ||
$spoutFormatCode = $spoutFormatCode ?? $formatCode; | ||
|
||
/** | ||
* | ||
* @param ColumnBinding $binding | ||
* @param SpoutCell|SpreadsheetCell $cell | ||
*/ | ||
return static function (ColumnBinding $binding, $cell) use ($formatCode, $spoutFormatCode) { | ||
if($cell instanceof SpoutCell) { | ||
$style = $cell->getStyle(); | ||
if($style === null) { | ||
$style = new Style(); | ||
} else { | ||
$style = clone $style; | ||
$style->setId(null); | ||
} | ||
$style->setFormat($spoutFormatCode); | ||
$cell->setStyle($style); | ||
return; | ||
} | ||
if($cell instanceof SpreadsheetCell) { | ||
$style = $cell->getStyle(); | ||
$style->applyFromArray(['numberFormat' => ['formatCode' => $formatCode]]); | ||
return; | ||
} | ||
}; | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
src/StingerSoft/ExcelCreator/CellModifier/CurrencyModifier.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the Stinger Excel Creator package. | ||
* | ||
* (c) Oliver Kotte <oliver.kotte@stinger-soft.net> | ||
* (c) Florian Meyer <florian.meyer@stinger-soft.net> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace StingerSoft\ExcelCreator\CellModifier; | ||
|
||
class CurrencyModifier extends AbstractModifier { | ||
|
||
/** | ||
* @return callable | ||
*/ | ||
public static function createCurrencyFormatter(): callable { | ||
return self::createFormatModifier('_("$"* #,##0.00_);_("$"* \(#,##0.00\);_("$"* "-"??_);_(@_)', '_("$"* #,##0.00_),_("$"* \(#,##0.00\),_("$"* "-"??_),_(@_)'); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
src/StingerSoft/ExcelCreator/CellModifier/NumberModifier.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the Stinger Excel Creator package. | ||
* | ||
* (c) Oliver Kotte <oliver.kotte@stinger-soft.net> | ||
* (c) Florian Meyer <florian.meyer@stinger-soft.net> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace StingerSoft\ExcelCreator\CellModifier; | ||
|
||
class NumberModifier extends AbstractModifier { | ||
|
||
/** | ||
* @return callable | ||
*/ | ||
public static function createIntFormatter(): callable { | ||
return self::createFormatModifier('#,##0'); | ||
} | ||
|
||
/** | ||
* @return callable | ||
*/ | ||
public static function createFloatFormatter(): callable { | ||
return self::createFormatModifier('#,##0.00'); | ||
} | ||
|
||
} |