初始化仓库

This commit is contained in:
wangxiaowei
2025-04-22 14:09:52 +08:00
commit 8b100110bb
5155 changed files with 664201 additions and 0 deletions

View File

@ -0,0 +1,12 @@
<?php
namespace PhpOffice\PhpSpreadsheet\Style\NumberFormat;
abstract class BaseFormatter
{
protected static function stripQuotes(string $format): string
{
// Some non-number strings are quoted, so we'll get rid of the quotes, likewise any positional * symbols
return str_replace(['"', '*'], '', $format);
}
}

View File

@ -0,0 +1,182 @@
<?php
namespace PhpOffice\PhpSpreadsheet\Style\NumberFormat;
use PhpOffice\PhpSpreadsheet\Shared\Date;
class DateFormatter
{
/**
* Search/replace values to convert Excel date/time format masks to PHP format masks.
*/
private const DATE_FORMAT_REPLACEMENTS = [
// first remove escapes related to non-format characters
'\\' => '',
// 12-hour suffix
'am/pm' => 'A',
// 4-digit year
'e' => 'Y',
'yyyy' => 'Y',
// 2-digit year
'yy' => 'y',
// first letter of month - no php equivalent
'mmmmm' => 'M',
// full month name
'mmmm' => 'F',
// short month name
'mmm' => 'M',
// mm is minutes if time, but can also be month w/leading zero
// so we try to identify times be the inclusion of a : separator in the mask
// It isn't perfect, but the best way I know how
':mm' => ':i',
'mm:' => 'i:',
// full day of week name
'dddd' => 'l',
// short day of week name
'ddd' => 'D',
// days leading zero
'dd' => 'd',
// days no leading zero
'd' => 'j',
// fractional seconds - no php equivalent
'.s' => '',
];
/**
* Search/replace values to convert Excel date/time format masks hours to PHP format masks (24 hr clock).
*/
private const DATE_FORMAT_REPLACEMENTS24 = [
'hh' => 'H',
'h' => 'G',
// month leading zero
'mm' => 'm',
// month no leading zero
'm' => 'n',
// seconds
'ss' => 's',
];
/**
* Search/replace values to convert Excel date/time format masks hours to PHP format masks (12 hr clock).
*/
private const DATE_FORMAT_REPLACEMENTS12 = [
'hh' => 'h',
'h' => 'g',
// month leading zero
'mm' => 'm',
// month no leading zero
'm' => 'n',
// seconds
'ss' => 's',
];
private const HOURS_IN_DAY = 24;
private const MINUTES_IN_DAY = 60 * self::HOURS_IN_DAY;
private const SECONDS_IN_DAY = 60 * self::MINUTES_IN_DAY;
private const INTERVAL_PRECISION = 10;
private const INTERVAL_LEADING_ZERO = [
'[hh]',
'[mm]',
'[ss]',
];
private const INTERVAL_ROUND_PRECISION = [
// hours and minutes truncate
'[h]' => self::INTERVAL_PRECISION,
'[hh]' => self::INTERVAL_PRECISION,
'[m]' => self::INTERVAL_PRECISION,
'[mm]' => self::INTERVAL_PRECISION,
// seconds round
'[s]' => 0,
'[ss]' => 0,
];
private const INTERVAL_MULTIPLIER = [
'[h]' => self::HOURS_IN_DAY,
'[hh]' => self::HOURS_IN_DAY,
'[m]' => self::MINUTES_IN_DAY,
'[mm]' => self::MINUTES_IN_DAY,
'[s]' => self::SECONDS_IN_DAY,
'[ss]' => self::SECONDS_IN_DAY,
];
/** @param mixed $value */
private static function tryInterval(bool &$seekingBracket, string &$block, $value, string $format): void
{
if ($seekingBracket) {
if (false !== strpos($block, $format)) {
$hours = (string) (int) round(
self::INTERVAL_MULTIPLIER[$format] * $value,
self::INTERVAL_ROUND_PRECISION[$format]
);
if (strlen($hours) === 1 && in_array($format, self::INTERVAL_LEADING_ZERO, true)) {
$hours = "0$hours";
}
$block = str_replace($format, $hours, $block);
$seekingBracket = false;
}
}
}
/** @param mixed $value */
public static function format($value, string $format): string
{
// strip off first part containing e.g. [$-F800] or [$USD-409]
// general syntax: [$<Currency string>-<language info>]
// language info is in hexadecimal
// strip off chinese part like [DBNum1][$-804]
$format = (string) preg_replace('/^(\[DBNum\d\])*(\[\$[^\]]*\])/i', '', $format);
// OpenOffice.org uses upper-case number formats, e.g. 'YYYY', convert to lower-case;
// but we don't want to change any quoted strings
/** @var callable */
$callable = [self::class, 'setLowercaseCallback'];
$format = (string) preg_replace_callback('/(?:^|")([^"]*)(?:$|")/', $callable, $format);
// Only process the non-quoted blocks for date format characters
$blocks = explode('"', $format);
foreach ($blocks as $key => &$block) {
if ($key % 2 == 0) {
$block = strtr($block, self::DATE_FORMAT_REPLACEMENTS);
if (!strpos($block, 'A')) {
// 24-hour time format
// when [h]:mm format, the [h] should replace to the hours of the value * 24
$seekingBracket = true;
self::tryInterval($seekingBracket, $block, $value, '[h]');
self::tryInterval($seekingBracket, $block, $value, '[hh]');
self::tryInterval($seekingBracket, $block, $value, '[mm]');
self::tryInterval($seekingBracket, $block, $value, '[m]');
self::tryInterval($seekingBracket, $block, $value, '[s]');
self::tryInterval($seekingBracket, $block, $value, '[ss]');
$block = strtr($block, self::DATE_FORMAT_REPLACEMENTS24);
} else {
// 12-hour time format
$block = strtr($block, self::DATE_FORMAT_REPLACEMENTS12);
}
}
}
$format = implode('"', $blocks);
// escape any quoted characters so that DateTime format() will render them correctly
/** @var callable */
$callback = [self::class, 'escapeQuotesCallback'];
$format = (string) preg_replace_callback('/"(.*)"/U', $callback, $format);
$dateObj = Date::excelToDateTimeObject($value);
// If the colon preceding minute had been quoted, as happens in
// Excel 2003 XML formats, m will not have been changed to i above.
// Change it now.
$format = (string) \preg_replace('/\\\\:m/', ':i', $format);
return $dateObj->format($format);
}
private static function setLowercaseCallback(array $matches): string
{
return mb_strtolower($matches[0]);
}
private static function escapeQuotesCallback(array $matches): string
{
return '\\' . implode('\\', /** @scrutinizer ignore-type */ str_split($matches[1]));
}
}

View File

@ -0,0 +1,201 @@
<?php
namespace PhpOffice\PhpSpreadsheet\Style\NumberFormat;
use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
use PhpOffice\PhpSpreadsheet\RichText\RichText;
use PhpOffice\PhpSpreadsheet\Style\Color;
use PhpOffice\PhpSpreadsheet\Style\NumberFormat;
class Formatter
{
/**
* Matches any @ symbol that isn't enclosed in quotes.
*/
private const SYMBOL_AT = '/@(?=(?:[^"]*"[^"]*")*[^"]*\Z)/miu';
/**
* Matches any ; symbol that isn't enclosed in quotes, for a "section" split.
*/
private const SECTION_SPLIT = '/;(?=(?:[^"]*"[^"]*")*[^"]*\Z)/miu';
/**
* @param mixed $value
* @param mixed $comparisonValue
* @param mixed $defaultComparisonValue
*/
private static function splitFormatComparison(
$value,
?string $condition,
$comparisonValue,
string $defaultCondition,
$defaultComparisonValue
): bool {
if (!$condition) {
$condition = $defaultCondition;
$comparisonValue = $defaultComparisonValue;
}
switch ($condition) {
case '>':
return $value > $comparisonValue;
case '<':
return $value < $comparisonValue;
case '<=':
return $value <= $comparisonValue;
case '<>':
return $value != $comparisonValue;
case '=':
return $value == $comparisonValue;
}
return $value >= $comparisonValue;
}
/** @param mixed $value */
private static function splitFormatForSectionSelection(array $sections, $value): array
{
// Extract the relevant section depending on whether number is positive, negative, or zero?
// Text not supported yet.
// Here is how the sections apply to various values in Excel:
// 1 section: [POSITIVE/NEGATIVE/ZERO/TEXT]
// 2 sections: [POSITIVE/ZERO/TEXT] [NEGATIVE]
// 3 sections: [POSITIVE/TEXT] [NEGATIVE] [ZERO]
// 4 sections: [POSITIVE] [NEGATIVE] [ZERO] [TEXT]
$sectionCount = count($sections);
$color_regex = '/\\[(' . implode('|', Color::NAMED_COLORS) . ')\\]/mui';
$cond_regex = '/\\[(>|>=|<|<=|=|<>)([+-]?\\d+([.]\\d+)?)\\]/';
$colors = ['', '', '', '', ''];
$conditionOperations = ['', '', '', '', ''];
$conditionComparisonValues = [0, 0, 0, 0, 0];
for ($idx = 0; $idx < $sectionCount; ++$idx) {
if (preg_match($color_regex, $sections[$idx], $matches)) {
$colors[$idx] = $matches[0];
$sections[$idx] = (string) preg_replace($color_regex, '', $sections[$idx]);
}
if (preg_match($cond_regex, $sections[$idx], $matches)) {
$conditionOperations[$idx] = $matches[1];
$conditionComparisonValues[$idx] = $matches[2];
$sections[$idx] = (string) preg_replace($cond_regex, '', $sections[$idx]);
}
}
$color = $colors[0];
$format = $sections[0];
$absval = $value;
switch ($sectionCount) {
case 2:
$absval = abs($value);
if (!self::splitFormatComparison($value, $conditionOperations[0], $conditionComparisonValues[0], '>=', 0)) {
$color = $colors[1];
$format = $sections[1];
}
break;
case 3:
case 4:
$absval = abs($value);
if (!self::splitFormatComparison($value, $conditionOperations[0], $conditionComparisonValues[0], '>', 0)) {
if (self::splitFormatComparison($value, $conditionOperations[1], $conditionComparisonValues[1], '<', 0)) {
$color = $colors[1];
$format = $sections[1];
} else {
$color = $colors[2];
$format = $sections[2];
}
}
break;
}
return [$color, $format, $absval];
}
/**
* Convert a value in a pre-defined format to a PHP string.
*
* @param null|bool|float|int|RichText|string $value Value to format
* @param string $format Format code: see = self::FORMAT_* for predefined values;
* or can be any valid MS Excel custom format string
* @param array $callBack Callback function for additional formatting of string
*
* @return string Formatted string
*/
public static function toFormattedString($value, $format, $callBack = null)
{
if (is_bool($value)) {
return $value ? Calculation::getTRUE() : Calculation::getFALSE();
}
// For now we do not treat strings in sections, although section 4 of a format code affects strings
// Process a single block format code containing @ for text substitution
if (preg_match(self::SECTION_SPLIT, $format) === 0 && preg_match(self::SYMBOL_AT, $format) === 1) {
return str_replace('"', '', preg_replace(self::SYMBOL_AT, (string) $value, $format) ?? '');
}
// If we have a text value, return it "as is"
if (!is_numeric($value)) {
return (string) $value;
}
// For 'General' format code, we just pass the value although this is not entirely the way Excel does it,
// it seems to round numbers to a total of 10 digits.
if (($format === NumberFormat::FORMAT_GENERAL) || ($format === NumberFormat::FORMAT_TEXT)) {
return (string) $value;
}
// Ignore square-$-brackets prefix in format string, like "[$-411]ge.m.d", "[$-010419]0%", etc
$format = (string) preg_replace('/^\[\$-[^\]]*\]/', '', $format);
$format = (string) preg_replace_callback(
'/(["])(?:(?=(\\\\?))\\2.)*?\\1/u',
function ($matches) {
return str_replace('.', chr(0x00), $matches[0]);
},
$format
);
// Convert any other escaped characters to quoted strings, e.g. (\T to "T")
$format = (string) preg_replace('/(\\\(((.)(?!((AM\/PM)|(A\/P))))|([^ ])))(?=(?:[^"]|"[^"]*")*$)/ui', '"${2}"', $format);
// Get the sections, there can be up to four sections, separated with a semi-colon (but only if not a quoted literal)
$sections = preg_split(self::SECTION_SPLIT, $format) ?: [];
[$colors, $format, $value] = self::splitFormatForSectionSelection($sections, $value);
// In Excel formats, "_" is used to add spacing,
// The following character indicates the size of the spacing, which we can't do in HTML, so we just use a standard space
$format = (string) preg_replace('/_.?/ui', ' ', $format);
// Let's begin inspecting the format and converting the value to a formatted string
// Check for date/time characters (not inside quotes)
if (
(preg_match('/(\[\$[A-Z]*-[0-9A-F]*\])*[hmsdy](?=(?:[^"]|"[^"]*")*$)/miu', $format)) &&
(preg_match('/0(?![^\[]*\])/miu', $format) === 0)
) {
// datetime format
$value = DateFormatter::format($value, $format);
} else {
if (substr($format, 0, 1) === '"' && substr($format, -1, 1) === '"' && substr_count($format, '"') === 2) {
$value = substr($format, 1, -1);
} elseif (preg_match('/[0#, ]%/', $format)) {
// % number format - avoid weird '-0' problem
$value = PercentageFormatter::format(0 + (float) $value, $format);
} else {
$value = NumberFormatter::format($value, $format);
}
}
// Additional formatting provided by callback function
if ($callBack !== null) {
[$writerInstance, $function] = $callBack;
$value = $writerInstance->$function($value, $colors);
}
$value = str_replace(chr(0x00), '.', $value);
return $value;
}
}

View File

@ -0,0 +1,72 @@
<?php
namespace PhpOffice\PhpSpreadsheet\Style\NumberFormat;
use PhpOffice\PhpSpreadsheet\Calculation\MathTrig;
class FractionFormatter extends BaseFormatter
{
/**
* @param mixed $value
*/
public static function format($value, string $format): string
{
$format = self::stripQuotes($format);
$value = (float) $value;
$absValue = abs($value);
$sign = ($value < 0.0) ? '-' : '';
$integerPart = floor($absValue);
$decimalPart = self::getDecimal((string) $absValue);
if ($decimalPart === '0') {
return "{$sign}{$integerPart}";
}
$decimalLength = strlen($decimalPart);
$decimalDivisor = 10 ** $decimalLength;
preg_match('/(#?.*\?)\/(\?+|\d+)/', $format, $matches);
$formatIntegerPart = $matches[1];
if (is_numeric($matches[2])) {
$fractionDivisor = 100 / (int) $matches[2];
} else {
/** @var float */
$fractionDivisor = MathTrig\Gcd::evaluate((int) $decimalPart, $decimalDivisor);
}
$adjustedDecimalPart = (int) round((int) $decimalPart / $fractionDivisor, 0);
$adjustedDecimalDivisor = $decimalDivisor / $fractionDivisor;
if ((strpos($formatIntegerPart, '0') !== false)) {
return "{$sign}{$integerPart} {$adjustedDecimalPart}/{$adjustedDecimalDivisor}";
} elseif ((strpos($formatIntegerPart, '#') !== false)) {
if ($integerPart == 0) {
return "{$sign}{$adjustedDecimalPart}/{$adjustedDecimalDivisor}";
}
return "{$sign}{$integerPart} {$adjustedDecimalPart}/{$adjustedDecimalDivisor}";
} elseif ((substr($formatIntegerPart, 0, 3) == '? ?')) {
if ($integerPart == 0) {
$integerPart = '';
}
return "{$sign}{$integerPart} {$adjustedDecimalPart}/{$adjustedDecimalDivisor}";
}
$adjustedDecimalPart += $integerPart * $adjustedDecimalDivisor;
return "{$sign}{$adjustedDecimalPart}/{$adjustedDecimalDivisor}";
}
private static function getDecimal(string $value): string
{
$decimalPart = '0';
if (preg_match('/^\\d*[.](\\d*[1-9])0*$/', $value, $matches) === 1) {
$decimalPart = $matches[1];
}
return $decimalPart;
}
}

View File

@ -0,0 +1,326 @@
<?php
namespace PhpOffice\PhpSpreadsheet\Style\NumberFormat;
use PhpOffice\PhpSpreadsheet\Shared\StringHelper;
use PhpOffice\PhpSpreadsheet\Style\NumberFormat;
class NumberFormatter
{
private const NUMBER_REGEX = '/(0+)(\\.?)(0*)/';
private static function mergeComplexNumberFormatMasks(array $numbers, array $masks): array
{
$decimalCount = strlen($numbers[1]);
$postDecimalMasks = [];
do {
$tempMask = array_pop($masks);
if ($tempMask !== null) {
$postDecimalMasks[] = $tempMask;
$decimalCount -= strlen($tempMask);
}
} while ($tempMask !== null && $decimalCount > 0);
return [
implode('.', $masks),
implode('.', array_reverse($postDecimalMasks)),
];
}
/**
* @param mixed $number
*/
private static function processComplexNumberFormatMask($number, string $mask): string
{
/** @var string */
$result = $number;
$maskingBlockCount = preg_match_all('/0+/', $mask, $maskingBlocks, PREG_OFFSET_CAPTURE);
if ($maskingBlockCount > 1) {
$maskingBlocks = array_reverse($maskingBlocks[0]);
$offset = 0;
foreach ($maskingBlocks as $block) {
$size = strlen($block[0]);
$divisor = 10 ** $size;
$offset = $block[1];
/** @var float */
$numberFloat = $number;
$blockValue = sprintf("%0{$size}d", fmod($numberFloat, $divisor));
$number = floor($numberFloat / $divisor);
$mask = substr_replace($mask, $blockValue, $offset, $size);
}
/** @var string */
$numberString = $number;
if ($number > 0) {
$mask = substr_replace($mask, $numberString, $offset, 0);
}
$result = $mask;
}
return self::makeString($result);
}
/**
* @param mixed $number
*/
private static function complexNumberFormatMask($number, string $mask, bool $splitOnPoint = true): string
{
/** @var float */
$numberFloat = $number;
if ($splitOnPoint) {
$masks = explode('.', $mask);
if (count($masks) <= 2) {
$decmask = $masks[1] ?? '';
$decpos = substr_count($decmask, '0');
$numberFloat = round($numberFloat, $decpos);
}
}
$sign = ($numberFloat < 0.0) ? '-' : '';
$number = self::f2s(abs($numberFloat));
if ($splitOnPoint && strpos($mask, '.') !== false && strpos($number, '.') !== false) {
$numbers = explode('.', $number);
$masks = explode('.', $mask);
if (count($masks) > 2) {
$masks = self::mergeComplexNumberFormatMasks($numbers, $masks);
}
$integerPart = self::complexNumberFormatMask($numbers[0], $masks[0], false);
$numlen = strlen($numbers[1]);
$msklen = strlen($masks[1]);
if ($numlen < $msklen) {
$numbers[1] .= str_repeat('0', $msklen - $numlen);
}
$decimalPart = strrev(self::complexNumberFormatMask(strrev($numbers[1]), strrev($masks[1]), false));
$decimalPart = substr($decimalPart, 0, $msklen);
return "{$sign}{$integerPart}.{$decimalPart}";
}
if (strlen($number) < strlen($mask)) {
$number = str_repeat('0', strlen($mask) - strlen($number)) . $number;
}
$result = self::processComplexNumberFormatMask($number, $mask);
return "{$sign}{$result}";
}
public static function f2s(float $f): string
{
return self::floatStringConvertScientific((string) $f);
}
public static function floatStringConvertScientific(string $s): string
{
// convert only normalized form of scientific notation:
// optional sign, single digit 1-9,
// decimal point and digits (allowed to be omitted),
// E (e permitted), optional sign, one or more digits
if (preg_match('/^([+-])?([1-9])([.]([0-9]+))?[eE]([+-]?[0-9]+)$/', $s, $matches) === 1) {
$exponent = (int) $matches[5];
$sign = ($matches[1] === '-') ? '-' : '';
if ($exponent >= 0) {
$exponentPlus1 = $exponent + 1;
$out = $matches[2] . $matches[4];
$len = strlen($out);
if ($len < $exponentPlus1) {
$out .= str_repeat('0', $exponentPlus1 - $len);
}
$out = substr($out, 0, $exponentPlus1) . ((strlen($out) === $exponentPlus1) ? '' : ('.' . substr($out, $exponentPlus1)));
$s = "$sign$out";
} else {
$s = $sign . '0.' . str_repeat('0', -$exponent - 1) . $matches[2] . $matches[4];
}
}
return $s;
}
/**
* @param mixed $value
*/
private static function formatStraightNumericValue($value, string $format, array $matches, bool $useThousands): string
{
/** @var float */
$valueFloat = $value;
$left = $matches[1];
$dec = $matches[2];
$right = $matches[3];
// minimun width of formatted number (including dot)
$minWidth = strlen($left) + strlen($dec) + strlen($right);
if ($useThousands) {
$value = number_format(
$valueFloat,
strlen($right),
StringHelper::getDecimalSeparator(),
StringHelper::getThousandsSeparator()
);
return self::pregReplace(self::NUMBER_REGEX, $value, $format);
}
if (preg_match('/[0#]E[+-]0/i', $format)) {
// Scientific format
$decimals = strlen($right);
$size = $decimals + 3;
return sprintf("%{$size}.{$decimals}E", $valueFloat);
} elseif (preg_match('/0([^\d\.]+)0/', $format) || substr_count($format, '.') > 1) {
if ($valueFloat == floor($valueFloat) && substr_count($format, '.') === 1) {
$value *= 10 ** strlen(explode('.', $format)[1]);
}
$result = self::complexNumberFormatMask($value, $format);
if (strpos($result, 'E') !== false) {
// This is a hack and doesn't match Excel.
// It will, at least, be an accurate representation,
// even if formatted incorrectly.
// This is needed for absolute values >=1E18.
$result = self::f2s($valueFloat);
}
return $result;
}
$sprintf_pattern = "%0$minWidth." . strlen($right) . 'f';
/** @var float */
$valueFloat = $value;
$value = sprintf($sprintf_pattern, round($valueFloat, strlen($right)));
return self::pregReplace(self::NUMBER_REGEX, $value, $format);
}
/**
* @param mixed $value
*/
public static function format($value, string $format): string
{
// The "_" in this string has already been stripped out,
// so this test is never true. Furthermore, testing
// on Excel shows this format uses Euro symbol, not "EUR".
// if ($format === NumberFormat::FORMAT_CURRENCY_EUR_SIMPLE) {
// return 'EUR ' . sprintf('%1.2f', $value);
// }
$baseFormat = $format;
$useThousands = self::areThousandsRequired($format);
$scale = self::scaleThousandsMillions($format);
if (preg_match('/[#\?0]?.*[#\?0]\/(\?+|\d+|#)/', $format)) {
// It's a dirty hack; but replace # and 0 digit placeholders with ?
$format = (string) preg_replace('/[#0]+\//', '?/', $format);
$format = (string) preg_replace('/\/[#0]+/', '/?', $format);
$value = FractionFormatter::format($value, $format);
} else {
// Handle the number itself
// scale number
$value = $value / $scale;
$paddingPlaceholder = (strpos($format, '?') !== false);
// Replace # or ? with 0
$format = self::pregReplace('/[\\#\?](?=(?:[^"]*"[^"]*")*[^"]*\Z)/', '0', $format);
// Remove locale code [$-###] for an LCID
$format = self::pregReplace('/\[\$\-.*\]/', '', $format);
$n = '/\\[[^\\]]+\\]/';
$m = self::pregReplace($n, '', $format);
// Some non-number strings are quoted, so we'll get rid of the quotes, likewise any positional * symbols
$format = self::makeString(str_replace(['"', '*'], '', $format));
if (preg_match(self::NUMBER_REGEX, $m, $matches)) {
// There are placeholders for digits, so inject digits from the value into the mask
$value = self::formatStraightNumericValue($value, $format, $matches, $useThousands);
if ($paddingPlaceholder === true) {
$value = self::padValue($value, $baseFormat);
}
} elseif ($format !== NumberFormat::FORMAT_GENERAL) {
// Yes, I know that this is basically just a hack;
// if there's no placeholders for digits, just return the format mask "as is"
$value = self::makeString(str_replace('?', '', $format));
}
}
if (preg_match('/\[\$(.*)\]/u', $format, $m)) {
// Currency or Accounting
$currencyCode = $m[1];
[$currencyCode] = explode('-', $currencyCode);
if ($currencyCode == '') {
$currencyCode = StringHelper::getCurrencyCode();
}
$value = self::pregReplace('/\[\$([^\]]*)\]/u', $currencyCode, (string) $value);
}
if (
(strpos((string) $value, '0.') !== false) &&
((strpos($baseFormat, '#.') !== false) || (strpos($baseFormat, '?.') !== false))
) {
$value = preg_replace('/(\b)0\.|([^\d])0\./', '${2}.', (string) $value);
}
return (string) $value;
}
/**
* @param array|string $value
*/
private static function makeString($value): string
{
return is_array($value) ? '' : "$value";
}
private static function pregReplace(string $pattern, string $replacement, string $subject): string
{
return self::makeString(preg_replace($pattern, $replacement, $subject) ?? '');
}
public static function padValue(string $value, string $baseFormat): string
{
/** @phpstan-ignore-next-line */
[$preDecimal, $postDecimal] = preg_split('/\.(?=(?:[^"]*"[^"]*")*[^"]*\Z)/miu', $baseFormat . '.?');
$length = strlen($value);
if (strpos($postDecimal, '?') !== false) {
$value = str_pad(rtrim($value, '0. '), $length, ' ', STR_PAD_RIGHT);
}
if (strpos($preDecimal, '?') !== false) {
$value = str_pad(ltrim($value, '0, '), $length, ' ', STR_PAD_LEFT);
}
return $value;
}
/**
* Find out if we need thousands separator
* This is indicated by a comma enclosed by a digit placeholders: #, 0 or ?
*/
public static function areThousandsRequired(string &$format): bool
{
$useThousands = (bool) preg_match('/([#\?0]),([#\?0])/', $format);
if ($useThousands) {
$format = self::pregReplace('/([#\?0]),([#\?0])/', '${1}${2}', $format);
}
return $useThousands;
}
/**
* Scale thousands, millions,...
* This is indicated by a number of commas after a digit placeholder: #, or 0.0,, or ?,.
*/
public static function scaleThousandsMillions(string &$format): int
{
$scale = 1; // same as no scale
if (preg_match('/(#|0|\?)(,+)/', $format, $matches)) {
$scale = 1000 ** strlen($matches[2]);
// strip the commas
$format = self::pregReplace('/([#\?0]),+/', '${1}', $format);
}
return $scale;
}
}

View File

@ -0,0 +1,48 @@
<?php
namespace PhpOffice\PhpSpreadsheet\Style\NumberFormat;
use PhpOffice\PhpSpreadsheet\Style\NumberFormat;
class PercentageFormatter extends BaseFormatter
{
/** @param float|int $value */
public static function format($value, string $format): string
{
if ($format === NumberFormat::FORMAT_PERCENTAGE) {
return round((100 * $value), 0) . '%';
}
$value *= 100;
$format = self::stripQuotes($format);
[, $vDecimals] = explode('.', ((string) $value) . '.');
$vDecimalCount = strlen(rtrim($vDecimals, '0'));
$format = str_replace('%', '%%', $format);
$wholePartSize = strlen((string) floor($value));
$decimalPartSize = 0;
$placeHolders = '';
// Number of decimals
if (preg_match('/\.([?0]+)/u', $format, $matches)) {
$decimalPartSize = strlen($matches[1]);
$vMinDecimalCount = strlen(rtrim($matches[1], '?'));
$decimalPartSize = min(max($vMinDecimalCount, $vDecimalCount), $decimalPartSize);
$placeHolders = str_repeat(' ', strlen($matches[1]) - $decimalPartSize);
}
// Number of digits to display before the decimal
if (preg_match('/([#0,]+)\.?/u', $format, $matches)) {
$firstZero = preg_replace('/^[#,]*/', '', $matches[1]) ?? '';
$wholePartSize = max($wholePartSize, strlen($firstZero));
}
$wholePartSize += $decimalPartSize + (int) ($decimalPartSize > 0);
$replacement = "0{$wholePartSize}.{$decimalPartSize}";
$mask = (string) preg_replace('/[#0,]+\.?[?#0,]*/ui', "%{$replacement}f{$placeHolders}", $format);
/** @var float */
$valueFloat = $value;
return sprintf($mask, round($valueFloat, $decimalPartSize));
}
}

View File

@ -0,0 +1,102 @@
<?php
namespace PhpOffice\PhpSpreadsheet\Style\NumberFormat\Wizard;
use NumberFormatter;
use PhpOffice\PhpSpreadsheet\Exception;
class Accounting extends Currency
{
/**
* @param string $currencyCode the currency symbol or code to display for this mask
* @param int $decimals number of decimal places to display, in the range 0-30
* @param bool $thousandsSeparator indicator whether the thousands separator should be used, or not
* @param bool $currencySymbolPosition indicates whether the currency symbol comes before or after the value
* Possible values are Currency::LEADING_SYMBOL and Currency::TRAILING_SYMBOL
* @param bool $currencySymbolSpacing indicates whether there is spacing between the currency symbol and the value
* Possible values are Currency::SYMBOL_WITH_SPACING and Currency::SYMBOL_WITHOUT_SPACING
* @param ?string $locale Set the locale for the currency format; or leave as the default null.
* If provided, Locale values must be a valid formatted locale string (e.g. 'en-GB', 'fr', uz-Arab-AF).
* Note that setting a locale will override any other settings defined in this class
* other than the currency code; or decimals (unless the decimals value is set to 0).
*
* @throws Exception If a provided locale code is not a valid format
*/
public function __construct(
string $currencyCode = '$',
int $decimals = 2,
bool $thousandsSeparator = true,
bool $currencySymbolPosition = self::LEADING_SYMBOL,
bool $currencySymbolSpacing = self::SYMBOL_WITHOUT_SPACING,
?string $locale = null
) {
$this->setCurrencyCode($currencyCode);
$this->setThousandsSeparator($thousandsSeparator);
$this->setDecimals($decimals);
$this->setCurrencySymbolPosition($currencySymbolPosition);
$this->setCurrencySymbolSpacing($currencySymbolSpacing);
$this->setLocale($locale);
}
/**
* @throws Exception if the Intl extension and ICU version don't support Accounting formats
*/
protected function getLocaleFormat(): string
{
if (version_compare(PHP_VERSION, '7.4.1', '<')) {
throw new Exception('The Intl extension does not support Accounting Formats below PHP 7.4.1');
}
if ($this->icuVersion() < 53.0) {
throw new Exception('The Intl extension does not support Accounting Formats without ICU 53');
}
// Scrutinizer does not recognize CURRENCY_ACCOUNTING
$formatter = new Locale($this->fullLocale, NumberFormatter::CURRENCY_ACCOUNTING);
$mask = $formatter->format();
if ($this->decimals === 0) {
$mask = (string) preg_replace('/\.0+/miu', '', $mask);
}
return str_replace('¤', $this->formatCurrencyCode(), $mask);
}
private function icuVersion(): float
{
[$major, $minor] = explode('.', INTL_ICU_VERSION);
return (float) "{$major}.{$minor}";
}
private function formatCurrencyCode(): string
{
if ($this->locale === null) {
return $this->currencyCode . '*';
}
return "[\${$this->currencyCode}-{$this->locale}]";
}
public function format(): string
{
if ($this->localeFormat !== null) {
return $this->localeFormat;
}
return sprintf(
'_-%s%s%s0%s%s%s_-',
$this->currencySymbolPosition === self::LEADING_SYMBOL ? $this->formatCurrencyCode() : 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->formatCurrencyCode() : null
);
}
}

View File

@ -0,0 +1,112 @@
<?php
namespace PhpOffice\PhpSpreadsheet\Style\NumberFormat\Wizard;
use NumberFormatter;
use PhpOffice\PhpSpreadsheet\Exception;
class Currency extends Number
{
public const LEADING_SYMBOL = true;
public const TRAILING_SYMBOL = false;
public const SYMBOL_WITH_SPACING = true;
public const SYMBOL_WITHOUT_SPACING = false;
protected string $currencyCode = '$';
protected bool $currencySymbolPosition = self::LEADING_SYMBOL;
protected bool $currencySymbolSpacing = self::SYMBOL_WITHOUT_SPACING;
/**
* @param string $currencyCode the currency symbol or code to display for this mask
* @param int $decimals number of decimal places to display, in the range 0-30
* @param bool $thousandsSeparator indicator whether the thousands separator should be used, or not
* @param bool $currencySymbolPosition indicates whether the currency symbol comes before or after the value
* Possible values are Currency::LEADING_SYMBOL and Currency::TRAILING_SYMBOL
* @param bool $currencySymbolSpacing indicates whether there is spacing between the currency symbol and the value
* Possible values are Currency::SYMBOL_WITH_SPACING and Currency::SYMBOL_WITHOUT_SPACING
* @param ?string $locale Set the locale for the currency format; or leave as the default null.
* If provided, Locale values must be a valid formatted locale string (e.g. 'en-GB', 'fr', uz-Arab-AF).
* Note that setting a locale will override any other settings defined in this class
* other than the currency code; or decimals (unless the decimals value is set to 0).
*
* @throws Exception If a provided locale code is not a valid format
*/
public function __construct(
string $currencyCode = '$',
int $decimals = 2,
bool $thousandsSeparator = true,
bool $currencySymbolPosition = self::LEADING_SYMBOL,
bool $currencySymbolSpacing = self::SYMBOL_WITHOUT_SPACING,
?string $locale = null
) {
$this->setCurrencyCode($currencyCode);
$this->setThousandsSeparator($thousandsSeparator);
$this->setDecimals($decimals);
$this->setCurrencySymbolPosition($currencySymbolPosition);
$this->setCurrencySymbolSpacing($currencySymbolSpacing);
$this->setLocale($locale);
}
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;
}
protected function getLocaleFormat(): string
{
$formatter = new Locale($this->fullLocale, NumberFormatter::CURRENCY);
$mask = $formatter->format();
if ($this->decimals === 0) {
$mask = (string) preg_replace('/\.0+/miu', '', $mask);
}
return str_replace('¤', $this->formatCurrencyCode(), $mask);
}
private function formatCurrencyCode(): string
{
if ($this->locale === null) {
return $this->currencyCode;
}
return "[\${$this->currencyCode}-{$this->locale}]";
}
public function format(): string
{
if ($this->localeFormat !== null) {
return $this->localeFormat;
}
return sprintf(
'%s%s%s0%s%s%s',
$this->currencySymbolPosition === self::LEADING_SYMBOL ? $this->formatCurrencyCode() : null,
(
$this->currencySymbolPosition === self::LEADING_SYMBOL &&
$this->currencySymbolSpacing === self::SYMBOL_WITH_SPACING
) ? "\u{a0}" : '',
$this->thousandsSeparator ? '#,##' : null,
$this->decimals > 0 ? '.' . str_repeat('0', $this->decimals) : null,
(
$this->currencySymbolPosition === self::TRAILING_SYMBOL &&
$this->currencySymbolSpacing === self::SYMBOL_WITH_SPACING
) ? "\u{a0}" : '',
$this->currencySymbolPosition === self::TRAILING_SYMBOL ? $this->formatCurrencyCode() : null
);
}
}

View File

@ -0,0 +1,37 @@
<?php
namespace PhpOffice\PhpSpreadsheet\Style\NumberFormat\Wizard;
use NumberFormatter;
use PhpOffice\PhpSpreadsheet\Exception;
final class Locale
{
/**
* Language code: ISO-639 2 character, alpha.
* Optional script code: ISO-15924 4 alpha.
* Optional country code: ISO-3166-1, 2 character alpha.
* Separated by underscores or dashes.
*/
public const STRUCTURE = '/^(?P<language>[a-z]{2})([-_](?P<script>[a-z]{4}))?([-_](?P<country>[a-z]{2}))?$/i';
private NumberFormatter $formatter;
public function __construct(?string $locale, int $style)
{
if (class_exists(NumberFormatter::class) === false) {
throw new Exception();
}
$formatterLocale = str_replace('-', '_', $locale ?? '');
$this->formatter = new NumberFormatter($formatterLocale, $style);
if ($this->formatter->getLocale() !== $formatterLocale) {
throw new Exception("Unable to read locale data for '{$locale}'");
}
}
public function format(): string
{
return $this->formatter->getPattern();
}
}

View File

@ -0,0 +1,57 @@
<?php
namespace PhpOffice\PhpSpreadsheet\Style\NumberFormat\Wizard;
use PhpOffice\PhpSpreadsheet\Exception;
class Number extends NumberBase implements Wizard
{
public const WITH_THOUSANDS_SEPARATOR = true;
public const WITHOUT_THOUSANDS_SEPARATOR = false;
protected bool $thousandsSeparator = true;
/**
* @param int $decimals number of decimal places to display, in the range 0-30
* @param bool $thousandsSeparator indicator whether the thousands separator should be used, or not
* @param ?string $locale Set the locale for the number format; or leave as the default null.
* Locale has no effect for Number Format values, and is retained here only for compatibility
* with the other Wizards.
* If provided, Locale values must be a valid formatted locale string (e.g. 'en-GB', 'fr', uz-Arab-AF).
*
* @throws Exception If a provided locale code is not a valid format
*/
public function __construct(
int $decimals = 2,
bool $thousandsSeparator = self::WITH_THOUSANDS_SEPARATOR,
?string $locale = null
) {
$this->setDecimals($decimals);
$this->setThousandsSeparator($thousandsSeparator);
$this->setLocale($locale);
}
public function setThousandsSeparator(bool $thousandsSeparator = self::WITH_THOUSANDS_SEPARATOR): void
{
$this->thousandsSeparator = $thousandsSeparator;
}
/**
* As MS Excel cannot easily handle Lakh, which is the only locale-specific Number format variant,
* we don't use locale with Numbers.
*/
protected function getLocaleFormat(): string
{
return $this->format();
}
public function format(): string
{
return sprintf(
'%s0%s',
$this->thousandsSeparator ? '#,##' : null,
$this->decimals > 0 ? '.' . str_repeat('0', $this->decimals) : null
);
}
}

View File

@ -0,0 +1,80 @@
<?php
namespace PhpOffice\PhpSpreadsheet\Style\NumberFormat\Wizard;
use NumberFormatter;
use PhpOffice\PhpSpreadsheet\Exception;
use PhpOffice\PhpSpreadsheet\Style\NumberFormat;
abstract class NumberBase
{
protected const MAX_DECIMALS = 30;
protected int $decimals = 2;
protected ?string $locale = null;
protected ?string $fullLocale = null;
protected ?string $localeFormat = null;
public function setDecimals(int $decimals = 2): void
{
$this->decimals = ($decimals > self::MAX_DECIMALS) ? self::MAX_DECIMALS : max($decimals, 0);
}
/**
* Setting a locale will override any settings defined in this class.
*
* @throws Exception If the locale code is not a valid format
*/
public function setLocale(?string $locale = null): void
{
if ($locale === null) {
$this->localeFormat = $this->locale = $this->fullLocale = null;
return;
}
$this->locale = $this->validateLocale($locale);
if (class_exists(NumberFormatter::class)) {
$this->localeFormat = $this->getLocaleFormat();
}
}
/**
* Stub: should be implemented as a concrete method in concrete wizards.
*/
abstract protected function getLocaleFormat(): string;
/**
* @throws Exception If the locale code is not a valid format
*/
private function validateLocale(string $locale): string
{
if (preg_match(Locale::STRUCTURE, $locale, $matches, PREG_UNMATCHED_AS_NULL) !== 1) {
throw new Exception("Invalid locale code '{$locale}'");
}
['language' => $language, 'script' => $script, 'country' => $country] = $matches;
// Set case and separator to match standardised locale case
$language = strtolower($language ?? '');
$script = ($script === null) ? null : ucfirst(strtolower($script));
$country = ($country === null) ? null : strtoupper($country);
$this->fullLocale = implode('-', array_filter([$language, $script, $country]));
return $country === null ? $language : "{$language}-{$country}";
}
public function format(): string
{
return NumberFormat::FORMAT_GENERAL;
}
public function __toString(): string
{
return $this->format();
}
}

View File

@ -0,0 +1,40 @@
<?php
namespace PhpOffice\PhpSpreadsheet\Style\NumberFormat\Wizard;
use NumberFormatter;
use PhpOffice\PhpSpreadsheet\Exception;
class Percentage extends NumberBase implements Wizard
{
/**
* @param int $decimals number of decimal places to display, in the range 0-30
* @param ?string $locale Set the locale for the percentage format; or leave as the default null.
* If provided, Locale values must be a valid formatted locale string (e.g. 'en-GB', 'fr', uz-Arab-AF).
*
* @throws Exception If a provided locale code is not a valid format
*/
public function __construct(int $decimals = 2, ?string $locale = null)
{
$this->setDecimals($decimals);
$this->setLocale($locale);
}
protected function getLocaleFormat(): string
{
$formatter = new Locale($this->fullLocale, NumberFormatter::PERCENT);
return $this->decimals > 0
? str_replace('0', '0.' . str_repeat('0', $this->decimals), $formatter->format())
: $formatter->format();
}
public function format(): string
{
if ($this->localeFormat !== null) {
return $this->localeFormat;
}
return sprintf('0%s%%', $this->decimals > 0 ? '.' . str_repeat('0', $this->decimals) : null);
}
}

View File

@ -0,0 +1,33 @@
<?php
namespace PhpOffice\PhpSpreadsheet\Style\NumberFormat\Wizard;
use PhpOffice\PhpSpreadsheet\Exception;
class Scientific extends NumberBase implements Wizard
{
/**
* @param int $decimals number of decimal places to display, in the range 0-30
* @param ?string $locale Set the locale for the scientific format; or leave as the default null.
* Locale has no effect for Scientific Format values, and is retained here for compatibility
* with the other Wizards.
* If provided, Locale values must be a valid formatted locale string (e.g. 'en-GB', 'fr', uz-Arab-AF).
*
* @throws Exception If a provided locale code is not a valid format
*/
public function __construct(int $decimals = 2, ?string $locale = null)
{
$this->setDecimals($decimals);
$this->setLocale($locale);
}
protected function getLocaleFormat(): string
{
return $this->format();
}
public function format(): string
{
return sprintf('0%sE+00', $this->decimals > 0 ? '.' . str_repeat('0', $this->decimals) : null);
}
}

View File

@ -0,0 +1,8 @@
<?php
namespace PhpOffice\PhpSpreadsheet\Style\NumberFormat\Wizard;
interface Wizard
{
public function format(): string;
}