• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

PHPOffice / PhpSpreadsheet / 18013950625

25 Sep 2025 04:20PM UTC coverage: 95.867% (+0.3%) from 95.602%
18013950625

push

github

web-flow
Merge pull request #4663 from oleibman/tweakcoveralls

Tweak Coveralls

45116 of 47061 relevant lines covered (95.87%)

373.63 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

83.5
/src/PhpSpreadsheet/Style/ConditionalFormatting/ConditionalFormattingRuleExtension.php
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
    public function __construct(?string $id = null, string $cfRule = self::CONDITION_EXTENSION_DATABAR)
2✔
26
    {
27
        if (null === $id) {
2✔
28
            $this->id = '{' . $this->generateUuid() . '}';
×
29
        } else {
30
            $this->id = $id;
2✔
31
        }
32
        $this->cfRule = $cfRule;
2✔
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
    public static function parseExtLstXml(?SimpleXMLElement $extLstXml): array
224✔
52
    {
53
        $conditionalFormattingRuleExtensions = [];
224✔
54
        $conditionalFormattingRuleExtensionXml = null;
224✔
55
        if ($extLstXml instanceof SimpleXMLElement) {
224✔
56
            foreach ((count($extLstXml) > 0 ? $extLstXml : [$extLstXml]) as $extLst) {
224✔
57
                //this uri is conditionalFormattings
58
                //https://docs.microsoft.com/en-us/openspecs/office_standards/ms-xlsx/07d607af-5618-4ca2-b683-6a78dc0d9627
59
                if (isset($extLst->ext['uri']) && (string) $extLst->ext['uri'] === '{78C0D931-6437-407d-A8EE-F0AAD7539E65}') {
224✔
60
                    $conditionalFormattingRuleExtensionXml = $extLst->ext;
180✔
61
                }
62
            }
63

64
            if ($conditionalFormattingRuleExtensionXml) {
224✔
65
                $ns = $conditionalFormattingRuleExtensionXml->getNamespaces(true);
180✔
66
                $extFormattingsXml = $conditionalFormattingRuleExtensionXml->children($ns['x14']);
180✔
67

68
                foreach ($extFormattingsXml->children($ns['x14']) as $extFormattingXml) {
180✔
69
                    $extCfRuleXml = $extFormattingXml->cfRule;
180✔
70
                    $attributes = $extCfRuleXml->attributes();
180✔
71
                    if (!$attributes || ((string) $attributes->type) !== Conditional::CONDITION_DATABAR) {
180✔
72
                        continue;
178✔
73
                    }
74

75
                    $extFormattingRuleObj = new self((string) $attributes->id);
2✔
76
                    $extFormattingRuleObj->setSqref((string) $extFormattingXml->children($ns['xm'])->sqref);
2✔
77
                    $conditionalFormattingRuleExtensions[$extFormattingRuleObj->getId()] = $extFormattingRuleObj;
2✔
78

79
                    $extDataBarObj = new ConditionalDataBarExtension();
2✔
80
                    $extFormattingRuleObj->setDataBarExt($extDataBarObj);
2✔
81
                    $dataBarXml = $extCfRuleXml->dataBar;
2✔
82
                    self::parseExtDataBarAttributesFromXml($extDataBarObj, $dataBarXml);
2✔
83
                    self::parseExtDataBarElementChildrenFromXml($extDataBarObj, $dataBarXml, $ns);
2✔
84
                }
85
            }
86
        }
87

88
        return $conditionalFormattingRuleExtensions;
224✔
89
    }
90

91
    private static function parseExtDataBarAttributesFromXml(
2✔
92
        ConditionalDataBarExtension $extDataBarObj,
93
        SimpleXMLElement $dataBarXml
94
    ): void {
95
        $dataBarAttribute = $dataBarXml->attributes();
2✔
96
        if ($dataBarAttribute === null) {
2✔
97
            return;
×
98
        }
99
        if ($dataBarAttribute->minLength) {
2✔
100
            $extDataBarObj->setMinLength((int) $dataBarAttribute->minLength);
2✔
101
        }
102
        if ($dataBarAttribute->maxLength) {
2✔
103
            $extDataBarObj->setMaxLength((int) $dataBarAttribute->maxLength);
2✔
104
        }
105
        if ($dataBarAttribute->border) {
2✔
106
            $extDataBarObj->setBorder((bool) (string) $dataBarAttribute->border);
2✔
107
        }
108
        if ($dataBarAttribute->gradient) {
2✔
109
            $extDataBarObj->setGradient((bool) (string) $dataBarAttribute->gradient);
2✔
110
        }
111
        if ($dataBarAttribute->direction) {
2✔
112
            $extDataBarObj->setDirection((string) $dataBarAttribute->direction);
2✔
113
        }
114
        if ($dataBarAttribute->negativeBarBorderColorSameAsPositive) {
2✔
115
            $extDataBarObj->setNegativeBarBorderColorSameAsPositive((bool) (string) $dataBarAttribute->negativeBarBorderColorSameAsPositive);
2✔
116
        }
117
        if ($dataBarAttribute->axisPosition) {
2✔
118
            $extDataBarObj->setAxisPosition((string) $dataBarAttribute->axisPosition);
2✔
119
        }
120
    }
121

122
    /** @param string[] $ns */
123
    private static function parseExtDataBarElementChildrenFromXml(ConditionalDataBarExtension $extDataBarObj, SimpleXMLElement $dataBarXml, array $ns): void
2✔
124
    {
125
        if ($dataBarXml->borderColor) {
2✔
126
            $attributes = $dataBarXml->borderColor->attributes();
2✔
127
            if ($attributes !== null) {
2✔
128
                $extDataBarObj->setBorderColor((string) $attributes['rgb']);
2✔
129
            }
130
        }
131
        if ($dataBarXml->negativeFillColor) {
2✔
132
            $attributes = $dataBarXml->negativeFillColor->attributes();
2✔
133
            if ($attributes !== null) {
2✔
134
                $extDataBarObj->setNegativeFillColor((string) $attributes['rgb']);
2✔
135
            }
136
        }
137
        if ($dataBarXml->negativeBorderColor) {
2✔
138
            $attributes = $dataBarXml->negativeBorderColor->attributes();
2✔
139
            if ($attributes !== null) {
2✔
140
                $extDataBarObj->setNegativeBorderColor((string) $attributes['rgb']);
2✔
141
            }
142
        }
143
        if ($dataBarXml->axisColor) {
2✔
144
            $axisColorAttr = $dataBarXml->axisColor->attributes();
2✔
145
            if ($axisColorAttr !== null) {
2✔
146
                $extDataBarObj->setAxisColor((string) $axisColorAttr['rgb'], (string) $axisColorAttr['theme'], (string) $axisColorAttr['tint']);
2✔
147
            }
148
        }
149
        $cfvoIndex = 0;
2✔
150
        foreach ($dataBarXml->cfvo as $cfvo) {
2✔
151
            $f = (string) $cfvo->children($ns['xm'])->f;
2✔
152
            $attributes = $cfvo->attributes();
2✔
153
            if (!($attributes)) {
2✔
154
                continue;
×
155
            }
156

157
            if ($cfvoIndex === 0) {
2✔
158
                $extDataBarObj->setMinimumConditionalFormatValueObject(new ConditionalFormatValueObject((string) $attributes['type'], null, (empty($f) ? null : $f)));
2✔
159
            }
160
            if ($cfvoIndex === 1) {
2✔
161
                $extDataBarObj->setMaximumConditionalFormatValueObject(new ConditionalFormatValueObject((string) $attributes['type'], null, (empty($f) ? null : $f)));
2✔
162
            }
163
            ++$cfvoIndex;
2✔
164
        }
165
    }
166

167
    public function getId(): string
2✔
168
    {
169
        return $this->id;
2✔
170
    }
171

172
    public function setId(string $id): self
×
173
    {
174
        $this->id = $id;
×
175

176
        return $this;
×
177
    }
178

179
    public function getCfRule(): string
2✔
180
    {
181
        return $this->cfRule;
2✔
182
    }
183

184
    public function setCfRule(string $cfRule): self
×
185
    {
186
        $this->cfRule = $cfRule;
×
187

188
        return $this;
×
189
    }
190

191
    public function getDataBarExt(): ConditionalDataBarExtension
2✔
192
    {
193
        return $this->dataBar;
2✔
194
    }
195

196
    public function setDataBarExt(ConditionalDataBarExtension $dataBar): self
2✔
197
    {
198
        $this->dataBar = $dataBar;
2✔
199

200
        return $this;
2✔
201
    }
202

203
    public function getSqref(): string
2✔
204
    {
205
        return $this->sqref;
2✔
206
    }
207

208
    public function setSqref(string $sqref): self
2✔
209
    {
210
        $this->sqref = $sqref;
2✔
211

212
        return $this;
2✔
213
    }
214
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc