Passed
Push — master ( f9b1b7...3922d9 )
by
unknown
18:56 queued 11:14
created

getDataBarExt()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
ccs 2
cts 2
cp 1
crap 1
1
<?php
2
3
namespace PhpOffice\PhpSpreadsheet\Style\ConditionalFormatting;
4
5
use PhpOffice\PhpSpreadsheet\Style\Conditional;
6
use SimpleXMLElement;
7
8
class ConditionalFormattingRuleExtension
9
{
10
    const CONDITION_EXTENSION_DATABAR = 'dataBar';
11
12
    private string $id;
13
14
    /** @var string Conditional Formatting Rule */
15
    private string $cfRule;
16
17
    private ConditionalDataBarExtension $dataBar;
18
19
    /** @var string Sequence of References */
20
    private string $sqref = '';
21
22
    /**
23
     * ConditionalFormattingRuleExtension constructor.
24
     */
25 2
    public function __construct(?string $id = null, string $cfRule = self::CONDITION_EXTENSION_DATABAR)
26
    {
27 2
        if (null === $id) {
28
            $this->id = '{' . $this->generateUuid() . '}';
29
        } else {
30 2
            $this->id = $id;
31
        }
32 2
        $this->cfRule = $cfRule;
33
    }
34
35
    private function generateUuid(): string
36
    {
37
        $chars = mb_str_split('xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx', 1, 'UTF-8');
38
39
        foreach ($chars as $i => $char) {
40
            if ($char === 'x') {
41
                $chars[$i] = dechex(random_int(0, 15));
42
            } elseif ($char === 'y') {
43
                $chars[$i] = dechex(random_int(8, 11));
44
            }
45
        }
46
47
        return implode('', $chars);
48
    }
49
50
    /** @return mixed[] */
51 221
    public static function parseExtLstXml(?SimpleXMLElement $extLstXml): array
52
    {
53 221
        $conditionalFormattingRuleExtensions = [];
54 221
        $conditionalFormattingRuleExtensionXml = null;
55 221
        if ($extLstXml instanceof SimpleXMLElement) {
56 221
            foreach ((count($extLstXml) > 0 ? $extLstXml : [$extLstXml]) as $extLst) {
57
                //this uri is conditionalFormattings
58
                //https://docs.microsoft.com/en-us/openspecs/office_standards/ms-xlsx/07d607af-5618-4ca2-b683-6a78dc0d9627
59 221
                if (isset($extLst->ext['uri']) && (string) $extLst->ext['uri'] === '{78C0D931-6437-407d-A8EE-F0AAD7539E65}') {
60 178
                    $conditionalFormattingRuleExtensionXml = $extLst->ext;
61
                }
62
            }
63
64 221
            if ($conditionalFormattingRuleExtensionXml) {
65 178
                $ns = $conditionalFormattingRuleExtensionXml->getNamespaces(true);
66 178
                $extFormattingsXml = $conditionalFormattingRuleExtensionXml->children($ns['x14']);
67
68 178
                foreach ($extFormattingsXml->children($ns['x14']) as $extFormattingXml) {
69 178
                    $extCfRuleXml = $extFormattingXml->cfRule;
70 178
                    $attributes = $extCfRuleXml->attributes();
71 178
                    if (!$attributes || ((string) $attributes->type) !== Conditional::CONDITION_DATABAR) {
72 176
                        continue;
73
                    }
74
75 2
                    $extFormattingRuleObj = new self((string) $attributes->id);
76 2
                    $extFormattingRuleObj->setSqref((string) $extFormattingXml->children($ns['xm'])->sqref);
77 2
                    $conditionalFormattingRuleExtensions[$extFormattingRuleObj->getId()] = $extFormattingRuleObj;
78
79 2
                    $extDataBarObj = new ConditionalDataBarExtension();
80 2
                    $extFormattingRuleObj->setDataBarExt($extDataBarObj);
81 2
                    $dataBarXml = $extCfRuleXml->dataBar;
82 2
                    self::parseExtDataBarAttributesFromXml($extDataBarObj, $dataBarXml);
83 2
                    self::parseExtDataBarElementChildrenFromXml($extDataBarObj, $dataBarXml, $ns);
84
                }
85
            }
86
        }
87
88 221
        return $conditionalFormattingRuleExtensions;
89
    }
90
91 2
    private static function parseExtDataBarAttributesFromXml(
92
        ConditionalDataBarExtension $extDataBarObj,
93
        SimpleXMLElement $dataBarXml
94
    ): void {
95 2
        $dataBarAttribute = $dataBarXml->attributes();
96 2
        if ($dataBarAttribute === null) {
97
            return;
98
        }
99 2
        if ($dataBarAttribute->minLength) {
100 2
            $extDataBarObj->setMinLength((int) $dataBarAttribute->minLength);
101
        }
102 2
        if ($dataBarAttribute->maxLength) {
103 2
            $extDataBarObj->setMaxLength((int) $dataBarAttribute->maxLength);
104
        }
105 2
        if ($dataBarAttribute->border) {
106 2
            $extDataBarObj->setBorder((bool) (string) $dataBarAttribute->border);
107
        }
108 2
        if ($dataBarAttribute->gradient) {
109 2
            $extDataBarObj->setGradient((bool) (string) $dataBarAttribute->gradient);
110
        }
111 2
        if ($dataBarAttribute->direction) {
112 2
            $extDataBarObj->setDirection((string) $dataBarAttribute->direction);
113
        }
114 2
        if ($dataBarAttribute->negativeBarBorderColorSameAsPositive) {
115 2
            $extDataBarObj->setNegativeBarBorderColorSameAsPositive((bool) (string) $dataBarAttribute->negativeBarBorderColorSameAsPositive);
116
        }
117 2
        if ($dataBarAttribute->axisPosition) {
118 2
            $extDataBarObj->setAxisPosition((string) $dataBarAttribute->axisPosition);
119
        }
120
    }
121
122
    /** @param string[] $ns */
123 2
    private static function parseExtDataBarElementChildrenFromXml(ConditionalDataBarExtension $extDataBarObj, SimpleXMLElement $dataBarXml, array $ns): void
124
    {
125 2
        if ($dataBarXml->borderColor) {
126 2
            $attributes = $dataBarXml->borderColor->attributes();
127 2
            if ($attributes !== null) {
128 2
                $extDataBarObj->setBorderColor((string) $attributes['rgb']);
129
            }
130
        }
131 2
        if ($dataBarXml->negativeFillColor) {
132 2
            $attributes = $dataBarXml->negativeFillColor->attributes();
133 2
            if ($attributes !== null) {
134 2
                $extDataBarObj->setNegativeFillColor((string) $attributes['rgb']);
135
            }
136
        }
137 2
        if ($dataBarXml->negativeBorderColor) {
138 2
            $attributes = $dataBarXml->negativeBorderColor->attributes();
139 2
            if ($attributes !== null) {
140 2
                $extDataBarObj->setNegativeBorderColor((string) $attributes['rgb']);
141
            }
142
        }
143 2
        if ($dataBarXml->axisColor) {
144 2
            $axisColorAttr = $dataBarXml->axisColor->attributes();
145 2
            if ($axisColorAttr !== null) {
146 2
                $extDataBarObj->setAxisColor((string) $axisColorAttr['rgb'], (string) $axisColorAttr['theme'], (string) $axisColorAttr['tint']);
147
            }
148
        }
149 2
        $cfvoIndex = 0;
150 2
        foreach ($dataBarXml->cfvo as $cfvo) {
151 2
            $f = (string) $cfvo->children($ns['xm'])->f;
152 2
            $attributes = $cfvo->attributes();
153 2
            if (!($attributes)) {
154
                continue;
155
            }
156
157 2
            if ($cfvoIndex === 0) {
158 2
                $extDataBarObj->setMinimumConditionalFormatValueObject(new ConditionalFormatValueObject((string) $attributes['type'], null, (empty($f) ? null : $f)));
159
            }
160 2
            if ($cfvoIndex === 1) {
161 2
                $extDataBarObj->setMaximumConditionalFormatValueObject(new ConditionalFormatValueObject((string) $attributes['type'], null, (empty($f) ? null : $f)));
162
            }
163 2
            ++$cfvoIndex;
164
        }
165
    }
166
167 2
    public function getId(): string
168
    {
169 2
        return $this->id;
170
    }
171
172
    public function setId(string $id): self
173
    {
174
        $this->id = $id;
175
176
        return $this;
177
    }
178
179 2
    public function getCfRule(): string
180
    {
181 2
        return $this->cfRule;
182
    }
183
184
    public function setCfRule(string $cfRule): self
185
    {
186
        $this->cfRule = $cfRule;
187
188
        return $this;
189
    }
190
191 2
    public function getDataBarExt(): ConditionalDataBarExtension
192
    {
193 2
        return $this->dataBar;
194
    }
195
196 2
    public function setDataBarExt(ConditionalDataBarExtension $dataBar): self
197
    {
198 2
        $this->dataBar = $dataBar;
199
200 2
        return $this;
201
    }
202
203 2
    public function getSqref(): string
204
    {
205 2
        return $this->sqref;
206
    }
207
208 2
    public function setSqref(string $sqref): self
209
    {
210 2
        $this->sqref = $sqref;
211
212 2
        return $this;
213
    }
214
}
215