1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpOffice\PhpSpreadsheet\Style\NumberFormat\Wizard; |
4
|
|
|
|
5
|
|
|
use NumberFormatter; |
6
|
|
|
use PhpOffice\PhpSpreadsheet\Exception; |
7
|
|
|
use PhpOffice\PhpSpreadsheet\Style\NumberFormat; |
8
|
|
|
|
9
|
|
|
abstract class NumberBase |
10
|
|
|
{ |
11
|
|
|
protected const MAX_DECIMALS = 30; |
12
|
|
|
|
13
|
|
|
protected int $decimals = 2; |
14
|
|
|
|
15
|
|
|
protected ?string $locale = null; |
16
|
|
|
|
17
|
|
|
protected ?string $fullLocale = null; |
18
|
|
|
|
19
|
|
|
protected ?string $localeFormat = null; |
20
|
|
|
|
21
|
70 |
|
public function setDecimals(int $decimals = 2, ?string $locale = null): void |
|
|
|
|
22
|
|
|
{ |
23
|
70 |
|
$this->decimals = ($decimals > self::MAX_DECIMALS) ? self::MAX_DECIMALS : max($decimals, 0); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Setting a locale will override any settings defined in this class. |
28
|
|
|
* |
29
|
|
|
* @throws Exception If the locale code is not a valid format |
30
|
|
|
*/ |
31
|
70 |
|
public function setLocale(?string $locale = null): void |
32
|
|
|
{ |
33
|
70 |
|
if ($locale === null) { |
34
|
70 |
|
$this->localeFormat = $this->locale = $this->fullLocale = null; |
35
|
|
|
|
36
|
70 |
|
return; |
37
|
|
|
} |
38
|
|
|
|
39
|
40 |
|
$this->locale = $this->validateLocale($locale); |
40
|
|
|
|
41
|
35 |
|
if (class_exists(NumberFormatter::class)) { |
42
|
35 |
|
$this->localeFormat = $this->getLocaleFormat(); |
43
|
|
|
} |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Stub: should be implemented as a concrete method in concrete wizards. |
48
|
|
|
*/ |
49
|
|
|
abstract protected function getLocaleFormat(): string; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @throws Exception If the locale code is not a valid format |
53
|
|
|
*/ |
54
|
40 |
|
private function validateLocale(string $locale): string |
55
|
|
|
{ |
56
|
40 |
|
if (preg_match(Locale::STRUCTURE, $locale, $matches, PREG_UNMATCHED_AS_NULL) !== 1) { |
57
|
5 |
|
throw new Exception("Invalid locale code '{$locale}'"); |
58
|
|
|
} |
59
|
|
|
|
60
|
35 |
|
['language' => $language, 'script' => $script, 'country' => $country] = $matches; |
61
|
|
|
// Set case and separator to match standardised locale case |
62
|
35 |
|
$language = strtolower($language ?? ''); |
63
|
35 |
|
$script = ($script === null) ? null : ucfirst(strtolower($script)); |
64
|
35 |
|
$country = ($country === null) ? null : strtoupper($country); |
65
|
|
|
|
66
|
35 |
|
$this->fullLocale = implode('-', array_filter([$language, $script, $country])); |
67
|
|
|
|
68
|
35 |
|
return $country === null ? $language : "{$language}-{$country}"; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function format(): string |
72
|
|
|
{ |
73
|
|
|
return NumberFormat::FORMAT_GENERAL; |
74
|
|
|
} |
75
|
|
|
|
76
|
62 |
|
public function __toString(): string |
77
|
|
|
{ |
78
|
62 |
|
return $this->format(); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.