Skip to content

Commit

Permalink
updating: adding modifiers for currency, numbers and time
Browse files Browse the repository at this point in the history
  • Loading branch information
koddistortion committed Aug 24, 2020
1 parent 6e84a39 commit 6e577c4
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 32 deletions.
51 changes: 51 additions & 0 deletions src/StingerSoft/ExcelCreator/CellModifier/AbstractModifier.php
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 src/StingerSoft/ExcelCreator/CellModifier/CurrencyModifier.php
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\),_("$"* "-"??_),_(@_)');
}

}
40 changes: 8 additions & 32 deletions src/StingerSoft/ExcelCreator/CellModifier/DateTimeModifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,7 @@

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;

class DateTimeModifier {
class DateTimeModifier extends AbstractModifier {

/**
* @return callable
Expand All @@ -29,36 +23,18 @@ public static function createDateModifier(): callable {
}

/**
* @param bool $addSeconds Note: not working for SPOUT..
* @param bool $addSeconds Note: not working for SPOUT..
* @return callable
*/
public static function createDateTimeModifier(bool $addSeconds = false): callable {
return self::createFormatModifier('m/d/yyyy h:mm' . ($addSeconds ? ':ss' : ''), 'm/d/yy h:mm');
}

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();
}
$style->setFormat($spoutFormatCode);
$cell->setStyle($style);
return;
}
if($cell instanceof SpreadsheetCell) {
$style = $cell->getStyle();
$style->applyFromArray(['numberFormat' => ['formatCode' => $formatCode]]);
return;
}
};
/**
* @param bool $addSeconds
* @return callable
*/
public static function createTimeModifier(bool $addSeconds = false): callable {
return self::createFormatModifier('h:mm' . ($addSeconds ? ':ss' : ''));
}
}
32 changes: 32 additions & 0 deletions src/StingerSoft/ExcelCreator/CellModifier/NumberModifier.php
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');
}

}

0 comments on commit 6e577c4

Please sign in to comment.