init
This commit is contained in:
commit
72a26edcff
22092 changed files with 2101903 additions and 0 deletions
25
lib/PhpSpreadsheet/Style/NumberFormat/BaseFormatter.php
Normal file
25
lib/PhpSpreadsheet/Style/NumberFormat/BaseFormatter.php
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
<?php
|
||||
|
||||
namespace PhpOffice\PhpSpreadsheet\Style\NumberFormat;
|
||||
|
||||
use PhpOffice\PhpSpreadsheet\Shared\StringHelper;
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
protected static function adjustSeparators(string $value): string
|
||||
{
|
||||
$thousandsSeparator = StringHelper::getThousandsSeparator();
|
||||
$decimalSeparator = StringHelper::getDecimalSeparator();
|
||||
if ($thousandsSeparator !== ',' || $decimalSeparator !== '.') {
|
||||
$value = str_replace(['.', ',', "\u{fffd}"], ["\u{fffd}", '.', ','], $value);
|
||||
}
|
||||
|
||||
return $value;
|
||||
}
|
||||
}
|
||||
211
lib/PhpSpreadsheet/Style/NumberFormat/DateFormatter.php
Normal file
211
lib/PhpSpreadsheet/Style/NumberFormat/DateFormatter.php
Normal file
|
|
@ -0,0 +1,211 @@
|
|||
<?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,
|
||||
];
|
||||
|
||||
private static function tryInterval(bool &$seekingBracket, string &$block, mixed $value, string $format): void
|
||||
{
|
||||
if ($seekingBracket) {
|
||||
if (str_contains($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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static function format(mixed $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 */
|
||||
$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 */
|
||||
$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);
|
||||
$microseconds = (int) $dateObj->format('u');
|
||||
if (str_contains($format, ':s.000')) {
|
||||
$milliseconds = (int) round($microseconds / 1000.0);
|
||||
if ($milliseconds === 1000) {
|
||||
$milliseconds = 0;
|
||||
$dateObj->modify('+1 second');
|
||||
}
|
||||
$dateObj->modify("-$microseconds microseconds");
|
||||
$format = str_replace(':s.000', ':s.' . sprintf('%03d', $milliseconds), $format);
|
||||
} elseif (str_contains($format, ':s.00')) {
|
||||
$centiseconds = (int) round($microseconds / 10000.0);
|
||||
if ($centiseconds === 100) {
|
||||
$centiseconds = 0;
|
||||
$dateObj->modify('+1 second');
|
||||
}
|
||||
$dateObj->modify("-$microseconds microseconds");
|
||||
$format = str_replace(':s.00', ':s.' . sprintf('%02d', $centiseconds), $format);
|
||||
} elseif (str_contains($format, ':s.0')) {
|
||||
$deciseconds = (int) round($microseconds / 100000.0);
|
||||
if ($deciseconds === 10) {
|
||||
$deciseconds = 0;
|
||||
$dateObj->modify('+1 second');
|
||||
}
|
||||
$dateObj->modify("-$microseconds microseconds");
|
||||
$format = str_replace(':s.0', ':s.' . sprintf('%1d', $deciseconds), $format);
|
||||
} else { // no fractional second
|
||||
if ($microseconds >= 500000) {
|
||||
$dateObj->modify('+1 second');
|
||||
}
|
||||
$dateObj->modify("-$microseconds microseconds");
|
||||
}
|
||||
|
||||
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('\\', str_split($matches[1]));
|
||||
}
|
||||
}
|
||||
188
lib/PhpSpreadsheet/Style/NumberFormat/Formatter.php
Normal file
188
lib/PhpSpreadsheet/Style/NumberFormat/Formatter.php
Normal file
|
|
@ -0,0 +1,188 @@
|
|||
<?php
|
||||
|
||||
namespace PhpOffice\PhpSpreadsheet\Style\NumberFormat;
|
||||
|
||||
use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
|
||||
use PhpOffice\PhpSpreadsheet\Reader\Xls\Color\BIFF8;
|
||||
use PhpOffice\PhpSpreadsheet\RichText\RichText;
|
||||
use PhpOffice\PhpSpreadsheet\Style\Color;
|
||||
use PhpOffice\PhpSpreadsheet\Style\NumberFormat;
|
||||
|
||||
class Formatter extends BaseFormatter
|
||||
{
|
||||
/**
|
||||
* 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';
|
||||
|
||||
private static function splitFormatComparison(
|
||||
mixed $value,
|
||||
?string $condition,
|
||||
mixed $comparisonValue,
|
||||
string $defaultCondition,
|
||||
mixed $defaultComparisonValue
|
||||
): bool {
|
||||
if (!$condition) {
|
||||
$condition = $defaultCondition;
|
||||
$comparisonValue = $defaultComparisonValue;
|
||||
}
|
||||
|
||||
return match ($condition) {
|
||||
'>' => $value > $comparisonValue,
|
||||
'<' => $value < $comparisonValue,
|
||||
'<=' => $value <= $comparisonValue,
|
||||
'<>' => $value != $comparisonValue,
|
||||
'=' => $value == $comparisonValue,
|
||||
default => $value >= $comparisonValue,
|
||||
};
|
||||
}
|
||||
|
||||
private static function splitFormatForSectionSelection(array $sections, mixed $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);
|
||||
// Colour could be a named colour, or a numeric index entry in the colour-palette
|
||||
$color_regex = '/\\[(' . implode('|', Color::NAMED_COLORS) . '|color\\s*(\\d+))\\]/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)) {
|
||||
if (isset($matches[2])) {
|
||||
$colors[$idx] = '#' . BIFF8::lookup((int) $matches[2] + 7)['rgb'];
|
||||
} else {
|
||||
$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, string $format, ?array $callBack = null): string
|
||||
{
|
||||
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 self::adjustSeparators((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',
|
||||
fn (array $matches): string => 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
|
||||
if (
|
||||
// Check for date/time characters (not inside quotes)
|
||||
(preg_match('/(\[\$[A-Z]*-[0-9A-F]*\])*[hmsdy](?=(?:[^"]|"[^"]*")*$)/miu', $format))
|
||||
// A date/time with a decimal time shouldn't have a digit placeholder before the decimal point
|
||||
&& (preg_match('/[0\?#]\.(?![^\[]*\])/miu', $format) === 0)
|
||||
) {
|
||||
// datetime format
|
||||
$value = DateFormatter::format($value, $format);
|
||||
} else {
|
||||
if (str_starts_with($format, '"') && str_ends_with($format, '"') && 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);
|
||||
}
|
||||
|
||||
return str_replace(chr(0x00), '.', $value);
|
||||
}
|
||||
}
|
||||
69
lib/PhpSpreadsheet/Style/NumberFormat/FractionFormatter.php
Normal file
69
lib/PhpSpreadsheet/Style/NumberFormat/FractionFormatter.php
Normal file
|
|
@ -0,0 +1,69 @@
|
|||
<?php
|
||||
|
||||
namespace PhpOffice\PhpSpreadsheet\Style\NumberFormat;
|
||||
|
||||
use PhpOffice\PhpSpreadsheet\Calculation\MathTrig;
|
||||
|
||||
class FractionFormatter extends BaseFormatter
|
||||
{
|
||||
public static function format(mixed $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 */
|
||||
$fractionDivisor = MathTrig\Gcd::evaluate((int) $decimalPart, $decimalDivisor);
|
||||
}
|
||||
|
||||
$adjustedDecimalPart = (int) round((int) $decimalPart / $fractionDivisor, 0);
|
||||
$adjustedDecimalDivisor = $decimalDivisor / $fractionDivisor;
|
||||
|
||||
if ((str_contains($formatIntegerPart, '0'))) {
|
||||
return "{$sign}{$integerPart} {$adjustedDecimalPart}/{$adjustedDecimalDivisor}";
|
||||
} elseif ((str_contains($formatIntegerPart, '#'))) {
|
||||
if ($integerPart == 0) {
|
||||
return "{$sign}{$adjustedDecimalPart}/{$adjustedDecimalDivisor}";
|
||||
}
|
||||
|
||||
return "{$sign}{$integerPart} {$adjustedDecimalPart}/{$adjustedDecimalDivisor}";
|
||||
} elseif ((str_starts_with($formatIntegerPart, '? ?'))) {
|
||||
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;
|
||||
}
|
||||
}
|
||||
311
lib/PhpSpreadsheet/Style/NumberFormat/NumberFormatter.php
Normal file
311
lib/PhpSpreadsheet/Style/NumberFormat/NumberFormatter.php
Normal file
|
|
@ -0,0 +1,311 @@
|
|||
<?php
|
||||
|
||||
namespace PhpOffice\PhpSpreadsheet\Style\NumberFormat;
|
||||
|
||||
use PhpOffice\PhpSpreadsheet\Shared\StringHelper;
|
||||
use PhpOffice\PhpSpreadsheet\Style\NumberFormat;
|
||||
|
||||
class NumberFormatter extends BaseFormatter
|
||||
{
|
||||
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)),
|
||||
];
|
||||
}
|
||||
|
||||
private static function processComplexNumberFormatMask(mixed $number, string $mask): string
|
||||
{
|
||||
/** @var string $result */
|
||||
$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 */
|
||||
$numberFloat = $number;
|
||||
$blockValue = sprintf("%0{$size}d", fmod($numberFloat, $divisor));
|
||||
$number = floor($numberFloat / $divisor);
|
||||
$mask = substr_replace($mask, $blockValue, $offset, $size);
|
||||
}
|
||||
/** @var string $numberString */
|
||||
$numberString = $number;
|
||||
if ($number > 0) {
|
||||
$mask = substr_replace($mask, $numberString, $offset, 0);
|
||||
}
|
||||
$result = $mask;
|
||||
}
|
||||
|
||||
return self::makeString($result);
|
||||
}
|
||||
|
||||
private static function complexNumberFormatMask(mixed $number, string $mask, bool $splitOnPoint = true): string
|
||||
{
|
||||
/** @var float $numberFloat */
|
||||
$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 && str_contains($mask, '.') && str_contains($number, '.')) {
|
||||
$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;
|
||||
}
|
||||
|
||||
private static function formatStraightNumericValue(mixed $value, string $format, array $matches, bool $useThousands): string
|
||||
{
|
||||
/** @var float $valueFloat */
|
||||
$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 (str_contains($result, 'E')) {
|
||||
// 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 */
|
||||
$valueFloat = $value;
|
||||
$value = self::adjustSeparators(sprintf($sprintf_pattern, round($valueFloat, strlen($right))));
|
||||
|
||||
return self::pregReplace(self::NUMBER_REGEX, $value, $format);
|
||||
}
|
||||
|
||||
public static function format(mixed $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 = (str_contains($format, '?'));
|
||||
|
||||
// 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 (
|
||||
(str_contains((string) $value, '0.'))
|
||||
&& ((str_contains($baseFormat, '#.')) || (str_contains($baseFormat, '?.')))
|
||||
) {
|
||||
$value = preg_replace('/(\b)0\.|([^\d])0\./', '${2}.', (string) $value);
|
||||
}
|
||||
|
||||
return (string) $value;
|
||||
}
|
||||
|
||||
private static function makeString(array|string $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 (str_contains($postDecimal, '?')) {
|
||||
$value = str_pad(rtrim($value, '0. '), $length, ' ', STR_PAD_RIGHT);
|
||||
}
|
||||
if (str_contains($preDecimal, '?')) {
|
||||
$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;
|
||||
}
|
||||
}
|
||||
|
|
@ -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 */
|
||||
$valueFloat = $value;
|
||||
|
||||
return self::adjustSeparators(sprintf($mask, round($valueFloat, $decimalPartSize)));
|
||||
}
|
||||
}
|
||||
102
lib/PhpSpreadsheet/Style/NumberFormat/Wizard/Accounting.php
Normal file
102
lib/PhpSpreadsheet/Style/NumberFormat/Wizard/Accounting.php
Normal 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,
|
||||
bool $stripLeadingRLM = self::DEFAULT_STRIP_LEADING_RLM
|
||||
) {
|
||||
$this->setCurrencyCode($currencyCode);
|
||||
$this->setThousandsSeparator($thousandsSeparator);
|
||||
$this->setDecimals($decimals);
|
||||
$this->setCurrencySymbolPosition($currencySymbolPosition);
|
||||
$this->setCurrencySymbolSpacing($currencySymbolSpacing);
|
||||
$this->setLocale($locale);
|
||||
$this->stripLeadingRLM = $stripLeadingRLM;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws Exception if the Intl extension and ICU version don't support Accounting formats
|
||||
*/
|
||||
protected function getLocaleFormat(): string
|
||||
{
|
||||
if (self::icuVersion() < 53.0) {
|
||||
// @codeCoverageIgnoreStart
|
||||
throw new Exception('The Intl extension does not support Accounting Formats without ICU 53');
|
||||
// @codeCoverageIgnoreEnd
|
||||
}
|
||||
|
||||
// Scrutinizer does not recognize CURRENCY_ACCOUNTING
|
||||
$formatter = new Locale($this->fullLocale, NumberFormatter::CURRENCY_ACCOUNTING);
|
||||
$mask = $formatter->format($this->stripLeadingRLM);
|
||||
if ($this->decimals === 0) {
|
||||
$mask = (string) preg_replace('/\.0+/miu', '', $mask);
|
||||
}
|
||||
|
||||
return str_replace('¤', $this->formatCurrencyCode(), $mask);
|
||||
}
|
||||
|
||||
public static 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
|
||||
) ? "\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
|
||||
);
|
||||
}
|
||||
}
|
||||
125
lib/PhpSpreadsheet/Style/NumberFormat/Wizard/Currency.php
Normal file
125
lib/PhpSpreadsheet/Style/NumberFormat/Wizard/Currency.php
Normal file
|
|
@ -0,0 +1,125 @@
|
|||
<?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;
|
||||
|
||||
protected const DEFAULT_STRIP_LEADING_RLM = false;
|
||||
|
||||
protected bool $stripLeadingRLM = self::DEFAULT_STRIP_LEADING_RLM;
|
||||
|
||||
/**
|
||||
* @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).
|
||||
* @param bool $stripLeadingRLM remove leading RLM added with
|
||||
* ICU 72.1+.
|
||||
*
|
||||
* @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,
|
||||
bool $stripLeadingRLM = self::DEFAULT_STRIP_LEADING_RLM
|
||||
) {
|
||||
$this->setCurrencyCode($currencyCode);
|
||||
$this->setThousandsSeparator($thousandsSeparator);
|
||||
$this->setDecimals($decimals);
|
||||
$this->setCurrencySymbolPosition($currencySymbolPosition);
|
||||
$this->setCurrencySymbolSpacing($currencySymbolSpacing);
|
||||
$this->setLocale($locale);
|
||||
$this->stripLeadingRLM = $stripLeadingRLM;
|
||||
}
|
||||
|
||||
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 setStripLeadingRLM(bool $stripLeadingRLM): void
|
||||
{
|
||||
$this->stripLeadingRLM = $stripLeadingRLM;
|
||||
}
|
||||
|
||||
protected function getLocaleFormat(): string
|
||||
{
|
||||
$formatter = new Locale($this->fullLocale, NumberFormatter::CURRENCY);
|
||||
$mask = $formatter->format($this->stripLeadingRLM);
|
||||
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
|
||||
);
|
||||
}
|
||||
}
|
||||
125
lib/PhpSpreadsheet/Style/NumberFormat/Wizard/Date.php
Normal file
125
lib/PhpSpreadsheet/Style/NumberFormat/Wizard/Date.php
Normal file
|
|
@ -0,0 +1,125 @@
|
|||
<?php
|
||||
|
||||
namespace PhpOffice\PhpSpreadsheet\Style\NumberFormat\Wizard;
|
||||
|
||||
class Date extends DateTimeWizard
|
||||
{
|
||||
/**
|
||||
* Year (4 digits), e.g. 2023.
|
||||
*/
|
||||
public const YEAR_FULL = 'yyyy';
|
||||
|
||||
/**
|
||||
* Year (last 2 digits), e.g. 23.
|
||||
*/
|
||||
public const YEAR_SHORT = 'yy';
|
||||
|
||||
public const MONTH_FIRST_LETTER = 'mmmmm';
|
||||
/**
|
||||
* Month name, long form, e.g. January.
|
||||
*/
|
||||
public const MONTH_NAME_FULL = 'mmmm';
|
||||
/**
|
||||
* Month name, short form, e.g. Jan.
|
||||
*/
|
||||
public const MONTH_NAME_SHORT = 'mmm';
|
||||
/**
|
||||
* Month number with a leading zero if required, e.g. 01.
|
||||
*/
|
||||
public const MONTH_NUMBER_LONG = 'mm';
|
||||
|
||||
/**
|
||||
* Month number without a leading zero, e.g. 1.
|
||||
*/
|
||||
public const MONTH_NUMBER_SHORT = 'm';
|
||||
|
||||
/**
|
||||
* Day of the week, full form, e.g. Tuesday.
|
||||
*/
|
||||
public const WEEKDAY_NAME_LONG = 'dddd';
|
||||
|
||||
/**
|
||||
* Day of the week, short form, e.g. Tue.
|
||||
*/
|
||||
public const WEEKDAY_NAME_SHORT = 'ddd';
|
||||
|
||||
/**
|
||||
* Day number with a leading zero, e.g. 03.
|
||||
*/
|
||||
public const DAY_NUMBER_LONG = 'dd';
|
||||
|
||||
/**
|
||||
* Day number without a leading zero, e.g. 3.
|
||||
*/
|
||||
public const DAY_NUMBER_SHORT = 'd';
|
||||
|
||||
protected const DATE_BLOCKS = [
|
||||
self::YEAR_FULL,
|
||||
self::YEAR_SHORT,
|
||||
self::MONTH_FIRST_LETTER,
|
||||
self::MONTH_NAME_FULL,
|
||||
self::MONTH_NAME_SHORT,
|
||||
self::MONTH_NUMBER_LONG,
|
||||
self::MONTH_NUMBER_SHORT,
|
||||
self::WEEKDAY_NAME_LONG,
|
||||
self::WEEKDAY_NAME_SHORT,
|
||||
self::DAY_NUMBER_LONG,
|
||||
self::DAY_NUMBER_SHORT,
|
||||
];
|
||||
|
||||
public const SEPARATOR_DASH = '-';
|
||||
public const SEPARATOR_DOT = '.';
|
||||
public const SEPARATOR_SLASH = '/';
|
||||
public const SEPARATOR_SPACE_NONBREAKING = "\u{a0}";
|
||||
public const SEPARATOR_SPACE = ' ';
|
||||
|
||||
protected const DATE_DEFAULT = [
|
||||
self::YEAR_FULL,
|
||||
self::MONTH_NUMBER_LONG,
|
||||
self::DAY_NUMBER_LONG,
|
||||
];
|
||||
|
||||
/**
|
||||
* @var string[]
|
||||
*/
|
||||
protected array $separators;
|
||||
|
||||
/**
|
||||
* @var string[]
|
||||
*/
|
||||
protected array $formatBlocks;
|
||||
|
||||
/**
|
||||
* @param null|string|string[] $separators
|
||||
* If you want to use the same separator for all format blocks, then it can be passed as a string literal;
|
||||
* if you wish to use different separators, then they should be passed as an array.
|
||||
* If you want to use only a single format block, then pass a null as the separator argument
|
||||
*/
|
||||
public function __construct($separators = self::SEPARATOR_DASH, string ...$formatBlocks)
|
||||
{
|
||||
$separators ??= self::SEPARATOR_DASH;
|
||||
$formatBlocks = (count($formatBlocks) === 0) ? self::DATE_DEFAULT : $formatBlocks;
|
||||
|
||||
$this->separators = $this->padSeparatorArray(
|
||||
is_array($separators) ? $separators : [$separators],
|
||||
count($formatBlocks) - 1
|
||||
);
|
||||
$this->formatBlocks = array_map([$this, 'mapFormatBlocks'], $formatBlocks);
|
||||
}
|
||||
|
||||
private function mapFormatBlocks(string $value): string
|
||||
{
|
||||
// Any date masking codes are returned as lower case values
|
||||
if (in_array(mb_strtolower($value), self::DATE_BLOCKS, true)) {
|
||||
return mb_strtolower($value);
|
||||
}
|
||||
|
||||
// Wrap any string literals in quotes, so that they're clearly defined as string literals
|
||||
return $this->wrapLiteral($value);
|
||||
}
|
||||
|
||||
public function format(): string
|
||||
{
|
||||
return implode('', array_map([$this, 'intersperse'], $this->formatBlocks, $this->separators));
|
||||
}
|
||||
}
|
||||
47
lib/PhpSpreadsheet/Style/NumberFormat/Wizard/DateTime.php
Normal file
47
lib/PhpSpreadsheet/Style/NumberFormat/Wizard/DateTime.php
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
<?php
|
||||
|
||||
namespace PhpOffice\PhpSpreadsheet\Style\NumberFormat\Wizard;
|
||||
|
||||
class DateTime extends DateTimeWizard
|
||||
{
|
||||
/**
|
||||
* @var string[]
|
||||
*/
|
||||
protected array $separators;
|
||||
|
||||
/**
|
||||
* @var array<DateTimeWizard|string>
|
||||
*/
|
||||
protected array $formatBlocks;
|
||||
|
||||
/**
|
||||
* @param null|string|string[] $separators
|
||||
* If you want to use only a single format block, then pass a null as the separator argument
|
||||
* @param DateTimeWizard|string ...$formatBlocks
|
||||
*/
|
||||
public function __construct($separators, ...$formatBlocks)
|
||||
{
|
||||
$this->separators = $this->padSeparatorArray(
|
||||
is_array($separators) ? $separators : [$separators],
|
||||
count($formatBlocks) - 1
|
||||
);
|
||||
$this->formatBlocks = array_map([$this, 'mapFormatBlocks'], $formatBlocks);
|
||||
}
|
||||
|
||||
private function mapFormatBlocks(DateTimeWizard|string $value): string
|
||||
{
|
||||
// Any date masking codes are returned as lower case values
|
||||
if (is_object($value)) {
|
||||
// We can't explicitly test for Stringable until PHP >= 8.0
|
||||
return $value;
|
||||
}
|
||||
|
||||
// Wrap any string literals in quotes, so that they're clearly defined as string literals
|
||||
return $this->wrapLiteral($value);
|
||||
}
|
||||
|
||||
public function format(): string
|
||||
{
|
||||
return implode('', array_map([$this, 'intersperse'], $this->formatBlocks, $this->separators));
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,46 @@
|
|||
<?php
|
||||
|
||||
namespace PhpOffice\PhpSpreadsheet\Style\NumberFormat\Wizard;
|
||||
|
||||
use Stringable;
|
||||
|
||||
abstract class DateTimeWizard implements Stringable, Wizard
|
||||
{
|
||||
protected const NO_ESCAPING_NEEDED = "$+-/():!^&'~{}<>= ";
|
||||
|
||||
protected function padSeparatorArray(array $separators, int $count): array
|
||||
{
|
||||
$lastSeparator = array_pop($separators);
|
||||
|
||||
return $separators + array_fill(0, $count, $lastSeparator);
|
||||
}
|
||||
|
||||
protected function escapeSingleCharacter(string $value): string
|
||||
{
|
||||
if (str_contains(self::NO_ESCAPING_NEEDED, $value)) {
|
||||
return $value;
|
||||
}
|
||||
|
||||
return "\\{$value}";
|
||||
}
|
||||
|
||||
protected function wrapLiteral(string $value): string
|
||||
{
|
||||
if (mb_strlen($value, 'UTF-8') === 1) {
|
||||
return $this->escapeSingleCharacter($value);
|
||||
}
|
||||
|
||||
// Wrap any other string literals in quotes, so that they're clearly defined as string literals
|
||||
return '"' . str_replace('"', '""', $value) . '"';
|
||||
}
|
||||
|
||||
protected function intersperse(string $formatBlock, ?string $separator): string
|
||||
{
|
||||
return "{$formatBlock}{$separator}";
|
||||
}
|
||||
|
||||
public function __toString(): string
|
||||
{
|
||||
return $this->format();
|
||||
}
|
||||
}
|
||||
153
lib/PhpSpreadsheet/Style/NumberFormat/Wizard/Duration.php
Normal file
153
lib/PhpSpreadsheet/Style/NumberFormat/Wizard/Duration.php
Normal file
|
|
@ -0,0 +1,153 @@
|
|||
<?php
|
||||
|
||||
namespace PhpOffice\PhpSpreadsheet\Style\NumberFormat\Wizard;
|
||||
|
||||
class Duration extends DateTimeWizard
|
||||
{
|
||||
public const DAYS_DURATION = 'd';
|
||||
|
||||
/**
|
||||
* Hours as a duration (can exceed 24), e.g. 29.
|
||||
*/
|
||||
public const HOURS_DURATION = '[h]';
|
||||
|
||||
/**
|
||||
* Hours without a leading zero, e.g. 9.
|
||||
*/
|
||||
public const HOURS_SHORT = 'h';
|
||||
|
||||
/**
|
||||
* Hours with a leading zero, e.g. 09.
|
||||
*/
|
||||
public const HOURS_LONG = 'hh';
|
||||
|
||||
/**
|
||||
* Minutes as a duration (can exceed 60), e.g. 109.
|
||||
*/
|
||||
public const MINUTES_DURATION = '[m]';
|
||||
|
||||
/**
|
||||
* Minutes without a leading zero, e.g. 5.
|
||||
*/
|
||||
public const MINUTES_SHORT = 'm';
|
||||
|
||||
/**
|
||||
* Minutes with a leading zero, e.g. 05.
|
||||
*/
|
||||
public const MINUTES_LONG = 'mm';
|
||||
|
||||
/**
|
||||
* Seconds as a duration (can exceed 60), e.g. 129.
|
||||
*/
|
||||
public const SECONDS_DURATION = '[s]';
|
||||
|
||||
/**
|
||||
* Seconds without a leading zero, e.g. 2.
|
||||
*/
|
||||
public const SECONDS_SHORT = 's';
|
||||
|
||||
/**
|
||||
* Seconds with a leading zero, e.g. 02.
|
||||
*/
|
||||
public const SECONDS_LONG = 'ss';
|
||||
|
||||
protected const DURATION_BLOCKS = [
|
||||
self::DAYS_DURATION,
|
||||
self::HOURS_DURATION,
|
||||
self::HOURS_LONG,
|
||||
self::HOURS_SHORT,
|
||||
self::MINUTES_DURATION,
|
||||
self::MINUTES_LONG,
|
||||
self::MINUTES_SHORT,
|
||||
self::SECONDS_DURATION,
|
||||
self::SECONDS_LONG,
|
||||
self::SECONDS_SHORT,
|
||||
];
|
||||
|
||||
protected const DURATION_MASKS = [
|
||||
self::DAYS_DURATION => self::DAYS_DURATION,
|
||||
self::HOURS_DURATION => self::HOURS_SHORT,
|
||||
self::MINUTES_DURATION => self::MINUTES_LONG,
|
||||
self::SECONDS_DURATION => self::SECONDS_LONG,
|
||||
];
|
||||
|
||||
protected const DURATION_DEFAULTS = [
|
||||
self::HOURS_LONG => self::HOURS_DURATION,
|
||||
self::HOURS_SHORT => self::HOURS_DURATION,
|
||||
self::MINUTES_LONG => self::MINUTES_DURATION,
|
||||
self::MINUTES_SHORT => self::MINUTES_DURATION,
|
||||
self::SECONDS_LONG => self::SECONDS_DURATION,
|
||||
self::SECONDS_SHORT => self::SECONDS_DURATION,
|
||||
];
|
||||
|
||||
public const SEPARATOR_COLON = ':';
|
||||
public const SEPARATOR_SPACE_NONBREAKING = "\u{a0}";
|
||||
public const SEPARATOR_SPACE = ' ';
|
||||
|
||||
public const DURATION_DEFAULT = [
|
||||
self::HOURS_DURATION,
|
||||
self::MINUTES_LONG,
|
||||
self::SECONDS_LONG,
|
||||
];
|
||||
|
||||
/**
|
||||
* @var string[]
|
||||
*/
|
||||
protected array $separators;
|
||||
|
||||
/**
|
||||
* @var string[]
|
||||
*/
|
||||
protected array $formatBlocks;
|
||||
|
||||
protected bool $durationIsSet = false;
|
||||
|
||||
/**
|
||||
* @param null|string|string[] $separators
|
||||
* If you want to use the same separator for all format blocks, then it can be passed as a string literal;
|
||||
* if you wish to use different separators, then they should be passed as an array.
|
||||
* If you want to use only a single format block, then pass a null as the separator argument
|
||||
*/
|
||||
public function __construct($separators = self::SEPARATOR_COLON, string ...$formatBlocks)
|
||||
{
|
||||
$separators ??= self::SEPARATOR_COLON;
|
||||
$formatBlocks = (count($formatBlocks) === 0) ? self::DURATION_DEFAULT : $formatBlocks;
|
||||
|
||||
$this->separators = $this->padSeparatorArray(
|
||||
is_array($separators) ? $separators : [$separators],
|
||||
count($formatBlocks) - 1
|
||||
);
|
||||
$this->formatBlocks = array_map([$this, 'mapFormatBlocks'], $formatBlocks);
|
||||
|
||||
if ($this->durationIsSet === false) {
|
||||
// We need at least one duration mask, so if none has been set we change the first mask element
|
||||
// to a duration.
|
||||
$this->formatBlocks[0] = self::DURATION_DEFAULTS[mb_strtolower($this->formatBlocks[0])];
|
||||
}
|
||||
}
|
||||
|
||||
private function mapFormatBlocks(string $value): string
|
||||
{
|
||||
// Any duration masking codes are returned as lower case values
|
||||
if (in_array(mb_strtolower($value), self::DURATION_BLOCKS, true)) {
|
||||
if (array_key_exists(mb_strtolower($value), self::DURATION_MASKS)) {
|
||||
if ($this->durationIsSet) {
|
||||
// We should only have a single duration mask, the first defined in the mask set,
|
||||
// so convert any additional duration masks to standard time masks.
|
||||
$value = self::DURATION_MASKS[mb_strtolower($value)];
|
||||
}
|
||||
$this->durationIsSet = true;
|
||||
}
|
||||
|
||||
return mb_strtolower($value);
|
||||
}
|
||||
|
||||
// Wrap any string literals in quotes, so that they're clearly defined as string literals
|
||||
return $this->wrapLiteral($value);
|
||||
}
|
||||
|
||||
public function format(): string
|
||||
{
|
||||
return implode('', array_map([$this, 'intersperse'], $this->formatBlocks, $this->separators));
|
||||
}
|
||||
}
|
||||
39
lib/PhpSpreadsheet/Style/NumberFormat/Wizard/Locale.php
Normal file
39
lib/PhpSpreadsheet/Style/NumberFormat/Wizard/Locale.php
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
<?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(bool $stripRlm = true): string
|
||||
{
|
||||
$str = $this->formatter->getPattern();
|
||||
|
||||
return ($stripRlm && str_starts_with($str, "\xe2\x80\x8f")) ? substr($str, 3) : $str;
|
||||
}
|
||||
}
|
||||
57
lib/PhpSpreadsheet/Style/NumberFormat/Wizard/Number.php
Normal file
57
lib/PhpSpreadsheet/Style/NumberFormat/Wizard/Number.php
Normal 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
|
||||
);
|
||||
}
|
||||
}
|
||||
81
lib/PhpSpreadsheet/Style/NumberFormat/Wizard/NumberBase.php
Normal file
81
lib/PhpSpreadsheet/Style/NumberFormat/Wizard/NumberBase.php
Normal file
|
|
@ -0,0 +1,81 @@
|
|||
<?php
|
||||
|
||||
namespace PhpOffice\PhpSpreadsheet\Style\NumberFormat\Wizard;
|
||||
|
||||
use NumberFormatter;
|
||||
use PhpOffice\PhpSpreadsheet\Exception;
|
||||
use PhpOffice\PhpSpreadsheet\Style\NumberFormat;
|
||||
use Stringable;
|
||||
|
||||
abstract class NumberBase implements Stringable
|
||||
{
|
||||
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();
|
||||
}
|
||||
}
|
||||
40
lib/PhpSpreadsheet/Style/NumberFormat/Wizard/Percentage.php
Normal file
40
lib/PhpSpreadsheet/Style/NumberFormat/Wizard/Percentage.php
Normal 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);
|
||||
}
|
||||
}
|
||||
33
lib/PhpSpreadsheet/Style/NumberFormat/Wizard/Scientific.php
Normal file
33
lib/PhpSpreadsheet/Style/NumberFormat/Wizard/Scientific.php
Normal 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);
|
||||
}
|
||||
}
|
||||
105
lib/PhpSpreadsheet/Style/NumberFormat/Wizard/Time.php
Normal file
105
lib/PhpSpreadsheet/Style/NumberFormat/Wizard/Time.php
Normal file
|
|
@ -0,0 +1,105 @@
|
|||
<?php
|
||||
|
||||
namespace PhpOffice\PhpSpreadsheet\Style\NumberFormat\Wizard;
|
||||
|
||||
class Time extends DateTimeWizard
|
||||
{
|
||||
/**
|
||||
* Hours without a leading zero, e.g. 9.
|
||||
*/
|
||||
public const HOURS_SHORT = 'h';
|
||||
|
||||
/**
|
||||
* Hours with a leading zero, e.g. 09.
|
||||
*/
|
||||
public const HOURS_LONG = 'hh';
|
||||
|
||||
/**
|
||||
* Minutes without a leading zero, e.g. 5.
|
||||
*/
|
||||
public const MINUTES_SHORT = 'm';
|
||||
|
||||
/**
|
||||
* Minutes with a leading zero, e.g. 05.
|
||||
*/
|
||||
public const MINUTES_LONG = 'mm';
|
||||
|
||||
/**
|
||||
* Seconds without a leading zero, e.g. 2.
|
||||
*/
|
||||
public const SECONDS_SHORT = 's';
|
||||
|
||||
/**
|
||||
* Seconds with a leading zero, e.g. 02.
|
||||
*/
|
||||
public const SECONDS_LONG = 'ss';
|
||||
|
||||
public const MORNING_AFTERNOON = 'AM/PM';
|
||||
|
||||
protected const TIME_BLOCKS = [
|
||||
self::HOURS_LONG,
|
||||
self::HOURS_SHORT,
|
||||
self::MINUTES_LONG,
|
||||
self::MINUTES_SHORT,
|
||||
self::SECONDS_LONG,
|
||||
self::SECONDS_SHORT,
|
||||
self::MORNING_AFTERNOON,
|
||||
];
|
||||
|
||||
public const SEPARATOR_COLON = ':';
|
||||
public const SEPARATOR_SPACE_NONBREAKING = "\u{a0}";
|
||||
public const SEPARATOR_SPACE = ' ';
|
||||
|
||||
protected const TIME_DEFAULT = [
|
||||
self::HOURS_LONG,
|
||||
self::MINUTES_LONG,
|
||||
self::SECONDS_LONG,
|
||||
];
|
||||
|
||||
/**
|
||||
* @var string[]
|
||||
*/
|
||||
protected array $separators;
|
||||
|
||||
/**
|
||||
* @var string[]
|
||||
*/
|
||||
protected array $formatBlocks;
|
||||
|
||||
/**
|
||||
* @param null|string|string[] $separators
|
||||
* If you want to use the same separator for all format blocks, then it can be passed as a string literal;
|
||||
* if you wish to use different separators, then they should be passed as an array.
|
||||
* If you want to use only a single format block, then pass a null as the separator argument
|
||||
*/
|
||||
public function __construct($separators = self::SEPARATOR_COLON, string ...$formatBlocks)
|
||||
{
|
||||
$separators ??= self::SEPARATOR_COLON;
|
||||
$formatBlocks = (count($formatBlocks) === 0) ? self::TIME_DEFAULT : $formatBlocks;
|
||||
|
||||
$this->separators = $this->padSeparatorArray(
|
||||
is_array($separators) ? $separators : [$separators],
|
||||
count($formatBlocks) - 1
|
||||
);
|
||||
$this->formatBlocks = array_map([$this, 'mapFormatBlocks'], $formatBlocks);
|
||||
}
|
||||
|
||||
private function mapFormatBlocks(string $value): string
|
||||
{
|
||||
// Any date masking codes are returned as lower case values
|
||||
// except for AM/PM, which is set to uppercase
|
||||
if (in_array(mb_strtolower($value), self::TIME_BLOCKS, true)) {
|
||||
return mb_strtolower($value);
|
||||
} elseif (mb_strtoupper($value) === self::MORNING_AFTERNOON) {
|
||||
return mb_strtoupper($value);
|
||||
}
|
||||
|
||||
// Wrap any string literals in quotes, so that they're clearly defined as string literals
|
||||
return $this->wrapLiteral($value);
|
||||
}
|
||||
|
||||
public function format(): string
|
||||
{
|
||||
return implode('', array_map([$this, 'intersperse'], $this->formatBlocks, $this->separators));
|
||||
}
|
||||
}
|
||||
8
lib/PhpSpreadsheet/Style/NumberFormat/Wizard/Wizard.php
Normal file
8
lib/PhpSpreadsheet/Style/NumberFormat/Wizard/Wizard.php
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
<?php
|
||||
|
||||
namespace PhpOffice\PhpSpreadsheet\Style\NumberFormat\Wizard;
|
||||
|
||||
interface Wizard
|
||||
{
|
||||
public function format(): string;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue