Skip to content

Commit acdcb0b

Browse files
committed
Refactoring and simplification of replacement code
1 parent f468e78 commit acdcb0b

File tree

1 file changed

+34
-25
lines changed

1 file changed

+34
-25
lines changed

src/PhpSpreadsheet/Style/NumberFormat/NumberFormatter.php

Lines changed: 34 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -208,29 +208,8 @@ public static function format($value, string $format): string
208208

209209
$baseFormat = $format;
210210

211-
// Find out if we need thousands separator
212-
// This is indicated by a comma enclosed by a digit placeholder:
213-
// #,# or 0,0
214-
$useThousands = (bool) preg_match('/(#,#|0,0|\?,\?)/', $format);
215-
if ($useThousands) {
216-
$format = self::pregReplace('/0,0/', '00', $format);
217-
$format = self::pregReplace('/#,#/', '##', $format);
218-
$format = self::pregReplace('/\?,\?/', '??', $format);
219-
}
220-
221-
// Scale thousands, millions,...
222-
// This is indicated by a number of commas after a digit placeholder:
223-
// #, or 0.0,,
224-
$scale = 1; // same as no scale
225-
$matches = [];
226-
if (preg_match('/(#|0|\?)(,+)/', $format, $matches)) {
227-
$scale = 1000 ** strlen($matches[2]);
228-
229-
// strip the commas
230-
$format = self::pregReplace('/0,+/', '0', $format);
231-
$format = self::pregReplace('/#,+/', '#', $format);
232-
$format = self::pregReplace('/\?,+/', '?', $format);
233-
}
211+
$useThousands = self::areThousandsRequired($format);
212+
$scale = self::scaleThousandsMillions($format);
234213

235214
if (preg_match('/#?.*\?\/(\?+|\d+)/', $format)) {
236215
$value = FractionFormatter::format($value, $format);
@@ -240,9 +219,9 @@ public static function format($value, string $format): string
240219
$value = $value / $scale;
241220
$paddingPlaceholder = (strpos($format, '?') !== false);
242221

243-
// Strip #
222+
// Replace # or ? with 0
244223
$format = self::pregReplace('/[\\#\?](?=(?:[^"]*"[^"]*")*[^"]*\Z)/', '0', $format);
245-
// Remove locale code [$-###]
224+
// Remove locale code [$-###] for an LCID
246225
$format = self::pregReplace('/\[\$\-.*\]/', '', $format);
247226

248227
$n = '/\\[[^\\]]+\\]/';
@@ -311,4 +290,34 @@ public static function padValue(string $value, string $baseFormat): string
311290

312291
return $value;
313292
}
293+
294+
/**
295+
* Find out if we need thousands separator
296+
* This is indicated by a comma enclosed by a digit placeholders: #, 0 or ?
297+
*/
298+
public static function areThousandsRequired(string &$format): bool
299+
{
300+
$useThousands = (bool) preg_match('/([#\?0]),([#\?0])/', $format);
301+
if ($useThousands) {
302+
$format = self::pregReplace('/([#\?0]),([#\?0])/', '${1}${2}', $format);
303+
}
304+
305+
return $useThousands;
306+
}
307+
308+
/**
309+
* Scale thousands, millions,...
310+
* This is indicated by a number of commas after a digit placeholder: #, or 0.0,, or ?,.
311+
*/
312+
public static function scaleThousandsMillions(string &$format): int
313+
{
314+
$scale = 1; // same as no scale
315+
if (preg_match('/(#|0|\?)(,+)/', $format, $matches)) {
316+
$scale = 1000 ** strlen($matches[2]);
317+
// strip the commas
318+
$format = self::pregReplace('/([#\?0]),+/', '${1}', $format);
319+
}
320+
321+
return $scale;
322+
}
314323
}

0 commit comments

Comments
 (0)