Failed Conditions
Pull Request — master (#4127)
by Owen
39:04 queued 27:50
created

Formatter   A

Complexity

Total Complexity 31

Size/Duplication

Total Lines 182
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 31
eloc 87
dl 0
loc 182
ccs 83
cts 83
cp 1
rs 9.92
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A splitFormatComparison() 0 19 2
B splitFormatForSectionSelection() 0 60 11
D toFormattedString() 0 75 18
1
<?php
2
3
namespace PhpOffice\PhpSpreadsheet\Style\NumberFormat;
4
5
use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
6
use PhpOffice\PhpSpreadsheet\Reader\Xls\Color\BIFF8;
7
use PhpOffice\PhpSpreadsheet\RichText\RichText;
8
use PhpOffice\PhpSpreadsheet\Style\Color;
9
use PhpOffice\PhpSpreadsheet\Style\NumberFormat;
10
11
class Formatter extends BaseFormatter
12
{
13
    /**
14
     * Matches any @ symbol that isn't enclosed in quotes.
15
     */
16
    private const SYMBOL_AT = '/@(?=(?:[^"]*"[^"]*")*[^"]*\Z)/miu';
17
18
    /**
19
     * Matches any ; symbol that isn't enclosed in quotes, for a "section" split.
20
     */
21
    private const SECTION_SPLIT = '/;(?=(?:[^"]*"[^"]*")*[^"]*\Z)/miu';
22
23 191
    private static function splitFormatComparison(
24
        mixed $value,
25
        ?string $condition,
26
        mixed $comparisonValue,
27
        string $defaultCondition,
28
        mixed $defaultComparisonValue
29
    ): bool {
30 191
        if (!$condition) {
31 182
            $condition = $defaultCondition;
32 182
            $comparisonValue = $defaultComparisonValue;
33
        }
34
35 191
        return match ($condition) {
36 91
            '>' => $value > $comparisonValue,
37 59
            '<' => $value < $comparisonValue,
38 2
            '<=' => $value <= $comparisonValue,
39 4
            '<>' => $value != $comparisonValue,
40 4
            '=' => $value == $comparisonValue,
41 191
            default => $value >= $comparisonValue,
42 191
        };
43
    }
44
45
    /** @param float|int|string $value value to be formatted */
46 954
    private static function splitFormatForSectionSelection(array $sections, mixed $value): array
47
    {
48
        // Extract the relevant section depending on whether number is positive, negative, or zero?
49
        // Text not supported yet.
50
        // Here is how the sections apply to various values in Excel:
51
        //   1 section:   [POSITIVE/NEGATIVE/ZERO/TEXT]
52
        //   2 sections:  [POSITIVE/ZERO/TEXT] [NEGATIVE]
53
        //   3 sections:  [POSITIVE/TEXT] [NEGATIVE] [ZERO]
54
        //   4 sections:  [POSITIVE] [NEGATIVE] [ZERO] [TEXT]
55 954
        $sectionCount = count($sections);
56
        // Colour could be a named colour, or a numeric index entry in the colour-palette
57 954
        $color_regex = '/\\[(' . implode('|', Color::NAMED_COLORS) . '|color\\s*(\\d+))\\]/mui';
58 954
        $cond_regex = '/\\[(>|>=|<|<=|=|<>)([+-]?\\d+([.]\\d+)?)\\]/';
59 954
        $colors = ['', '', '', '', ''];
60 954
        $conditionOperations = ['', '', '', '', ''];
61 954
        $conditionComparisonValues = [0, 0, 0, 0, 0];
62 954
        for ($idx = 0; $idx < $sectionCount; ++$idx) {
63 954
            if (preg_match($color_regex, $sections[$idx], $matches)) {
64 50
                if (isset($matches[2])) {
65 14
                    $colors[$idx] = '#' . BIFF8::lookup((int) $matches[2] + 7)['rgb'];
66
                } else {
67 36
                    $colors[$idx] = $matches[0];
68
                }
69 50
                $sections[$idx] = (string) preg_replace($color_regex, '', $sections[$idx]);
70
            }
71 954
            if (preg_match($cond_regex, $sections[$idx], $matches)) {
72 9
                $conditionOperations[$idx] = $matches[1];
73 9
                $conditionComparisonValues[$idx] = $matches[2];
74 9
                $sections[$idx] = (string) preg_replace($cond_regex, '', $sections[$idx]);
75
            }
76
        }
77 954
        $color = $colors[0];
78 954
        $format = $sections[0];
79 954
        $absval = $value;
80
        switch ($sectionCount) {
81 954
            case 2:
82 95
                $absval = abs($value);
83 95
                if (!self::splitFormatComparison($value, $conditionOperations[0], $conditionComparisonValues[0], '>=', 0)) {
84 46
                    $color = $colors[1];
85 46
                    $format = $sections[1];
86
                }
87
88 95
                break;
89 865
            case 3:
90 851
            case 4:
91 96
                $absval = abs($value);
92 96
                if (!self::splitFormatComparison($value, $conditionOperations[0], $conditionComparisonValues[0], '>', 0)) {
93 61
                    if (self::splitFormatComparison($value, $conditionOperations[1], $conditionComparisonValues[1], '<', 0)) {
94 48
                        $color = $colors[1];
95 48
                        $format = $sections[1];
96
                    } else {
97 29
                        $color = $colors[2];
98 29
                        $format = $sections[2];
99
                    }
100
                }
101
102 96
                break;
103
        }
104
105 954
        return [$color, $format, $absval];
106
    }
107
108
    /**
109
     * Convert a value in a pre-defined format to a PHP string.
110
     *
111
     * @param null|array|bool|float|int|RichText|string $value Value to format
112
     * @param string $format Format code: see = self::FORMAT_* for predefined values;
113
     *                          or can be any valid MS Excel custom format string
114
     * @param ?array $callBack Callback function for additional formatting of string
115
     *
116
     * @return string Formatted string
117
     */
118 1193
    public static function toFormattedString($value, string $format, ?array $callBack = null): string
119
    {
120 1193
        while (is_array($value)) {
121 3
            $value = array_shift($value);
122
        }
123 1193
        if (is_bool($value)) {
124 10
            return $value ? Calculation::getTRUE() : Calculation::getFALSE();
125
        }
126
        // For now we do not treat strings in sections, although section 4 of a format code affects strings
127
        // Process a single block format code containing @ for text substitution
128 1193
        if (preg_match(self::SECTION_SPLIT, $format) === 0 && preg_match(self::SYMBOL_AT, $format) === 1) {
129 11
            return str_replace('"', '', preg_replace(self::SYMBOL_AT, (string) $value, $format) ?? '');
130
        }
131
132
        // If we have a text value, return it "as is"
133 1183
        if (!is_numeric($value)) {
134 215
            return (string) $value;
135
        }
136
137
        // For 'General' format code, we just pass the value although this is not entirely the way Excel does it,
138
        // it seems to round numbers to a total of 10 digits.
139 1072
        if (($format === NumberFormat::FORMAT_GENERAL) || ($format === NumberFormat::FORMAT_TEXT)) {
140 146
            return self::adjustSeparators((string) $value);
141
        }
142
143
        // Ignore square-$-brackets prefix in format string, like "[$-411]ge.m.d", "[$-010419]0%", etc
144 954
        $format = (string) preg_replace('/^\[\$-[^\]]*\]/', '', $format);
145
146 954
        $format = (string) preg_replace_callback(
147 954
            '/(["])(?:(?=(\\\\?))\\2.)*?\\1/u',
148 954
            fn (array $matches): string => str_replace('.', chr(0x00), $matches[0]),
149 954
            $format
150 954
        );
151
152
        // Convert any other escaped characters to quoted strings, e.g. (\T to "T")
153 954
        $format = (string) preg_replace('/(\\\(((.)(?!((AM\/PM)|(A\/P))))|([^ ])))(?=(?:[^"]|"[^"]*")*$)/ui', '"${2}"', $format);
154
155
        // Get the sections, there can be up to four sections, separated with a semi-colon (but only if not a quoted literal)
156 954
        $sections = preg_split(self::SECTION_SPLIT, $format) ?: [];
157
158 954
        [$colors, $format, $value] = self::splitFormatForSectionSelection($sections, $value);
159
160
        // In Excel formats, "_" is used to add spacing,
161
        //    The following character indicates the size of the spacing, which we can't do in HTML, so we just use a standard space
162 954
        $format = (string) preg_replace('/_.?/ui', ' ', $format);
163
164
        // Let's begin inspecting the format and converting the value to a formatted string
165
        if (
166
            //  Check for date/time characters (not inside quotes)
167 954
            (preg_match('/(\[\$[A-Z]*-[0-9A-F]*\])*[hmsdy](?=(?:[^"]|"[^"]*")*$)/miu', $format))
168
            //  Look out for Currency formats Issue 4124
169 954
            && !(preg_match('/\[\$[A-Z]{3}\]/miu', $format))
170
            // A date/time with a decimal time shouldn't have a digit placeholder before the decimal point
171 954
            && (preg_match('/[0\?#]\.(?![^\[]*\])/miu', $format) === 0)
172
        ) {
173
            // datetime format
174 154
            $value = DateFormatter::format($value, $format);
175
        } else {
176 822
            if (str_starts_with($format, '"') && str_ends_with($format, '"') && substr_count($format, '"') === 2) {
177 14
                $value = substr($format, 1, -1);
178 808
            } elseif (preg_match('/[0#, ]%/', $format)) {
179
                // % number format - avoid weird '-0' problem
180 141
                $value = PercentageFormatter::format(0 + (float) $value, $format);
181
            } else {
182 669
                $value = NumberFormatter::format($value, $format);
183
            }
184
        }
185
186
        // Additional formatting provided by callback function
187 954
        if ($callBack !== null) {
188 392
            [$writerInstance, $function] = $callBack;
189 392
            $value = $writerInstance->$function($value, $colors);
190
        }
191
192 954
        return str_replace(chr(0x00), '.', $value);
193
    }
194
}
195