Passed
Pull Request — master (#3384)
by Mark
16:40 queued 06:29
created

Accounting::getLocaleFormat()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4.128

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 9
c 2
b 0
f 0
dl 0
loc 18
ccs 8
cts 10
cp 0.8
rs 9.9666
cc 4
nc 4
nop 0
crap 4.128
1
<?php
2
3
namespace PhpOffice\PhpSpreadsheet\Style\NumberFormat\Wizard;
4
5
use NumberFormatter;
6
use PhpOffice\PhpSpreadsheet\Exception;
7
8
class Accounting extends Currency
9
{
10
    /**
11
     * @param string $currencyCode the currency symbol or code to display for this mask
12
     * @param int $decimals number of decimal places to display, in the range 0-30
13
     * @param bool $thousandsSeparator indicator whether the thousands separator should be used, or not
14
     * @param bool $currencySymbolPosition indicates whether the currency symbol comes before or after the value
15
     *              Possible values are Currency::LEADING_SYMBOL and Currency::TRAILING_SYMBOL
16
     * @param bool $currencySymbolSpacing indicates whether there is spacing between the currency symbol and the value
17
     *              Possible values are Currency::SYMBOL_WITH_SPACING and Currency::SYMBOL_WITHOUT_SPACING
18
     * @param ?string $locale Set the locale for the currency format; or leave as the default null.
19
     *          If provided, Locale values must be a valid formatted locale string (e.g. 'en-GB', 'fr', uz-Arab-AF).
20
     *          Note that setting a locale will override any other settings defined in this class
21
     *          other than the currency code; or decimals (unless the decimals value is set to 0).
22
     *
23
     * @throws Exception If a provided locale code is not a valid format
24
     */
25 26
    public function __construct(
26
        string $currencyCode = '$',
27
        int $decimals = 2,
28
        bool $thousandsSeparator = true,
29
        bool $currencySymbolPosition = self::LEADING_SYMBOL,
30
        bool $currencySymbolSpacing = self::SYMBOL_WITHOUT_SPACING,
31
        ?string $locale = null
32
    ) {
33 26
        $this->setCurrencyCode($currencyCode);
34 26
        $this->setThousandsSeparator($thousandsSeparator);
35 26
        $this->setDecimals($decimals);
36 26
        $this->setCurrencySymbolPosition($currencySymbolPosition);
37 26
        $this->setCurrencySymbolSpacing($currencySymbolSpacing);
38 26
        $this->setLocale($locale);
39
    }
40
41
    /**
42
     * @throws Exception if the Intl extension and ICU version don't support Accounting formats
43
     */
44 19
    protected function getLocaleFormat(): string
45
    {
46 19
        if (version_compare(PHP_VERSION, '7.4.1', '<')) {
47
            throw new Exception('The Intl extension does not support Accounting Formats below PHP 7.4.1');
48
        }
49
50 19
        if ($this->icuVersion() < 53.0) {
51
            throw new Exception('The Intl extension does not support Accounting Formats without ICU 53');
52
        }
53
54
        // Scrutinizer does not recognize CURRENCY_ACCOUNTING
55 19
        $formatter = new Locale($this->fullLocale, NumberFormatter::CURRENCY_ACCOUNTING);
1 ignored issue
show
Bug introduced by
The constant NumberFormatter::CURRENCY_ACCOUNTING was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
56 18
        $mask = $formatter->format();
57 18
        if ($this->decimals === 0) {
58 9
            $mask = (string) preg_replace('/\.0*/', '', $mask);
59
        }
60
61 18
        return str_replace('¤', $this->formatCurrencyCode(), $mask);
62
    }
63
64 19
    private function icuVersion(): float
65
    {
66 19
        [$major, $minor] = explode('.', INTL_ICU_VERSION);
67
68 19
        return (float) "{$major}.{$minor}";
69
    }
70
71 24
    private function formatCurrencyCode(): string
72
    {
73 24
        if ($this->locale === null) {
74 6
            return $this->currencyCode . '*';
75
        }
76
77 18
        return "[\${$this->currencyCode}-{$this->locale}]";
78
    }
79
80 24
    public function format(): string
81
    {
82 24
        if ($this->localeFormat !== null) {
83 18
            return $this->localeFormat;
84
        }
85
86 6
        return sprintf(
87 6
            '_-%s%s%s0%s%s%s_-',
88 6
            $this->currencySymbolPosition === self::LEADING_SYMBOL ? $this->formatCurrencyCode() : null,
89 6
            (
90 6
                $this->currencySymbolPosition === self::LEADING_SYMBOL &&
91 6
                $this->currencySymbolSpacing === self::SYMBOL_WITH_SPACING
92 6
            ) ? ' ' : '',
93 6
            $this->thousandsSeparator ? '#,##' : null,
94 6
            $this->decimals > 0 ? '.' . str_repeat('0', $this->decimals) : null,
95 6
            (
96 6
                $this->currencySymbolPosition === self::TRAILING_SYMBOL &&
97 6
                $this->currencySymbolSpacing === self::SYMBOL_WITH_SPACING
98 6
            ) ? ' ' : '',
99 6
            $this->currencySymbolPosition === self::TRAILING_SYMBOL ? $this->formatCurrencyCode() : null
100 6
        );
101
    }
102
}
103