Passed
Push — master ( d88efc...653645 )
by
unknown
12:43 queued 21s
created

ConditionalStyles::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 4
c 0
b 0
f 0
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 10
cc 1
nc 1
nop 4
crap 1
1
<?php
2
3
namespace PhpOffice\PhpSpreadsheet\Reader\Xlsx;
4
5
use PhpOffice\PhpSpreadsheet\Reader\Xlsx\Styles as StyleReader;
6
use PhpOffice\PhpSpreadsheet\Style\Color;
7
use PhpOffice\PhpSpreadsheet\Style\Conditional;
8
use PhpOffice\PhpSpreadsheet\Style\ConditionalFormatting\ConditionalColorScale;
9
use PhpOffice\PhpSpreadsheet\Style\ConditionalFormatting\ConditionalDataBar;
10
use PhpOffice\PhpSpreadsheet\Style\ConditionalFormatting\ConditionalFormattingRuleExtension;
11
use PhpOffice\PhpSpreadsheet\Style\ConditionalFormatting\ConditionalFormatValueObject;
12
use PhpOffice\PhpSpreadsheet\Style\ConditionalFormatting\ConditionalIconSet;
13
use PhpOffice\PhpSpreadsheet\Style\ConditionalFormatting\IconSetValues;
14
use PhpOffice\PhpSpreadsheet\Style\Style as Style;
15
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
16
use SimpleXMLElement;
17
use stdClass;
18
19
class ConditionalStyles
20
{
21
    private Worksheet $worksheet;
22
23
    private SimpleXMLElement $worksheetXml;
24
25
    /** @var string[] */
26
    private array $ns;
27
28
    /** @var Style[] */
29
    private array $dxfs;
30
31
    private StyleReader $styleReader;
32 240
33
    /** @param Style[] $dxfs */
34 240
    public function __construct(Worksheet $workSheet, SimpleXMLElement $worksheetXml, array $dxfs, StyleReader $styleReader)
35 240
    {
36 240
        $this->worksheet = $workSheet;
37 240
        $this->worksheetXml = $worksheetXml;
38
        $this->dxfs = $dxfs;
39
        $this->styleReader = $styleReader;
40 222
    }
41
42 222
    public function load(): void
43
    {
44 222
        $selectedCells = $this->worksheet->getSelectedCells();
45 222
46 222
        $this->setConditionalStyles(
47 222
            $this->worksheet,
48 222
            $this->readConditionalStyles($this->worksheetXml),
49
            $this->worksheetXml->extLst
50 222
        );
51
52
        $this->worksheet->setSelectedCells($selectedCells);
53 200
    }
54
55 200
    public function loadFromExt(): void
56
    {
57 200
        $selectedCells = $this->worksheet->getSelectedCells();
58 200
59 200
        $this->ns = $this->worksheetXml->getNamespaces(true);
60 200
        $this->setConditionalsFromExt(
61
            $this->readConditionalsFromExt($this->worksheetXml->extLst)
62 200
        );
63
64
        $this->worksheet->setSelectedCells($selectedCells);
65
    }
66 200
67
    /** @param Conditional[][] $conditionals */
68 200
    private function setConditionalsFromExt(array $conditionals): void
69 176
    {
70
        foreach ($conditionals as $conditionalRange => $cfRules) {
71
            ksort($cfRules);
72 176
            // Priority is used as the key for sorting; but may not start at 0,
73 176
            // so we use array_values to reset the index after sorting.
74
            $this->worksheet->getStyle($conditionalRange)
75
                ->setConditionalStyles(array_values($cfRules));
76
        }
77
    }
78 200
79
    /** @return array<string, array<int, Conditional>> */
80 200
    private function readConditionalsFromExt(SimpleXMLElement $extLst): array
81 200
    {
82
        $conditionals = [];
83
        if (!isset($extLst->ext)) {
84
            return $conditionals;
85 200
        }
86 200
87 200
        foreach ($extLst->ext as $extlstcond) {
88 200
            $extAttrs = $extlstcond->attributes() ?? [];
89 22
            $extUri = (string) ($extAttrs['uri'] ?? '');
90
            if ($extUri !== '{78C0D931-6437-407d-A8EE-F0AAD7539E65}') {
91 178
                continue;
92 178
            }
93
            $conditionalFormattingRuleXml = $extlstcond->children($this->ns['x14']);
94
            if (!$conditionalFormattingRuleXml->conditionalFormattings) {
95
                return [];
96 178
            }
97 178
98 178
            foreach ($conditionalFormattingRuleXml->children($this->ns['x14']) as $extFormattingXml) {
99
                $extFormattingRangeXml = $extFormattingXml->children($this->ns['xm']);
100
                if (!$extFormattingRangeXml->sqref) {
101
                    continue;
102 178
                }
103 178
104
                $sqref = (string) $extFormattingRangeXml->sqref;
105 178
                $extCfRuleXml = $extFormattingXml->cfRule;
106 178
107
                $attributes = $extCfRuleXml->attributes();
108
                if (!$attributes) {
109 178
                    continue;
110
                }
111 178
                $conditionType = (string) $attributes->type;
112 178
                if (
113
                    !Conditional::isValidConditionType($conditionType)
114 2
                    || $conditionType === Conditional::CONDITION_DATABAR
115
                ) {
116
                    continue;
117 176
                }
118
119 176
                $priority = (int) $attributes->priority;
120 176
121 176
                $conditional = $this->readConditionalRuleFromExt($extCfRuleXml, $attributes);
122 176
                $cfStyle = $this->readStyleFromExt($extCfRuleXml);
123
                $conditional->setStyle($cfStyle);
124
                $conditionals[$sqref][$priority] = $conditional;
125
            }
126 200
        }
127
128
        return $conditionals;
129 176
    }
130
131 176
    private function readConditionalRuleFromExt(SimpleXMLElement $cfRuleXml, SimpleXMLElement $attributes): Conditional
132 176
    {
133 176
        $conditionType = (string) $attributes->type;
134
        $operatorType = (string) $attributes->operator;
135 176
        $priority = (int) (string) $attributes->priority;
136 176
137 176
        $operands = [];
138
        foreach ($cfRuleXml->children($this->ns['xm']) as $cfRuleOperandsXml) {
139
            $operands[] = (string) $cfRuleOperandsXml;
140 176
        }
141 176
142 176
        $conditional = new Conditional();
143 176
        $conditional->setConditionType($conditionType);
144
        $conditional->setOperatorType($operatorType);
145 176
        $conditional->setPriority($priority);
146 176
        if (
147 176
            $conditionType === Conditional::CONDITION_CONTAINSTEXT
148 176
            || $conditionType === Conditional::CONDITION_NOTCONTAINSTEXT
149 176
            || $conditionType === Conditional::CONDITION_BEGINSWITH
150
            || $conditionType === Conditional::CONDITION_ENDSWITH
151 176
            || $conditionType === Conditional::CONDITION_TIMEPERIOD
152
        ) {
153 176
            $conditional->setText(array_pop($operands) ?? '');
154
        }
155 176
        $conditional->setConditions($operands);
156
157
        return $conditional;
158 176
    }
159
160 176
    private function readStyleFromExt(SimpleXMLElement $extCfRuleXml): Style
161 176
    {
162 176
        $cfStyle = new Style(false, true);
163
        if ($extCfRuleXml->dxf) {
164 176
            $styleXML = $extCfRuleXml->dxf->children();
165
166
            if ($styleXML->borders) {
167 176
                $this->styleReader->readBorderStyle($cfStyle->getBorders(), $styleXML->borders);
168 176
            }
169
            if ($styleXML->fill) {
170
                $this->styleReader->readFillStyle($cfStyle->getFill(), $styleXML->fill);
171
            }
172 176
        }
173
174
        return $cfStyle;
175
    }
176 222
177
    /** @return mixed[] */
178 222
    private function readConditionalStyles(SimpleXMLElement $xmlSheet): array
179 222
    {
180 222
        $conditionals = [];
181 222
        foreach ($xmlSheet->conditionalFormatting as $conditional) {
182 222
            foreach ($conditional->cfRule as $cfRule) {
183
                if (Conditional::isValidConditionType((string) $cfRule['type']) && (!isset($cfRule['dxfId']) || isset($this->dxfs[(int) ($cfRule['dxfId'])]))) {
184
                    $conditionals[(string) $conditional['sqref']][(int) ($cfRule['priority'])] = $cfRule;
185
                } elseif ((string) $cfRule['type'] == Conditional::CONDITION_DATABAR) {
186
                    $conditionals[(string) $conditional['sqref']][(int) ($cfRule['priority'])] = $cfRule;
187
                }
188
            }
189 222
        }
190
191
        return $conditionals;
192
    }
193 222
194
    /** @param mixed[] $conditionals */
195 222
    private function setConditionalStyles(Worksheet $worksheet, array $conditionals, SimpleXMLElement $xmlExtLst): void
196
    {
197 222
        foreach ($conditionals as $cellRangeReference => $cfRules) {
198 222
            /** @var mixed[] $cfRules */
199
            ksort($cfRules); // no longer needed for Xlsx, but helps Xls
200
            $conditionalStyles = $this->readStyleRules($cfRules, $xmlExtLst);
201
202
            // Extract all cell references in $cellRangeReference
203 222
            // N.B. In Excel UI, intersection is space and union is comma.
204
            // But in Xml, intersection is comma and union is space.
205 222
            $cellRangeReference = str_replace(['$', ' ', ',', '^'], ['', '^', ' ', ','], strtoupper($cellRangeReference));
206 222
207 222
            foreach ($conditionalStyles as $cs) {
208 6
                $scale = $cs->getColorScale();
209
                if ($scale !== null) {
210
                    $scale->setSqRef($cellRangeReference, $worksheet);
211 222
                }
212
            }
213
            $worksheet->getStyle($cellRangeReference)->setConditionalStyles($conditionalStyles);
214
        }
215
    }
216
217
    /**
218
     * @param mixed[] $cfRules
219
     *
220 222
     * @return Conditional[]
221
     */
222
    private function readStyleRules(array $cfRules, SimpleXMLElement $extLst): array
223 222
    {
224 222
        /** @var ConditionalFormattingRuleExtension[] */
225
        $conditionalFormattingRuleExtensions = ConditionalFormattingRuleExtension::parseExtLstXml($extLst);
226
        $conditionalStyles = [];
227 222
228 222
        /** @var SimpleXMLElement $cfRule */
229 222
        foreach ($cfRules as $cfRule) {
230 222
            $objConditional = new Conditional();
231 222
            $objConditional->setConditionType((string) $cfRule['type']);
232 222
            $objConditional->setOperatorType((string) $cfRule['operator']);
233
            $objConditional->setPriority((int) (string) $cfRule['priority']);
234 222
            $objConditional->setNoFormatSet(!isset($cfRule['dxfId']));
235 182
236 220
            if ((string) $cfRule['text'] != '') {
237 176
                $objConditional->setText((string) $cfRule['text']);
238
            } elseif ((string) $cfRule['timePeriod'] != '') {
239
                $objConditional->setText((string) $cfRule['timePeriod']);
240 222
            }
241 5
242
            if (isset($cfRule['stopIfTrue']) && (int) $cfRule['stopIfTrue'] === 1) {
243
                $objConditional->setStopIfTrue(true);
244 222
            }
245 215
246 215
            if (count($cfRule->formula) >= 1) {
247 215
                foreach ($cfRule->formula as $formulax) {
248 215
                    $formula = (string) $formulax;
249 1
                    $formula = str_replace(['_xlfn.', '_xlws.'], '', $formula);
250 215
                    if ($formula === 'TRUE') {
251 1
                        $objConditional->addCondition(true);
252
                    } elseif ($formula === 'FALSE') {
253 214
                        $objConditional->addCondition(false);
254
                    } else {
255
                        $objConditional->addCondition($formula);
256
                    }
257 186
                }
258
            } else {
259
                $objConditional->addCondition('');
260 222
            }
261 3
262 3
            if (isset($cfRule->dataBar)) {
263 3
                $objConditional->setDataBar(
264 219
                    $this->readDataBarOfConditionalRule($cfRule, $conditionalFormattingRuleExtensions)
265 6
                );
266 6
            } elseif (isset($cfRule->colorScale)) {
267 6
                $objConditional->setColorScale(
268 214
                    $this->readColorScale($cfRule)
269 214
                );
270
            } elseif (isset($cfRule->iconSet)) {
271
                $objConditional->setIconSet($this->readIconSet($cfRule));
272 222
            } elseif (isset($cfRule['dxfId'])) {
273
                $objConditional->setStyle(clone $this->dxfs[(int) ($cfRule['dxfId'])]);
274
            }
275 222
276
            $conditionalStyles[] = $objConditional;
277
        }
278
279 3
        return $conditionalStyles;
280
    }
281 3
282
    /** @param ConditionalFormattingRuleExtension[] $conditionalFormattingRuleExtensions */
283 3
    private function readDataBarOfConditionalRule(SimpleXMLElement $cfRule, array $conditionalFormattingRuleExtensions): ConditionalDataBar
284 2
    {
285
        $dataBar = new ConditionalDataBar();
286
        //dataBar attribute
287
        if (isset($cfRule->dataBar['showValue'])) {
288
            $dataBar->setShowValue((bool) $cfRule->dataBar['showValue']);
289 3
        }
290 3
291 3
        //dataBar children
292
        //conditionalFormatValueObjects
293 3
        $cfvoXml = $cfRule->dataBar->cfvo;
294 3
        $cfvoIndex = 0;
295
        foreach ((count($cfvoXml) > 1 ? $cfvoXml : [$cfvoXml]) as $cfvo) { //* @phpstan-ignore-line
296 3
            /** @var SimpleXMLElement $cfvo */
297 3
            if ($cfvoIndex === 0) {
298
                $dataBar->setMinimumConditionalFormatValueObject(new ConditionalFormatValueObject((string) $cfvo['type'], (string) $cfvo['val']));
299 3
            }
300
            if ($cfvoIndex === 1) {
301
                $dataBar->setMaximumConditionalFormatValueObject(new ConditionalFormatValueObject((string) $cfvo['type'], (string) $cfvo['val']));
302
            }
303 3
            ++$cfvoIndex;
304 3
        }
305
306
        //color
307 3
        if (isset($cfRule->dataBar->color)) {
308
            $dataBar->setColor($this->styleReader->readColor($cfRule->dataBar->color));
309 3
        }
310
        //extLst
311
        $this->readDataBarExtLstOfConditionalRule($dataBar, $cfRule, $conditionalFormattingRuleExtensions);
312 6
313
        return $dataBar;
314 6
    }
315
316 6
    private function readColorScale(SimpleXMLElement|stdClass $cfRule): ConditionalColorScale
317 6
    {
318 6
        $colorScale = new ConditionalColorScale();
319 6
        /** @var SimpleXMLElement $cfRule */
320 6
        $count = count($cfRule->colorScale->cfvo);
321 6
        $idx = 0;
322 6
        foreach ($cfRule->colorScale->cfvo as $cfvoXml) {
323 6
            $attr = $cfvoXml->attributes() ?? [];
324 6
            $type = (string) ($attr['type'] ?? '');
325 5
            $val = $attr['val'] ?? null;
326
            if ($idx === 0) {
327 6
                $method = 'setMinimumConditionalFormatValueObject';
328
            } elseif ($idx === 1 && $count === 3) {
329 6
                $method = 'setMidpointConditionalFormatValueObject';
330 6
            } else {
331
                $method = 'setMaximumConditionalFormatValueObject';
332 3
            }
333
            if ($type !== 'formula') {
334 6
                $colorScale->$method(new ConditionalFormatValueObject($type, $val));
335
            } else {
336 6
                $colorScale->$method(new ConditionalFormatValueObject($type, null, $val));
337 6
            }
338 6
            ++$idx;
339 6
        }
340 6
        $idx = 0;
341 6
        foreach ($cfRule->colorScale->color as $color) {
342 5
            $rgb = $this->styleReader->readColor($color);
343
            if ($idx === 0) {
344 6
                $colorScale->setMinimumColor(new Color($rgb));
345
            } elseif ($idx === 1 && $count === 3) {
346 6
                $colorScale->setMidpointColor(new Color($rgb));
347
            } else {
348
                $colorScale->setMaximumColor(new Color($rgb));
349 6
            }
350
            ++$idx;
351
        }
352
353 3
        return $colorScale;
354
    }
355 3
356 2
    private function readIconSet(SimpleXMLElement $cfRule): ConditionalIconSet
357 2
    {
358
        $iconSet = new ConditionalIconSet();
359 2
360 2
        if (isset($cfRule->iconSet['iconSet'])) {
361 2
            $iconSet->setIconSetType(IconSetValues::from($cfRule->iconSet['iconSet']));
362
        }
363
        if (isset($cfRule->iconSet['reverse'])) {
364
            $iconSet->setReverse('1' === (string) $cfRule->iconSet['reverse']);
365
        }
366
        if (isset($cfRule->iconSet['showValue'])) {
367
            $iconSet->setShowValue('1' === (string) $cfRule->iconSet['showValue']);
368
        }
369
        if (isset($cfRule->iconSet['custom'])) {
370
            $iconSet->setCustom('1' === (string) $cfRule->iconSet['custom']);
371
        }
372
373
        $cfvos = [];
374
        foreach ($cfRule->iconSet->cfvo as $cfvoXml) {
375
            $type = (string) $cfvoXml['type'];
376
            $value = (string) ($cfvoXml['val'] ?? '');
377
            $cfvo = new ConditionalFormatValueObject($type, $value);
378
            if (isset($cfvoXml['gte'])) {
379
                $cfvo->setGreaterThanOrEqual('1' === (string) $cfvoXml['gte']);
380
            }
381
            $cfvos[] = $cfvo;
382
        }
383
        $iconSet->setCfvos($cfvos);
384
385
        // TODO: The cfIcon element is not implemented yet.
386
387
        return $iconSet;
388
    }
389
390
    /** @param ConditionalFormattingRuleExtension[] $conditionalFormattingRuleExtensions */
391
    private function readDataBarExtLstOfConditionalRule(ConditionalDataBar $dataBar, SimpleXMLElement $cfRule, array $conditionalFormattingRuleExtensions): void
392
    {
393
        if (isset($cfRule->extLst)) {
394
            $ns = $cfRule->extLst->getNamespaces(true);
395
            foreach ((count($cfRule->extLst) > 0 ? $cfRule->extLst->ext : [$cfRule->extLst->ext]) as $ext) { //* @phpstan-ignore-line
396
                /** @var SimpleXMLElement $ext */
397
                $extId = (string) $ext->children($ns['x14'])->id;
398
                if (isset($conditionalFormattingRuleExtensions[$extId]) && (string) $ext['uri'] === '{B025F937-C7B1-47D3-B67F-A62EFF666E3E}') {
399
                    $dataBar->setConditionalFormattingRuleExt($conditionalFormattingRuleExtensions[$extId]);
400
                }
401
            }
402
        }
403
    }
404
}
405