Passed
Push — master ( a950d1...8a122f )
by Mark
14:29
created

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