tablepress
/
libraries
/
vendor
/
PhpSpreadsheet
/
Style
/
ConditionalFormatting
/
ConditionalFormattingRuleExtension.php
ConditionalFormattingRuleExtension.php in TablePress – Tables in WordPress made easy 3.4, at libraries/vendor/PhpSpreadsheet/Style/ConditionalFormatting/ConditionalFormattingRuleExtension.php
| 1 | <?php |
| 2 | |
| 3 | namespace TablePress\PhpOffice\PhpSpreadsheet\Style\ConditionalFormatting; |
| 4 | |
| 5 | use TablePress\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) |
| 26 | { |
| 27 | if (null === $id) { |
| 28 | $this->id = '{' . $this->generateUuid() . '}'; |
| 29 | } else { |
| 30 | $this->id = $id; |
| 31 | } |
| 32 | $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 | public static function parseExtLstXml(?SimpleXMLElement $extLstXml): array |
| 52 | { |
| 53 | $conditionalFormattingRuleExtensions = []; |
| 54 | $conditionalFormattingRuleExtensionXml = null; |
| 55 | if ($extLstXml instanceof SimpleXMLElement) { |
| 56 | 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 | if (isset($extLst->ext['uri']) && (string) $extLst->ext['uri'] === '{78C0D931-6437-407d-A8EE-F0AAD7539E65}') { |
| 60 | $conditionalFormattingRuleExtensionXml = $extLst->ext; |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | if ($conditionalFormattingRuleExtensionXml) { |
| 65 | $ns = $conditionalFormattingRuleExtensionXml->getNamespaces(true); |
| 66 | $extFormattingsXml = $conditionalFormattingRuleExtensionXml->children($ns['x14']); |
| 67 | |
| 68 | foreach ($extFormattingsXml->children($ns['x14']) as $extFormattingXml) { |
| 69 | $extCfRuleXml = $extFormattingXml->cfRule; |
| 70 | $attributes = $extCfRuleXml->attributes(); |
| 71 | if (!$attributes || ((string) $attributes->type) !== Conditional::CONDITION_DATABAR) { |
| 72 | continue; |
| 73 | } |
| 74 | |
| 75 | $extFormattingRuleObj = new self((string) $attributes->id); |
| 76 | $extFormattingRuleObj->setSqref((string) $extFormattingXml->children($ns['xm'])->sqref); |
| 77 | $conditionalFormattingRuleExtensions[$extFormattingRuleObj->getId()] = $extFormattingRuleObj; |
| 78 | |
| 79 | $extDataBarObj = new ConditionalDataBarExtension(); |
| 80 | $extFormattingRuleObj->setDataBarExt($extDataBarObj); |
| 81 | $dataBarXml = $extCfRuleXml->dataBar; |
| 82 | self::parseExtDataBarAttributesFromXml($extDataBarObj, $dataBarXml); |
| 83 | self::parseExtDataBarElementChildrenFromXml($extDataBarObj, $dataBarXml, $ns); |
| 84 | } |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | return $conditionalFormattingRuleExtensions; |
| 89 | } |
| 90 | |
| 91 | private static function parseExtDataBarAttributesFromXml( |
| 92 | ConditionalDataBarExtension $extDataBarObj, |
| 93 | SimpleXMLElement $dataBarXml |
| 94 | ): void { |
| 95 | $dataBarAttribute = $dataBarXml->attributes(); |
| 96 | if ($dataBarAttribute === null) { |
| 97 | return; |
| 98 | } |
| 99 | if ($dataBarAttribute->minLength) { |
| 100 | $extDataBarObj->setMinLength((int) $dataBarAttribute->minLength); |
| 101 | } |
| 102 | if ($dataBarAttribute->maxLength) { |
| 103 | $extDataBarObj->setMaxLength((int) $dataBarAttribute->maxLength); |
| 104 | } |
| 105 | if ($dataBarAttribute->border) { |
| 106 | $extDataBarObj->setBorder((bool) (string) $dataBarAttribute->border); |
| 107 | } |
| 108 | if ($dataBarAttribute->gradient) { |
| 109 | $extDataBarObj->setGradient((bool) (string) $dataBarAttribute->gradient); |
| 110 | } |
| 111 | if ($dataBarAttribute->direction) { |
| 112 | $extDataBarObj->setDirection((string) $dataBarAttribute->direction); |
| 113 | } |
| 114 | if ($dataBarAttribute->negativeBarBorderColorSameAsPositive) { |
| 115 | $extDataBarObj->setNegativeBarBorderColorSameAsPositive((bool) (string) $dataBarAttribute->negativeBarBorderColorSameAsPositive); |
| 116 | } |
| 117 | if ($dataBarAttribute->axisPosition) { |
| 118 | $extDataBarObj->setAxisPosition((string) $dataBarAttribute->axisPosition); |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | /** @param string[] $ns */ |
| 123 | private static function parseExtDataBarElementChildrenFromXml(ConditionalDataBarExtension $extDataBarObj, SimpleXMLElement $dataBarXml, array $ns): void |
| 124 | { |
| 125 | if ($dataBarXml->borderColor) { |
| 126 | $attributes = $dataBarXml->borderColor->attributes(); |
| 127 | if ($attributes !== null) { |
| 128 | $extDataBarObj->setBorderColor((string) $attributes['rgb']); |
| 129 | } |
| 130 | } |
| 131 | if ($dataBarXml->negativeFillColor) { |
| 132 | $attributes = $dataBarXml->negativeFillColor->attributes(); |
| 133 | if ($attributes !== null) { |
| 134 | $extDataBarObj->setNegativeFillColor((string) $attributes['rgb']); |
| 135 | } |
| 136 | } |
| 137 | if ($dataBarXml->negativeBorderColor) { |
| 138 | $attributes = $dataBarXml->negativeBorderColor->attributes(); |
| 139 | if ($attributes !== null) { |
| 140 | $extDataBarObj->setNegativeBorderColor((string) $attributes['rgb']); |
| 141 | } |
| 142 | } |
| 143 | if ($dataBarXml->axisColor) { |
| 144 | $axisColorAttr = $dataBarXml->axisColor->attributes(); |
| 145 | if ($axisColorAttr !== null) { |
| 146 | $extDataBarObj->setAxisColor((string) $axisColorAttr['rgb'], (string) $axisColorAttr['theme'], (string) $axisColorAttr['tint']); |
| 147 | } |
| 148 | } |
| 149 | $cfvoIndex = 0; |
| 150 | foreach ($dataBarXml->cfvo as $cfvo) { |
| 151 | $f = (string) $cfvo->children($ns['xm'])->f; |
| 152 | $attributes = $cfvo->attributes(); |
| 153 | if (!($attributes)) { |
| 154 | continue; |
| 155 | } |
| 156 | |
| 157 | if ($cfvoIndex === 0) { |
| 158 | $extDataBarObj->setMinimumConditionalFormatValueObject(new ConditionalFormatValueObject((string) $attributes['type'], null, (empty($f) ? null : $f))); |
| 159 | } |
| 160 | if ($cfvoIndex === 1) { |
| 161 | $extDataBarObj->setMaximumConditionalFormatValueObject(new ConditionalFormatValueObject((string) $attributes['type'], null, (empty($f) ? null : $f))); |
| 162 | } |
| 163 | ++$cfvoIndex; |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | public function getId(): string |
| 168 | { |
| 169 | return $this->id; |
| 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 |
| 180 | { |
| 181 | return $this->cfRule; |
| 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 |
| 192 | { |
| 193 | return $this->dataBar; |
| 194 | } |
| 195 | |
| 196 | public function setDataBarExt(ConditionalDataBarExtension $dataBar): self |
| 197 | { |
| 198 | $this->dataBar = $dataBar; |
| 199 | |
| 200 | return $this; |
| 201 | } |
| 202 | |
| 203 | public function getSqref(): string |
| 204 | { |
| 205 | return $this->sqref; |
| 206 | } |
| 207 | |
| 208 | public function setSqref(string $sqref): self |
| 209 | { |
| 210 | $this->sqref = $sqref; |
| 211 | |
| 212 | return $this; |
| 213 | } |
| 214 | } |
| 215 |