diff --git a/CHANGELOG.md b/CHANGELOG.md index 1ef24bfb3e..76673a9419 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org). ### Added - Support for configuring a Chart Title's overlay [PR #3325](https://github.com/PHPOffice/PhpSpreadsheet/pull/3325) +- Wizards for defining Number Format masks for Percentages, Scientific, Numbers, and Currency [PR #3334](https://github.com/PHPOffice/PhpSpreadsheet/pull/3334) ### Changed diff --git a/src/PhpSpreadsheet/Style/NumberFormat/Wizard/Currency.php b/src/PhpSpreadsheet/Style/NumberFormat/Wizard/Currency.php new file mode 100644 index 0000000000..461af3344e --- /dev/null +++ b/src/PhpSpreadsheet/Style/NumberFormat/Wizard/Currency.php @@ -0,0 +1,68 @@ +setCurrencyCode($currencyCode); + $this->setThousandsSeparator($thousandsSeparator); + $this->setDecimals($decimals); + $this->setCurrencySymbolPosition($currencySymbolPosition); + $this->setCurrencySymbolSpacing($currencySymbolSpacing); + } + + public function setCurrencyCode(string $currencyCode): void + { + $this->currencyCode = $currencyCode; + } + + public function setCurrencySymbolPosition(bool $currencySymbolPosition = self::LEADING_SYMBOL): void + { + $this->currencySymbolPosition = $currencySymbolPosition; + } + + public function setCurrencySymbolSpacing(bool $currencySymbolSpacing = self::SYMBOL_WITHOUT_SPACING): void + { + $this->currencySymbolSpacing = $currencySymbolSpacing; + } + + public function format(): string + { + return sprintf( + '%s%s%s0%s%s%s', + $this->currencySymbolPosition === self::LEADING_SYMBOL ? $this->currencyCode : null, + ( + $this->currencySymbolPosition === self::LEADING_SYMBOL && + $this->currencySymbolSpacing === self::SYMBOL_WITH_SPACING + ) ? ' ' : '', + $this->thousandsSeparator ? '#,##' : null, + $this->decimals > 0 ? '.' . str_repeat('0', $this->decimals) : null, + ( + $this->currencySymbolPosition === self::TRAILING_SYMBOL && + $this->currencySymbolSpacing === self::SYMBOL_WITH_SPACING + ) ? ' ' : '', + $this->currencySymbolPosition === self::TRAILING_SYMBOL ? $this->currencyCode : null + ); + } +} diff --git a/src/PhpSpreadsheet/Style/NumberFormat/Wizard/Number.php b/src/PhpSpreadsheet/Style/NumberFormat/Wizard/Number.php new file mode 100644 index 0000000000..f529c814d2 --- /dev/null +++ b/src/PhpSpreadsheet/Style/NumberFormat/Wizard/Number.php @@ -0,0 +1,32 @@ +setThousandsSeparator($thousandsSeparator); + $this->setDecimals($decimals); + } + + public function setThousandsSeparator(bool $thousandsSeparator = self::WITH_THOUSANDS_SEPARATOR): void + { + $this->thousandsSeparator = $thousandsSeparator; + } + + public function format(): string + { + return sprintf( + '%s0%s', + $this->thousandsSeparator ? '#,##' : null, + $this->decimals > 0 ? '.' . str_repeat('0', $this->decimals) : null + ); + } +} diff --git a/src/PhpSpreadsheet/Style/NumberFormat/Wizard/NumberBase.php b/src/PhpSpreadsheet/Style/NumberFormat/Wizard/NumberBase.php new file mode 100644 index 0000000000..6dceee4034 --- /dev/null +++ b/src/PhpSpreadsheet/Style/NumberFormat/Wizard/NumberBase.php @@ -0,0 +1,20 @@ +decimals = ($decimals > self::MAX_DECIMALS) ? self::MAX_DECIMALS : max($decimals, 0); + } + + public function __toString(): string + { + return $this->format(); + } +} diff --git a/src/PhpSpreadsheet/Style/NumberFormat/Wizard/Percentage.php b/src/PhpSpreadsheet/Style/NumberFormat/Wizard/Percentage.php new file mode 100644 index 0000000000..62e795afb4 --- /dev/null +++ b/src/PhpSpreadsheet/Style/NumberFormat/Wizard/Percentage.php @@ -0,0 +1,19 @@ +setDecimals($decimals); + } + + public function format(): string + { + return sprintf('0%s%%', $this->decimals > 0 ? '.' . str_repeat('0', $this->decimals) : null); + } +} diff --git a/src/PhpSpreadsheet/Style/NumberFormat/Wizard/Scientific.php b/src/PhpSpreadsheet/Style/NumberFormat/Wizard/Scientific.php new file mode 100644 index 0000000000..97dacb7c45 --- /dev/null +++ b/src/PhpSpreadsheet/Style/NumberFormat/Wizard/Scientific.php @@ -0,0 +1,19 @@ +setDecimals($decimals); + } + + public function format(): string + { + return sprintf('0%sE+00', $this->decimals > 0 ? '.' . str_repeat('0', $this->decimals) : null); + } +} diff --git a/src/PhpSpreadsheet/Style/NumberFormat/Wizard/Wizard.php b/src/PhpSpreadsheet/Style/NumberFormat/Wizard/Wizard.php new file mode 100644 index 0000000000..de4dc4f818 --- /dev/null +++ b/src/PhpSpreadsheet/Style/NumberFormat/Wizard/Wizard.php @@ -0,0 +1,8 @@ +