package and depencies
This commit is contained in:
12
vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Style/NumberFormat/BaseFormatter.php
vendored
Normal file
12
vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Style/NumberFormat/BaseFormatter.php
vendored
Normal 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);
|
||||
}
|
||||
}
|
||||
182
vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Style/NumberFormat/DateFormatter.php
vendored
Normal file
182
vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Style/NumberFormat/DateFormatter.php
vendored
Normal 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]));
|
||||
}
|
||||
}
|
||||
165
vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Style/NumberFormat/Formatter.php
vendored
Normal file
165
vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Style/NumberFormat/Formatter.php
vendored
Normal file
@@ -0,0 +1,165 @@
|
||||
<?php
|
||||
|
||||
namespace PhpOffice\PhpSpreadsheet\Style\NumberFormat;
|
||||
|
||||
use PhpOffice\PhpSpreadsheet\Style\Color;
|
||||
use PhpOffice\PhpSpreadsheet\Style\NumberFormat;
|
||||
|
||||
class Formatter
|
||||
{
|
||||
private static function splitFormatCompare($value, $cond, $val, $dfcond, $dfval)
|
||||
{
|
||||
if (!$cond) {
|
||||
$cond = $dfcond;
|
||||
$val = $dfval;
|
||||
}
|
||||
switch ($cond) {
|
||||
case '>':
|
||||
return $value > $val;
|
||||
|
||||
case '<':
|
||||
return $value < $val;
|
||||
|
||||
case '<=':
|
||||
return $value <= $val;
|
||||
|
||||
case '<>':
|
||||
return $value != $val;
|
||||
|
||||
case '=':
|
||||
return $value == $val;
|
||||
}
|
||||
|
||||
return $value >= $val;
|
||||
}
|
||||
|
||||
private static function splitFormat($sections, $value)
|
||||
{
|
||||
// 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]
|
||||
$cnt = count($sections);
|
||||
$color_regex = '/\\[(' . implode('|', Color::NAMED_COLORS) . ')\\]/mui';
|
||||
$cond_regex = '/\\[(>|>=|<|<=|=|<>)([+-]?\\d+([.]\\d+)?)\\]/';
|
||||
$colors = ['', '', '', '', ''];
|
||||
$condops = ['', '', '', '', ''];
|
||||
$condvals = [0, 0, 0, 0, 0];
|
||||
for ($idx = 0; $idx < $cnt; ++$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)) {
|
||||
$condops[$idx] = $matches[1];
|
||||
$condvals[$idx] = $matches[2];
|
||||
$sections[$idx] = (string) preg_replace($cond_regex, '', $sections[$idx]);
|
||||
}
|
||||
}
|
||||
$color = $colors[0];
|
||||
$format = $sections[0];
|
||||
$absval = $value;
|
||||
switch ($cnt) {
|
||||
case 2:
|
||||
$absval = abs($value);
|
||||
if (!self::splitFormatCompare($value, $condops[0], $condvals[0], '>=', 0)) {
|
||||
$color = $colors[1];
|
||||
$format = $sections[1];
|
||||
}
|
||||
|
||||
break;
|
||||
case 3:
|
||||
case 4:
|
||||
$absval = abs($value);
|
||||
if (!self::splitFormatCompare($value, $condops[0], $condvals[0], '>', 0)) {
|
||||
if (self::splitFormatCompare($value, $condops[1], $condvals[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 mixed $value Value to format
|
||||
* @param string $format Format code, see = NumberFormat::FORMAT_*
|
||||
* @param array $callBack Callback function for additional formatting of string
|
||||
*
|
||||
* @return string Formatted string
|
||||
*/
|
||||
public static function toFormattedString($value, $format, $callBack = null)
|
||||
{
|
||||
// For now we do not treat strings although section 4 of a format code affects strings
|
||||
if (!is_numeric($value)) {
|
||||
return $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 $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('/(;)(?=(?:[^"]|"[^"]*")*$)/u', $format);
|
||||
|
||||
[$colors, $format, $value] = self::splitFormat($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, $matches)) {
|
||||
// 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
|
||||
$value = PercentageFormatter::format($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;
|
||||
}
|
||||
}
|
||||
67
vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Style/NumberFormat/FractionFormatter.php
vendored
Normal file
67
vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Style/NumberFormat/FractionFormatter.php
vendored
Normal file
@@ -0,0 +1,67 @@
|
||||
<?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;
|
||||
|
||||
/** @var float */
|
||||
$GCD = MathTrig\Gcd::evaluate($decimalPart, $decimalDivisor);
|
||||
/** @var float */
|
||||
$decimalPartx = $decimalPart;
|
||||
|
||||
$adjustedDecimalPart = $decimalPartx / $GCD;
|
||||
$adjustedDecimalDivisor = $decimalDivisor / $GCD;
|
||||
|
||||
if ((strpos($format, '0') !== false)) {
|
||||
return "{$sign}{$integerPart} {$adjustedDecimalPart}/{$adjustedDecimalDivisor}";
|
||||
} elseif ((strpos($format, '#') !== false)) {
|
||||
if ($integerPart == 0) {
|
||||
return "{$sign}{$adjustedDecimalPart}/{$adjustedDecimalDivisor}";
|
||||
}
|
||||
|
||||
return "{$sign}{$integerPart} {$adjustedDecimalPart}/{$adjustedDecimalDivisor}";
|
||||
} elseif ((substr($format, 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;
|
||||
}
|
||||
}
|
||||
278
vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Style/NumberFormat/NumberFormatter.php
vendored
Normal file
278
vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Style/NumberFormat/NumberFormatter.php
vendored
Normal file
@@ -0,0 +1,278 @@
|
||||
<?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
|
||||
return sprintf('%5.2E', $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);
|
||||
//}
|
||||
|
||||
// Some non-number strings are quoted, so we'll get rid of the quotes, likewise any positional * symbols
|
||||
$format = self::makeString(str_replace(['"', '*'], '', $format));
|
||||
|
||||
// Find out if we need thousands separator
|
||||
// This is indicated by a comma enclosed by a digit placeholder:
|
||||
// #,# or 0,0
|
||||
$useThousands = (bool) preg_match('/(#,#|0,0)/', $format);
|
||||
if ($useThousands) {
|
||||
$format = self::pregReplace('/0,0/', '00', $format);
|
||||
$format = self::pregReplace('/#,#/', '##', $format);
|
||||
}
|
||||
|
||||
// Scale thousands, millions,...
|
||||
// This is indicated by a number of commas after a digit placeholder:
|
||||
// #, or 0.0,,
|
||||
$scale = 1; // same as no scale
|
||||
$matches = [];
|
||||
if (preg_match('/(#|0)(,+)/', $format, $matches)) {
|
||||
$scale = 1000 ** strlen($matches[2]);
|
||||
|
||||
// strip the commas
|
||||
$format = self::pregReplace('/0,+/', '0', $format);
|
||||
$format = self::pregReplace('/#,+/', '#', $format);
|
||||
}
|
||||
if (preg_match('/#?.*\?\/\?/', $format, $m)) {
|
||||
$value = FractionFormatter::format($value, $format);
|
||||
} else {
|
||||
// Handle the number itself
|
||||
|
||||
// scale number
|
||||
$value = $value / $scale;
|
||||
// Strip #
|
||||
$format = self::pregReplace('/\\#/', '0', $format);
|
||||
// Remove locale code [$-###]
|
||||
$format = self::pregReplace('/\[\$\-.*\]/', '', $format);
|
||||
|
||||
$n = '/\\[[^\\]]+\\]/';
|
||||
$m = self::pregReplace($n, '', $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);
|
||||
} 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);
|
||||
}
|
||||
|
||||
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) ?? '');
|
||||
}
|
||||
}
|
||||
47
vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Style/NumberFormat/PercentageFormatter.php
vendored
Normal file
47
vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Style/NumberFormat/PercentageFormatter.php
vendored
Normal file
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
|
||||
namespace PhpOffice\PhpSpreadsheet\Style\NumberFormat;
|
||||
|
||||
use PhpOffice\PhpSpreadsheet\Style\NumberFormat;
|
||||
|
||||
class PercentageFormatter extends BaseFormatter
|
||||
{
|
||||
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));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user